C# c文本框显示对象名称而不是值
我有一个用c写的表单,有各种下拉列表,但是表单上的列表框有问题。双击列表框时,我需要用从列表框中选择的值填充文本框。我已经让click事件工作,但是textbox将只填充对象名,而不是列表框中的值 i、 e 'System.Windows.Controls.SelectedItemCollection' 而不是实际值 下面是我正在处理的整个代码块:C# c文本框显示对象名称而不是值,c#,wpf,textbox,listbox,listboxitem,C#,Wpf,Textbox,Listbox,Listboxitem,我有一个用c写的表单,有各种下拉列表,但是表单上的列表框有问题。双击列表框时,我需要用从列表框中选择的值填充文本框。我已经让click事件工作,但是textbox将只填充对象名,而不是列表框中的值 i、 e 'System.Windows.Controls.SelectedItemCollection' 而不是实际值 下面是我正在处理的整个代码块: else if (theValue.FieldName.Equals("UIPathList", StringComparison.OrdinalI
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);
}
我应该在一开始就这样做-下面是我正在处理的完整代码块:
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方法,默认情况下,该方法将返回类名
解决方案是重写类的ToString方法以返回所需的值,或者将TextBox的text属性设置为所需的文本值,而不是对象。ListBox,Item是对象的集合,而不是字符串,因此必须让它知道如何将其转换为字符串,否则,它将使用其default.ToString函数,显然,当前项目中的对象不会给出期望的结果 假设项目的类型如下所示:
class SomeClass
{
public int Id;
public string Name;
}
您可以执行以下三种操作之一:
1.将ListBox的DisplayMember设置为Name
2.将override方法添加到类中,使其重写其.ToString并返回其Name属性:
class SomeClass
{
public int Id;
public string Name;
public override string ToString()
{
return Name;
}
}
3.只需将其转换为真实类型,即可获得所需的属性:
SomeClass selected = (SomeClass)ListBox.SelectedItem;
TextBox1.Text = selected.Name;
这可能已经在这里得到了回答: 然后,您只需将项目添加到文本框:
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;
你应该分享你的代码。听起来你是在将文本设置为一个对象,而不是文本值。将其设置为所需的值。所选项目是一个对象。您需要将其强制转换为实际的类型,并将该类型的相应属性指定给textbox value.void按钮1\u双击对象发送者,EventArgs e{theNewTextBox.Text=Options ListBox.SelectedItems.ToString;}当我尝试使用“GetItemText”时,VS告诉我ListBox不包含“GetItemText”的定义-是否需要引入特定的using语句?您只需要使用System.Windows.Forms。刚刚测试了以下行:textBox1.Text=listBox1.SelectedItem.ToString;不幸的是,在这个表单的上下文中,我不能使用Windows.Forms-它会弄乱表单上的所有其他控件、文本框等。谢谢你!此外,我不能使用“textBox1.Text=listBox1.SelectedItem.ToString;”建议-这就是我一直在使用的,并且没有显示所选项目的文本-尝试实现建议的覆盖,但它会干扰此表单上的其他控件等…你的意思是这样的:newtextbox.text=我想要显示的值吗?很抱歉,没有问题,但是当我尝试创建类时,我开始从我所有的其他控件中得到错误,等等-我应该把这个类放在哪里?我觉得这更复杂,因为这段代码在一个巨大的if语句的另一部分中抱歉回答晚了,你不需要创建一个类,我正在设置一个列表框当前输入的示例,它类似于我提到的一个类,为了得到更好的答案,你应该发布你的代码,事实上,如果你发布了你的代码,我们知道项目的确切代码来自哪里,你会在几分钟内收到很多好的答案,你对哪一部分有问题?嗨,阿什坎-如果我提到这是WPF而不是winforms,会有帮助吗?非常感谢你的帮助。我已经下载了这个文件,所以你可以删除评论。