Search code examples
c#wpftextboxlistboxlistboxitem

c# textbox shows object name instead of value


I have a form written in c#, with various drop down lists, but I'm having problems with a listbox on the form. I need to populate a textbox with the values selected from the listbox when I double click on them. I've got the click event working, but the textbox will only populate with the object name, not the value from the listbox

i.e.

'System.Windows.Controls.SelectedItemCollection'

instead of the actual value.

Here is the entire code block I'm working on:

I should have just done this at the start - here is the complete code block I'm working on:

else if (theValue.FieldName.Equals("UIPathList", StringComparison.OrdinalIgnoreCase) == true)
            {
                int nRow = 14;
                Button theUIPathOptionsButton = new Button();

                TextBox theOldValueTextBox = AddLabelAndOldValue(theHelper, nRow, theValue);
                theOldValueTextBox.Text = theValue.OldValue.Replace(",", "," + Environment.NewLine);

                theUIPathOuterStackPanel = new StackPanel
                {
                    Visibility = Visibility.Visible,
                    Orientation = Orientation.Vertical,
                    Background = new SolidColorBrush(Colors.White),
                    ClipToBounds = true,
                };

                theUIPathOptionsInnerStackPanel = new StackPanel
                {
                    Visibility = Visibility.Visible,
                    Orientation = Orientation.Horizontal,
                    Background = new SolidColorBrush(Colors.White)
                };
                theUIPathOuterStackPanel.ClipToBounds = true;

                TextBox theNewTextBox = new TextBox
                {
                    TabIndex = nRow,
                    TextWrapping = TextWrapping.Wrap,
                    AcceptsReturn = true,
                };

                theNewTextBox.Clear();
                theNewTextBox.MouseDoubleClick += MultiLineChildDatapointList_HandleMouseDoubleClick;
                theNewTextBox.Focusable = true;
                theNewTextBox.HorizontalAlignment = HorizontalAlignment.Stretch;
                theNewTextBox.Width = 365;

                theNewTextBox.PreviewKeyDown += theGetMetadataHelper.Preview_KeyDown_IsMultilineText;

                theNewTextBox.Tag = theValue;

                ListBox theUIPathOptionslistBox = new ListBox();
                theUIPathOptionslistBox.Items.Add("RuntimeDefaults");
                theUIPathOptionslistBox.Items.Add("CommonSettings");
                theUIPathOptionslistBox.Items.Add(InputDatapointManager.CONST_CHANGE_RECORD_CHANGES_CLEAR_VALUE);
                theUIPathOptionslistBox.TabIndex = nRow;
                theUIPathOptionslistBox.SelectionMode = SelectionMode.Multiple;
                theUIPathOptionslistBox.ClipToBounds = true;
                theUIPathOptionslistBox.Focusable = true;
                theUIPathOptionslistBox.Visibility = Visibility.Hidden;
                theUIPathOptionslistBox.Height = 34;

                theUIPathOptionsInnerStackPanel.Children.Add(theNewTextBox);
                theUIPathOptionsInnerStackPanel.Children.Add(theUIPathOptionsButton);

                theUIPathOuterStackPanel.Children.Add(theUIPathOptionsInnerStackPanel);
                theUIPathOuterStackPanel.Children.Add(theUIPathOptionslistBox);

                void button1_click(object sender, EventArgs e)
                {
                    theUIPathOptionslistBox.Visibility = Visibility.Visible;
                }

                void button1_doubleclick(object sender, EventArgs e)
                {
                    theNewTextBox.Text = theUIPathOptionslistBox.SelectedItem.ToString();
                }

                theUIPathOptionsButton.Click += button1_click;
                theUIPathOptionslistBox.MouseDoubleClick += button1_doubleclick;

                Grid.SetColumn(theUIPathOuterStackPanel, 4);
                Grid.SetRow(theUIPathOuterStackPanel, nRow);
                theDataGrid.Children.Add(theUIPathOuterStackPanel);

                theEditControlList.Add(theNewTextBox);
            }

Solution

  • ListBox, Item is a collection of objects, not strings, so you must let it know how to convert it to string, otherwise it will use its defualt .ToString() function that obviously the object currently in your items not giving the desired result.

    Imagine items are oftype following class:

    class SomeClass
    {
          public int Id;
          public string Name;
    }
    

    You may do one of these three:

    1.set the DisplayMember of your ListBox to Name

    2.add override method to your class so that it overrides its .ToString() and return its Name property:

    class SomeClass
    {
          public int Id;
          public string Name;
    
          public override string ToString()
          {
                return Name;
          }
    }
    

    3.Just cast it to its real type and get the property you want:

    SomeClass selected = (SomeClass)ListBox.SelectedItem;
    TextBox1.Text = selected.Name;