none
为什么有时候读某个类下的变量值时,得到的结果却是该变量所在的命名空间的呢?老师帮看看 RRS feed

  • 问题

  • 为什么有时候读某个类下的变量值时,得到的结果却是该变量所在的命名空间的呢?老师帮看看

    代码:
    string ss=combobox1.Text;

    其中combobox1的选项已绑定到数据表,
    可为什么最后ss的值为"Prj_2009.Comm.cmb绑定",而不是combobox1文本框中的内容呢?实在搞不清楚....

    Prj_2009.Comm
    ...
    public void cmb绑定(combobox cmb,DataTable dt)
    {
                cmb.DataSource = dt
                cmb.DisplayMember = "名称";
                cmb.ValueMember = "代码";
    }

    2009年6月13日 10:28

答案

  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace X.WinFormsApp
    {
        public partial class X200905132241 : Form
        {
            private ComboBox comboBox1;
            private Button btnShowMessage;
    
            #region " Employee "
    
            private DataTable fEmployee;
            public DataTable Employee
            {
                get
                {
                    if (fEmployee == null)
                    {
                        fEmployee = new DataTable();
    
                        fEmployee.Columns.Add(new DataColumn("EmployeeKey", typeof(Int32)));
                        fEmployee.Columns.Add(new DataColumn("Name", typeof(String)));
                        fEmployee.Columns.Add(new DataColumn("ModifyDate", typeof(DateTime)));
    
                        fEmployee.Rows.Add(new object[] { 1, "John", DateTime.Now });
                        fEmployee.Rows.Add(new object[] { 2, "Tomi", DateTime.Now });
                        fEmployee.Rows.Add(new object[] { 3, "X.XY", DateTime.Now });
                        fEmployee.Rows.Add(new object[] { 4, "Sam", DateTime.Now });
                        fEmployee.Rows.Add(new object[] { 5, "Mary", DateTime.Now });
                        fEmployee.Rows.Add(new object[] { 6, "Chirs", DateTime.Now });
                    }
    
                    return fEmployee;
                }
            }
    
            #endregion
    
            public X200905132241()
            {
                InitializeComponent();
                this.InitClass();
            }
    
            private void InitClass()
            {
                this.comboBox1 = new ComboBox();
                this.btnShowMessage = new Button();
    
                this.comboBox1.DataSource = this.Employee;
                this.comboBox1.DisplayMember = "Name";
                this.comboBox1.ValueMember = "EmployeeKey";
                
                this.comboBox1.Location = new System.Drawing.Point(67, 72);
                this.comboBox1.Size = new System.Drawing.Size(156, 20);
                
                this.btnShowMessage.Location = new System.Drawing.Point(67, 110);
                this.btnShowMessage.Size = new System.Drawing.Size(75, 23);
                this.btnShowMessage.Text = "ShowMessage";
                this.btnShowMessage.Click += new EventHandler(ShowMessage_Click);
                
                this.Controls.Add(this.btnShowMessage);
                this.Controls.Add(this.comboBox1);
            }
    
            private void ShowMessage_Click(object sender, EventArgs e)
            {
                // 将选定项转化为对应的类型然后再取你需要的值
                DataRowView fDataRowView = (DataRowView)this.comboBox1.SelectedItem;
    
                string fEmployeeKey = fDataRowView["EmployeeKey"].ToString();
                string fName = fDataRowView["Name"].ToString();
                string fModifyDate = fDataRowView["ModifyDate"].ToString();
    
                MessageBox.Show(fEmployeeKey);
                MessageBox.Show(fName);
                MessageBox.Show(fModifyDate);
            }
        }
    }
    
    

    知识改变命运,奋斗成就人生!
    2009年6月13日 15:11
    版主

全部回复

  • 你好 Text属性是指,获取或设置与此控件关联的文本 如果你没有设定 默认的就是这个类的FullName 如果你是想获取ComboBox的选定项的值 可以采用SelectedValue来读取
    Denn ich gehor nur dir!坚持不懈!http://hi.baidu.com/1987raymond
    2009年6月13日 10:57
    版主
  • (同一段代码里有些combobox控件不出现题目所说的情况,不是全部)

    SelectedValue值不是想要的,
    如果SelectedValue的值为1,则对应的文本值为"一级医院"
    我是想要文本值,该怎么读出来呢
    2009年6月13日 11:15
  • 你好!
         可以使用反射来获取,方法我在里的另外一个帖子里已经给你解释了啊!
    周雪峰
    2009年6月13日 12:02
    版主
  • (同一段代码里有些combobox控件不出现题目所说的情况,不是全部)

    SelectedValue值不是想要的,
    如果SelectedValue的值为1,则对应的文本值为"一级医院"
    我是想要文本值,该怎么读出来呢

    hi,
        应该是使用ComboBox1.Text来获取控件显示的符串。ComboBox1.SelectedText获取选择项的字符。
    你使用后面的属性试验一下。~

    Frank.Xu Lei--谦卑若愚,好学若饥
    专注于.NET平台下分布式应用系统开发和企业应用系统集成
    Focus on Distributed Applications Development and EAI based on .NET
    老徐的博客:http://www.cnblogs.com/frank_xl
    2009年6月13日 14:18
    版主
  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace X.WinFormsApp
    {
        public partial class X200905132241 : Form
        {
            private ComboBox comboBox1;
            private Button btnShowMessage;
    
            #region " Employee "
    
            private DataTable fEmployee;
            public DataTable Employee
            {
                get
                {
                    if (fEmployee == null)
                    {
                        fEmployee = new DataTable();
    
                        fEmployee.Columns.Add(new DataColumn("EmployeeKey", typeof(Int32)));
                        fEmployee.Columns.Add(new DataColumn("Name", typeof(String)));
                        fEmployee.Columns.Add(new DataColumn("ModifyDate", typeof(DateTime)));
    
                        fEmployee.Rows.Add(new object[] { 1, "John", DateTime.Now });
                        fEmployee.Rows.Add(new object[] { 2, "Tomi", DateTime.Now });
                        fEmployee.Rows.Add(new object[] { 3, "X.XY", DateTime.Now });
                        fEmployee.Rows.Add(new object[] { 4, "Sam", DateTime.Now });
                        fEmployee.Rows.Add(new object[] { 5, "Mary", DateTime.Now });
                        fEmployee.Rows.Add(new object[] { 6, "Chirs", DateTime.Now });
                    }
    
                    return fEmployee;
                }
            }
    
            #endregion
    
            public X200905132241()
            {
                InitializeComponent();
                this.InitClass();
            }
    
            private void InitClass()
            {
                this.comboBox1 = new ComboBox();
                this.btnShowMessage = new Button();
    
                this.comboBox1.DataSource = this.Employee;
                this.comboBox1.DisplayMember = "Name";
                this.comboBox1.ValueMember = "EmployeeKey";
                
                this.comboBox1.Location = new System.Drawing.Point(67, 72);
                this.comboBox1.Size = new System.Drawing.Size(156, 20);
                
                this.btnShowMessage.Location = new System.Drawing.Point(67, 110);
                this.btnShowMessage.Size = new System.Drawing.Size(75, 23);
                this.btnShowMessage.Text = "ShowMessage";
                this.btnShowMessage.Click += new EventHandler(ShowMessage_Click);
                
                this.Controls.Add(this.btnShowMessage);
                this.Controls.Add(this.comboBox1);
            }
    
            private void ShowMessage_Click(object sender, EventArgs e)
            {
                // 将选定项转化为对应的类型然后再取你需要的值
                DataRowView fDataRowView = (DataRowView)this.comboBox1.SelectedItem;
    
                string fEmployeeKey = fDataRowView["EmployeeKey"].ToString();
                string fName = fDataRowView["Name"].ToString();
                string fModifyDate = fDataRowView["ModifyDate"].ToString();
    
                MessageBox.Show(fEmployeeKey);
                MessageBox.Show(fName);
                MessageBox.Show(fModifyDate);
            }
        }
    }
    
    

    知识改变命运,奋斗成就人生!
    2009年6月13日 15:11
    版主