关于listbox和combobox

Black__Sabbath 2016-02-15 03:48:47
各位大大,能仔细说下selectedvalue selectedtext和selecteditem的区别及用法吗?每次都是用的时候不知道选哪个~经常取不到 值
...全文
175 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Black__Sabbath 2016-02-16
  • 打赏
  • 举报
回复
谢谢大家 ,弄明白了
  • 打赏
  • 举报
回复
listbox 是多行的布局,而 comboBox 是单行的布局。先把静态图像记住了,你就知道基本区别了。
crystal_lz 2016-02-15
  • 打赏
  • 举报
回复
试一下不就知道了

public class KeyValue {
    private string _Key;

    public string Key {
        get { return _Key; }
        set { _Key = value; }
    }

    private string _Value;

    public string Value {
        get { return _Value; }
        set { _Value = value; }
    }

    public override string ToString() {
        return string.Format("Key:{0} - Value:{1}", this._Key, this._Value);
    }
}

private void Form3_Load(object sender, EventArgs e) {
    treeView1.DrawMode = TreeViewDrawMode.OwnerDrawAll;
    treeView1.ShowLines = false;//去掉虚线
    //方式一
    //comboBox1.Items.Add("123");
    //comboBox1.Items.Add("456");
    //方式二
    //comboBox1.Items.Add(new KeyValue() { Key = "key_1", Value = "value_1" });
    //comboBox1.Items.Add(new KeyValue() { Key = "key_2", Value = "value_2" });
    //comboBox1.DisplayMember = "Key";
    //comboBox1.ValueMember = "Value";//这里指定也没有用
    //方式三
    //DataTable dt = new DataTable();
    //dt.Columns.Add("key");
    //dt.Columns.Add("Value");
    //dt.Rows.Add("dt_key_1", "dt_value_1");
    //dt.Rows.Add("dt_key_2", "dt_value_2");
    //comboBox1.DataSource = dt;
    //comboBox1.DisplayMember = "Key";
    //comboBox1.ValueMember = "Value";
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
    string strShow = "SelectedText:" + comboBox1.SelectedText;
    strShow += "\r\nSelectedValue:" + comboBox1.SelectedValue;
    strShow += "\r\nSelectedItem:" + comboBox1.SelectedItem.ToString();
    MessageBox.Show(strShow);
}
不过SelectedText 我也不知道是搞啥的 重来没有用过 貌似都是null 一般都直接用text属性

111,129

社区成员

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

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

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