c# comboBOX 属性selectedItem问题

jiang13824690 2018-07-24 08:40:20
1. 目的:获取当前下拉框选项值
2. String str = combox.selectedItem;
3. 提示错误:无法将object 方法组转换未String类型
4.为何:combox.selectedItem = str可以编译,而2)中示例就出错。
5. 感谢各位帮忙,在此谢过
...全文
2745 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
E次奥 2018-07-31
  • 打赏
  • 举报
回复
combox.selectedItem 返回的是object类型,而object类型是所有类型的基类; 所以string可以给object赋值,而object不能直接给string赋值,必须转换后才能赋值;
foxruo 2018-07-31
  • 打赏
  • 举报
回复
ToString()
xinbada1985 2018-07-31
  • 打赏
  • 举报
回复
String str = combox.selectedItem.tostring()
回答:combox.selectedItem的值是object类型,他类似于泛型,也就是任何类型值都可以赋值给他,包括一个Entity这样的一个class类,但是当你往回赋值是String类型相当于一个基本类型,只能接受String类型的值,所以你必须把object类型的值强制转换成String类型传值给他!
大然然 2018-07-27
  • 打赏
  • 举报
回复
你combox绑定的是一个对象数组,所以你selectedItem的也就是一个数组,
string str =combox. selectedItem显然是错的,因为你取的不是某个对象,而是对象里的某个值

if(combox.selectedItem !=null)
{
你的对象 obj = combox.selectedItem as 你的对象
string str = obj.属性
}
或者tostring()
CPU_2 2018-07-27
  • 打赏
  • 举报
回复
首先先检查一下那个dropDownList 的数据源 如 ddlMy.Items.Add(new ListItem(key, val)); 然后取值是否是使用 string getSelMyVal= ddlMy.SelectedValue.ToString();
weixin_42245467 2018-07-27
  • 打赏
  • 举报
回复
String str = combox.selectedItem.tostring();
gabriel37 2018-07-27
  • 打赏
  • 举报
回复
String str = combox.text.trim()
佛系Sheep 2018-07-25
  • 打赏
  • 举报
回复
selectedItem.Tostring()正解
Tll_W 2018-07-25
  • 打赏
  • 举报
回复 1
这个问题牵涉到类型转换的问题,具体来说就是对象间的继承问题了。
首先明确一点,就是所有的数据类型都是继承自object,所以object obj1 = 。。。。。,所以对象都可以赋值给这个。
但是如果你把基础类的对象赋值给高级对象,那肯定是不行的,比如string str1 = 一个object类型的对象,这个object级别太低了,string就不认你,你得用一些方法让string认识,比如上面说的tostring方法,就可以解决问题。
FainSheeg 2018-07-24
  • 打赏
  • 举报
回复
就是一个类型转换的问题嘛:
str=selecteditem 要求object转string,你出错了,说明无法隐式转换
selecteditem=str要求string转object,肯定是可以的。
threenewbee 2018-07-24
  • 打赏
  • 举报
回复
将object 方法组转换
说明你调用了一个方法,但是没有打括号。
比如
string foo()
{
return "aaa";
}
如果是
string a = foo; //报错。
应该是
string a = foo();
Roock 2018-07-24
  • 打赏
  • 举报
回复
ToString一下看看。
string str = this.comboBox1.SelectedItem.ToString();
MessageBox.Show(str);
自然搬运工 2018-07-24
  • 打赏
  • 举报
回复
获取text的话直接用String str = combox.selectedItem.Tostring();
public partial class Form1 : Form { public Form1() { InitializeComponent(); } SerialPort port1 = new SerialPort(); string InputData = String.Empty; delegate void SetTextCallback(string text); private void Port_Select() {//获取机器中的串口地址 string[] ports = SerialPort.GetPortNames(); foreach (string port in ports) { comboBox1.Items.Add(port); } } private void Form1_Load_1(object sender, EventArgs e) { Port_Select(); this.comboBox1.SelectedIndex = 0; this.comboBox2.SelectedIndex = 0; } private void button1_Click(object sender, EventArgs e) { if (button1.Text == "关闭串口") //当要关闭串口的时候 { port1.DiscardOutBuffer(); port1.DiscardInBuffer(); port1.Close(); button1.Text = "打开串口"; label3.Text = "串口当前状况:未打开"; comboBox1.Enabled = true; comboBox2.Enabled = true; } else if (button1.Text == "打开串口") //当要打开串口的时候 { try { port1.PortName = comboBox1.SelectedItem.ToString(); port1.BaudRate = Convert.ToInt32(comboBox2.SelectedItem); port1.DataBits = 8; port1.RtsEnable = true; port1.Open(); port1.DiscardOutBuffer(); port1.DiscardInBuffer(); button1.Text = "关闭串口"; comboBox1.Enabled = false; comboBox2.Enabled = false; label3.Text = "串口:" + comboBox1.SelectedItem.ToString() + " 波特率:" + comboBox2.SelectedItem.ToString() + " 数据位:8 "; } catch { button1.Text = "打开串口"; label3.Text = "串口:" + comboBox1.SelectedItem.ToString() + "打开失败"; MessageBox.Show("该串口无法打开"); } } } 资源中部分代码

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧