Get listbox into textbox

13 views (last 30 days)
Zafer Sahin
Zafer Sahin on 7 Oct 2021
Commented: Dave B on 8 Oct 2021
Hi, I want to get values from listbox to insert textbox by adding sentence.:
I have cities in listbox(milano, atina, istanbul, amsterdam)
When I choose amsterdam, sholud be shown in the textbox "Selected city is Amsterdam"
Next I choose milano, sholud be shown in the textbox "Selected cities is Amsterdam, Milano"
Next I choose istanbul, sholud be shown in the textbox "Selected cities is Amsterdam, Milano, İstanbul"
I try but I only know "string" and "value" command so I can't solve how to add directly string text and adding to each other. thanks for interest.
city=get(handles.listbox1,'string');
set(handles.edit1,'string',city);

Accepted Answer

Dave B
Dave B on 8 Oct 2021
Edited: Dave B on 8 Oct 2021
Here's the modern uifigure/appdesigner style way:
u = uifigure;
lst = uilistbox(u, 'Multiselect', true, 'Position', [50 50 100 300], 'Items', ["Amsterdam" "Milano" "Istanbul" "Natick"]);
txt = uieditfield(u, 'Position', [175 325 300 25]);
lst.ValueChangedFcn = @(~,~)set(txt, 'Value', "Selected cities are: " + string(join(lst.Value,', ')));
Here's the classic figure/guide/uicontrol style way:
f = figure;
lst = uicontrol(f, 'Style','listbox', 'Max',2, 'Position', [50 50 100 300], 'String', ["Amsterdam" "Milano" "Istanbul" "Natick"]);
txt = uicontrol(f, 'Style', 'edit', 'Position', [175 325 300 25]);
lst.Callback = @(~,~)set(txt,'String', "Selected cities are: " + join(lst.String(lst.Value),', '));
The bit you're looking for is the same, you can use join (or strjoin if you prefer).
  4 Comments
Zafer Sahin
Zafer Sahin on 8 Oct 2021
Yes, I mean keep of previous names as you said. This codes work perfect.
I wonder how I use app designer codes which you wrote, since I cant use directy copy/paste to code window. When I try, It gives error as a "Class name and filename must match".
Also I cant edit app designer code view menu. Thanks for helps.
Dave B
Dave B on 8 Oct 2021
Yeah I can see how that would be confusing. I'm guessing you might run into other problems if you made the class name and filename match (the class name is the app1 bit right after classdef, and the filename is...well...the name of the file that you save it as). I was thinking you'd be making this in App Designer.
I included what all of my App Designer code looked like, but most of it was autogenerated and not editable.
The steps would look like:
  • Create an app in App Designer
  • Add ListBox and EditField components interactively in the Design View
  • Switch to Code View
  • Look for the 'Code Browser', select 'Callbacks' and hit the + (or click Callback in the toolstrip)
  • Choose the ListBox component (or whatever you named the ListBox)
  • (you can give your callback function a custom name if you like) Click Add Callback
  • Now you can edit code for the callback, this is where you'd add the stuff in my ListBoxValueChanged above
  • Back in Code Browser, click Properties and then the + (or click Property in the toolstrip), this will add a new private property.
  • You can name it how you like (I chose SelectedCities), and you can also constrain the shape and type as I did (up to you!)
Sorry that looks like a lot of steps, but it really only takes like 30 seconds when you've done this kind of thing before!

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Tags

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!