Link to home
Start Free TrialLog in
Avatar of Olukayode Oluwole
Olukayode OluwoleFlag for Canada

asked on

How can I return a part of a list to a field on the screen

I am working in a C#  application

I have constituted  a List with the script Below:

StaffNameListBox.DataSource = GlobalConfig.Connection.GetEmployees_All();
StaffNameListBox.DisplayMember = "FullStaffName";

 public string FullStaffName
        {
            get
            {
                return $"{Staff_no}  { Surname } {Firname }";
            }
        }

(See  the Screen of The list below showing a record in the list

On the Click event on this record I want to return a part of the string (ie just the staff_no)

 to a text box on the screen.  The name of the textbox is StaffNoValue


My click event syntax  is as below:

  private void StaffNameListBox_Click(object sender, EventArgs e)
        {
            StaffNoValue.Text = StaffNameListBox.ValueMember;
        }

What I want Returned to the text box on screen is as shown below

StaffNoValue.Text = "N001"

How can I get a portion of a list returned into a field using the actual name with which the list was loaded

and not by searching for a sub string of the selected item

Thanks
Selection-From-List--11-01-2019.PNG
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Don't use StaffNameListBox.ValueMember - that property is used for data mapping just like the DisplayMember properly you used.

Instead, try StaffNameListBox.SelectedItem to get back the databound record.

That will return an "object" that you can cast to an Employee record and from there you should have access to every field on that record.
Avatar of Olukayode Oluwole

ASKER

Am i getting the syntax wrong ??

Cant go past StaffNameListBox.SelectedItem

It is being flagged  red

See  attached

Please  help with the syntax including how to cast it

Thanks

O.A.  Oluwole
SelectedItemError12012019.PNG
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Worked like magic.

You made my day

Thanks
Once again  Thanks