c# 文本框显示对象名称而不是值
c# textbox shows object name instead of value
我有一个用 c# 编写的表单,其中包含各种下拉列表,但我在表单上的列表框方面遇到了问题。当我双击它们时,我需要用从列表框中选择的值填充一个文本框。我的点击事件有效,但文本框只会填充对象名称,而不是列表框中的值
即
'System.Windows.Controls.SelectedItemCollection'
而不是实际值。
这是我正在处理的整个代码块:
我应该在一开始就这样做 - 这是我正在处理的完整代码块:
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);
}
这是因为 TextBox
将对其绑定的内容使用 ToString()
方法,默认情况下将 return class 名称。
您的解决方案是将 class 的 ToString()
方法重写为 return 您想要的值,或者设置 [=10= 的文本 属性 ] 到您想要的文本值而不是对象。
ListBox, Item 是对象的集合,不是字符串,所以你必须让它知道如何将它转换为字符串,否则它会使用它的默认 .ToString()
函数,显然你的项目中当前的对象没有给出想要的结果。
假设项目的类型如下 class:
class SomeClass
{
public int Id;
public string Name;
}
您可以选择以下三项之一:
1.set 你的 ListBox 的 DisplayMember
到 Name
2.add 将方法覆盖到您的 class 以便它覆盖其 .ToString()
和 return 其名称 属性:
class SomeClass
{
public int Id;
public string Name;
public override string ToString()
{
return Name;
}
}
3.Just 将它转换为它的真实类型并得到你想要的 属性:
SomeClass selected = (SomeClass)ListBox.SelectedItem;
TextBox1.Text = selected.Name;
这里(可能)已经回答了这个问题:Getting value of selected item in list box as string
string myItem = listBox1.GetItemText(listBox1.SelectedItem);
然后您只需将您的项目添加到文本框即可:
textBox1.Text = myItem;
如果您不想创建新的字符串变量,那么这个也可以:
textBox1.Text = listBox1.SelectedItem.ToString();
最后得到的解决方案如下:
void theUIPathOptionslistBox_SelectedIndexChanged(object sender,
SelectionChangedEventArgs e)
{
theNewTextBox.Clear();
foreach (object selectedItem in theUIPathOptionslistBox.SelectedItems)
{
theNewTextBox.AppendText(selectedItem.ToString() + Environment.NewLine);
}
}
theUIPathOptionslistBox.SelectionChanged +=
theUIPathOptionslistBox_SelectedIndexChanged;
我有一个用 c# 编写的表单,其中包含各种下拉列表,但我在表单上的列表框方面遇到了问题。当我双击它们时,我需要用从列表框中选择的值填充一个文本框。我的点击事件有效,但文本框只会填充对象名称,而不是列表框中的值
即
'System.Windows.Controls.SelectedItemCollection'
而不是实际值。
这是我正在处理的整个代码块:
我应该在一开始就这样做 - 这是我正在处理的完整代码块:
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);
}
这是因为 TextBox
将对其绑定的内容使用 ToString()
方法,默认情况下将 return class 名称。
您的解决方案是将 class 的 ToString()
方法重写为 return 您想要的值,或者设置 [=10= 的文本 属性 ] 到您想要的文本值而不是对象。
ListBox, Item 是对象的集合,不是字符串,所以你必须让它知道如何将它转换为字符串,否则它会使用它的默认 .ToString()
函数,显然你的项目中当前的对象没有给出想要的结果。
假设项目的类型如下 class:
class SomeClass
{
public int Id;
public string Name;
}
您可以选择以下三项之一:
1.set 你的 ListBox 的 DisplayMember
到 Name
2.add 将方法覆盖到您的 class 以便它覆盖其 .ToString()
和 return 其名称 属性:
class SomeClass
{
public int Id;
public string Name;
public override string ToString()
{
return Name;
}
}
3.Just 将它转换为它的真实类型并得到你想要的 属性:
SomeClass selected = (SomeClass)ListBox.SelectedItem;
TextBox1.Text = selected.Name;
这里(可能)已经回答了这个问题:Getting value of selected item in list box as string
string myItem = listBox1.GetItemText(listBox1.SelectedItem);
然后您只需将您的项目添加到文本框即可:
textBox1.Text = myItem;
如果您不想创建新的字符串变量,那么这个也可以:
textBox1.Text = listBox1.SelectedItem.ToString();
最后得到的解决方案如下:
void theUIPathOptionslistBox_SelectedIndexChanged(object sender,
SelectionChangedEventArgs e)
{
theNewTextBox.Clear();
foreach (object selectedItem in theUIPathOptionslistBox.SelectedItems)
{
theNewTextBox.AppendText(selectedItem.ToString() + Environment.NewLine);
}
}
theUIPathOptionslistBox.SelectionChanged +=
theUIPathOptionslistBox_SelectedIndexChanged;