以前使用VC++6.0:对话框中的控件一般都可以定义一个变量如int,CString等与控件一一对应,这样在调用对话框的OnOK()函数之后,都可以使用下述方法得到用户通过控件键入的数据,如下:
void SomeFunc()
{
    CDlgMyUI dlgMyUI;
    if (dlgMyUI.DoModal() == IDOK)
    {
        CString strData1 = dlgMyUI.m_strData1;
        int iData2 = dlgMyUI.m_iData2;
    }
}现在使用C#,好像不支持普通变量(如string,int等)与控件的绑定,而只是支持控件与控件变量的绑定(如System.Windows.Form.Control)!
请问各位一般是如何解决此类问题的???

解决方案 »

  1.   

    做属性是可以实现这个功能,但是应该必须在对话框内部执行Close()函数之前手工写代码将用户通过控件键入的数据填入到这个属性里面去,而在VC++中这个工作是由框架自己通过调用UpdateData(ture/false)来完成的,无须人工写代码!
      

  2.   

    另外如果通过属性,是否可以如下所述:
    public string strData1
    {
        get
        {
            return TextBox.Text;// 这里TextBox应该是一个实际的控件变量
        }
    }
    我想这样可能存在问题:
    当我们在对话框外部(即调用
    if(dlgMyUI.ShowDialog() == DialogResult.OK)
    {
        strData1 = dlgMyUI.strData1;
    }
    时,因为dlgMyUI窗口已经被关闭了,属性内部再调用return TextBox.Text会出现问题。
    至少VC++里面是这样的!
      

  3.   

    给你个例子看看你就明白了:
    if( Auditing.AuditingQuery.ShowAuditingQuery( this ,ref SearchID ,ref SearchVal ) )
    {
    //your code
    }public static bool ShowAuditingQuery(IWin32Window Owner ,ref int SearchID ,ref string SearchVal)
    {
    AuditingQuery frm = new AuditingQuery() ; if(frm.ShowDialog(Owner) == DialogResult.OK)
    {
    SearchID = frm.cbb_val.SelectedIndex ;
    SearchVal = frm.txb_val.Text.Trim() ;
    return true;
    }
    else
    return false;
    }
      

  4.   

    没有直接的绑定,参看
    http://blog.csdn.net/knight94/archive/2006/03/18/628285.aspx
      

  5.   

    private void jstzdfile_add_Click(object sender, System.EventArgs e)  //增加新节点jstzdfile_add
    {    Form createbdselectfrm = new Form();
                   createbdselectfrm.ClientSize= new System.Drawing.Size(300,300);
                   createbdselectfrm.Location = new System.Drawing.Point(100,50);    Label  Label1 = new Label();
       Label1.Location= new System.Drawing.Point(24,24);
       Label1.Text = "请输入技术通知单的文件名:";
       Label1.Size = new System.Drawing.Size(168,23);               TextBox TextBox1 = new TextBox();
                   TextBox1.Location= new System.Drawing.Point(80,64);
       TextBox1.Size = new System.Drawing.Size(100,20);
       Button  Button1 = new Button();
                   Button1.Location = new System.Drawing.Point(75,260);
                   Button1.Size = new System.Drawing.Size(100,20);
                   Button1.Text="确定";
                   Button1.DialogResult = System.Windows.Forms.DialogResult.OK;
       createbdselectfrm.Controls.Add(Label1);
       createbdselectfrm.Controls.Add(TextBox1);
       createbdselectfrm.Controls.Add(Button1);
       createbdselectfrm.StartPosition = FormStartPosition.CenterScreen;   DialogResult res = createbdselectfrm.ShowDialog();
    if( res ==System.Windows.Forms.DialogResult.OK && TextBox1.Text!="" ) 
    {                jstzd_bianhao=TextBox1.Text.ToString().Trim().ToLower();
                    //add_nodetext = TextBox1.Text.ToString().Trim().ToLower();
                    nodeatr="技术通知单";
    strnzjswj="技术通知单";
                    strjdhswj="0";
                    createbdselectfrm.Close(); DataTable tempTable = new DataTable(); this.strSQL = "select jstzd_bianhao from jstzd where jstzd_bianhao = '" + jstzd_bianhao + "'";
    this.da = new SqlDataAdapter(this.strSQL,this.tempConnection);
    this.da.Fill(tempTable);//查询获得机构的单位名称,并填写到文本框中
    if (tempTable.Rows.Count==0)
    {
    AddRecord(false,"jstzd");
                            
             new_open_del_ren = "jstzd_new"; jstzd jstzdMDIChild = new jstzd();
    jstzdMDIChild.MdiParent = this;
    jstzdMDIChild.Show();
    }
    else
                            MessageBox.Show ("库中已存在");
    }  }
    以上是代码动态弹出窗口,获取输入数据的例子。
      

  6.   

    主窗口:
            public string str;
            private void button1_Click(object sender, EventArgs e)
            {
                Form2 a = new Form2(this);
                a.ShowDialog();
                MessageBox.Show(str);
            }对话框窗口:
            public Form2(Form1 form)
            {
                InitializeComponent();
                form1 = form;
            }
            private Form1 form1;
            private void button1_Click(object sender, EventArgs e)
            {
                form1.str = textBox1.Text;
                this.Close();
            }