社区
C#
帖子详情
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
打赏
收藏
c# comboBOX 属性selectedItem问题
1. 目的:获取当前下拉框选项值 2. String str = combox.selectedItem; 3. 提示错误:无法将object 方法组转换未String类型 4.为何:combox.selectedItem = str可以编译,而2)中示例就出错。 5. 感谢各位帮忙,在此谢过
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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();
Winform中
ComboBox
控件使用总结
此为示例程序源码. 其中Form1窗体演示了为
ComboBox
添加和移除项的使用方法; Form2窗体演示了
ComboBox
从数据库获取数据源的用法,以及Text、SelectedText、SelectedValue和
SelectedItem
的区别。 n年前写的一个非常简单的小例子。开发环境为vs2008,有一个页面连了一下SQL SERVER的pubs数据库
C#
串口通信,包括打开串口,接收发送数据
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) {
comboBox
1.Items.Add(port); } } private void Form1_Load_1(object sender, EventArgs e) { Port_Select(); this.
comboBox
1.SelectedIndex = 0; this.
comboBox
2.SelectedIndex = 0; } private void button1_Click(object sender, EventArgs e) { if (button1.Text == "关闭串口") //当要关闭串口的时候 { port1.DiscardOutBuffer(); port1.DiscardInBuffer(); port1.Close(); button1.Text = "打开串口"; label3.Text = "串口当前状况:未打开";
comboBox
1.Enabled = true;
comboBox
2.Enabled = true; } else if (button1.Text == "打开串口") //当要打开串口的时候 { try { port1.PortName =
comboBox
1.
SelectedItem
.ToString(); port1.BaudRate = Convert.ToInt32(
comboBox
2.
SelectedItem
); port1.DataBits = 8; port1.RtsEnable = true; port1.Open(); port1.DiscardOutBuffer(); port1.DiscardInBuffer(); button1.Text = "关闭串口";
comboBox
1.Enabled = false;
comboBox
2.Enabled = false; label3.Text = "串口:" +
comboBox
1.
SelectedItem
.ToString() + " 波特率:" +
comboBox
2.
SelectedItem
.ToString() + " 数据位:8 "; } catch { button1.Text = "打开串口"; label3.Text = "串口:" +
comboBox
1.
SelectedItem
.ToString() + "打开失败"; MessageBox.Show("该串口无法打开"); } } } 资源中部分代码
面向对象
C#
初级入门精讲(6)WinForm基础
欢迎加入QQ群538724338咨询提问 【课程特色】 1、课程设计循序渐进、讲解细致、通俗易懂、非常适合自主学习 2、教学过程实例丰富、强调技术关键点、并且分析透彻 3、物美价廉:本着知识共享、帮助更多有需求者原则,毫无保留,不另外设置VIP课程。 此外,提供源代码+配套练习+答疑+上课日志。
自定义的
C#
DataGridView下拉列表单元格
在自定义类似dropdownlist类型的单元格时,按照通常的方法,在点击输入的时候显示的是正常的文本,而输入完成后单元格显示的是实际值,并不符合我们的合适习惯. 本例通过在EditingControlFormattedValue中返回
combobox
的
selecteditem
, 在自定义单元格控件时重写GetValue,GetFormattedValue 解决了 显示时显示的是绑定的实际值,而输入选择时显示的是显示的字段的
问题
. 具体实现见附件,也请大家指教
c#
读取odbc数据库,注释详细
包括Odbc数据库dsn的获取,表名的获取.界面代码概览:public Form1() { InitializeComponent(); List
dsnNames = DataSource.GetAllDSN(); //读取DSN for (int i = 0; i < dsnNames.Count; i++) { this.
comboBox
1.Items.Add(dsnNames[i]); } } private void
comboBox
1_SelectedIndexChanged(object sender, EventArgs e) { if (this.
comboBox
1.SelectedIndex == -1) { return; } List
sheetNames = new List
(); try { DataSource.ConnectToDataBase(this.
comboBox
1.
SelectedItem
.ToString(), this.txtUserName.Text, this.txtPassword.Text); sheetNames = DataSource.GetTableAndViewNamesFromDataBase(); } catch (Exception err) { MessageBox.Show(err.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } for (int i = 0; i < sheetNames.Count; i++) { this.
comboBox
2.Items.Add(sheetNames[i]); } } private void
comboBox
2_SelectedIndexChanged(object sender, EventArgs e) { DataTable dt; DataSource.GetTableFromDataBase(this.
comboBox
2.
SelectedItem
.ToString(),out dt); this.dataGridView1.DataSource = dt; }
C#
110,536
社区成员
642,578
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章