C#随笔1

欢迎光临http://www.5xiuxiu.cn/  我秀秀购物导航 (关键字:淘宝网购物,淘宝购物导航,淘宝导航,淘宝皇冠,淘宝皇冠店,我秀秀,5xiuxiu,最专业的导航购物网,卓越购物导航,卓越导航,凡客导航,凡客购物导航,当当购物导航,淘宝钻石店,淘宝钻石,淘宝皇冠,购物搜索,比较搜索)

1,判断一个文件是否存在:

     System.IO.File.Exists("E:\\ServerIP-B.xml")) 

2,绑定对象到combox组件中,有两种方法:

    a,将绑定的对象的toString方法复写,如下所示,然后调用combox.items.add(category),则combox显示的是category的toString方法的返回值,从而实现绑定;

   

Code

 

b,

C#的winform中,我们都发现comboBox没有一个能绑定内容的容器,而此外的控件都有一个tag属性用来绑定信息,也没有仔细查过,vs为什么这么设计,不过有一种方法能够解决这个问题。
(1)我们创建一个ComboBoxItem类:
public class ComboBoxItem
    {
        private string _text = null;
        private object _value = null;
        public string Text { get { return this._text; } set { this._text = value; } }
        public object Value { get { return this._value; } set { this._value = value; } }
        public override string ToString()
        {
            return this._text;
        }
    }
(2)在添加的时间,我们将这个类的列表添加到comboBox的item属性中:
 private void Form1_Load(object sender, EventArgs e)
        {
            ComboBoxItem cbi1 = new ComboBoxItem();
            cbi1.Text = "测试Test1";
            cbi1.Value = "测试Value1";
            comboBox1.Items.Add(cbi1);
            ComboBoxItem cbi2 = new ComboBoxItem();
            cbi2.Text = "测试Test2";
            cbi2.Value = "测试Value2";
            comboBox1.Items.Add(cbi2);
        }
 
(3)在我们使用的时候:
 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.Items != null && comboBox1.Items.Count > 0 && comboBox1.SelectedItem != null)
            {
                string text = ((ComboBoxItem)comboBox1.SelectedItem).Text;
                object vlaue = ((ComboBoxItem)comboBox1.SelectedItem).Value;
            }
        }
其中test是表面要显示的文档,而vlaue则是我们绑定的object的数据,给大家一个好玩的想法。

 

欢迎光临http://www.5xiuxiu.cn/  我秀秀购物导航 (关键字:淘宝网购物,淘宝购物导航,淘宝导航,淘宝皇冠,淘宝皇冠店,我秀秀,5xiuxiu,最专业的导航购物网,卓越购物导航,卓越导航,凡客导航,凡客购物导航,当当购物导航,淘宝钻石店,淘宝钻石,淘宝皇冠,购物搜索,比较搜索)

posted @ 2009-07-11 13:01  tonywangzg  阅读(205)  评论(0)    收藏  举报