在C#中从另一个表单访问文本框的值 [英] To access value of a textbox from another form in C#

查看:47
本文介绍了在C#中从另一个表单访问文本框的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有两种形式:Form1和sqlDBCompare。

我想访问Form1中3个文本框的值。



下面我定义了get和set属性。

Form1.cs代码:

  public   partial   class  Form1:表格
{
public string TextDB1
{
get { return txtMasterDB.Text; }
set {txtMasterDB.Text = value ;

}
}

public string TextDB2
{
get { return txtSlaveDB.Text; }
set {txtSlaveDB.Text = value ; }
}

public string TextDB3
{
get { return txtThirdDB.Text; }
set {txtThirdDB.Text = value ; }
}


public Form1()
{
InitializeComponent();
}

private void button1_Click( object sender,EventArgs e)
{
SqlDbCompare sqlDB = new SqlDbCompare();
sqlDB.Show();
}
}





sqlDBCompare.cs代码



private void button1_Click(object sender,EventArgs e)

{

Form1 form = new Form1();

string db1 = form .TextDB1;

string db2 = form.TextDB2;

string db3 = form.TextDB3;

MessageBox.Show(db1的值是:+ db1);



试试

{



DataSet ds0 = GetDB1(db1);

DataSet ds1 = GetDB1(db2);

DataSet ds2 = GetDB1(db3);

}

}



在db1,db2和db3中我没有得到值。



请让我知道我哪里错了。



谢谢,

解决方案

你实际上是正确的值。只需查看代码:您只需创建一个表单,然后从三个文本框中获取三个值。形式上,这是正确的,只是它根本没有实际意义,因为这与得到一些默认值相同。您没有让用户有机会编辑这些值。



要让用户进行更改以编辑值,您不能使用堆栈变量表单,因为它会绑定你一遍又一遍地创建一个新实例。你真的需要创建一个寿命更长的实例。另一种选择是:创建一个表单实例,并使用 form.ShowDialog()将其显示为模式对话框。 这是你的问题的答案我哪里出错?。



这是关于表单协作的热门问题。最强大的解决方案是在表单类中实现适当的接口,并传递接口引用而不是引用Form的整个实例。有关更多详细信息,请参阅我以前的解决方案:如何以两种形式复制列表框之间的所有项目 [ ^ ]。



另请参阅此处的其他解决方案讨论。如果应用程序足够简单,解决方案就像在一个表单中声明一些 internal 属性并将对一个表单的实例的引用传递给另一个表单的实例一样简单形成。对于更复杂的项目,这种违反严格封装的样式和松散耦合可能会增加代码的意外复杂性并引发错误,因此封装良好的解决方案将是优惠。



另请参阅:

http://en.wikipedia.org/wiki/Accidental_complexity [ ^ ],

http://en.wikipedia.org/wiki/Loose_coupling [ ^ ]。



-SA


您的sqlDBCompare.cs表单不应该创建 Form1的实例!它需要动态访问到Form1的实例中的TextBox控件的Text内容(在本例中,显然是您的主窗体)创建它。



因此,在sqlDBCompare.cs的构造函数中:

  public   partial   class  Form1:Form 
{
public sqlDBCompare()
{
InitializeComponent();
}

// Form1实例上的TextBoxes
public TextBox TextDB1 { get ; set ; }
public TextBox TextDB2 { get ; set ; }
public TextBox TextDB3 { get ; set ; }

// 其他Form1字段,方法等
}

并且,在Form1中:

  //  在表单中范围 
SqlDbCompare sqlDB = new SqlDbCompare();

private void Form1_Load( object sender,EventArgs e)
{
sqlDB.TextDB1 = this .TextDB1;
sqlDB.TextDB2 = this .TextDB2;
sqlDB.TextDB3 = this .TextDB3;
}

现在,在Form sqlDB中,如果需要访问Form1的TextBoxes的Text内容,可以这样做:

 私有  void  button1_Click( object  sender,EventArgs e)
{
// 假设sqlDB可见

string db1 = TextDB1.Text;
string db2 = TextDB2.Text;
string db3 = TextDB3.Text;

MessageBox.Show( db1的值为: + db1) ;

try
{
DataSet ds0 = GetDB1(db1);
DataSet ds1 = GetDB1(db2);
DataSet ds2 = GetDB1(db3);
}
catch // [1]
{
throw new DataException( db access problem);
}
}

[1]注意'try块必须有'catch或'finally块。



我们在这里做的是注入:我们已经将主窗体Form1实例中的TextBox控件的有效引用放入sqlDBCompare窗体的实例中。



我们可以使用其他技术来允许sqlDBCompare Form访问Form1的TextBoxes:例如,我们可以将Form1上的方法的引用注入到sqlDBCompare中。


< blockquote>我不知道天气是否合适。但是你可以用它完成这样的事情。

尝试使用参数化构造函数为第二个形式&将相关参数传递给它。



  public   partial   class  Form1:Form 
{
public string TextDB1
{
get {返回 txtMasterDB.Text; }
set {txtMasterDB.Text = value ;

}
}

public string TextDB2
{
get { return txtSlaveDB.Text; }
set {txtSlaveDB.Text = value ; }
}

public string TextDB3
{
get { return txtThirdDB.Text; }
set {txtThirdDB.Text = value ; }
}


public Form1()
{
InitializeComponent();
}

private void button1_Click( object sender,EventArgs e)
{
// 你也可以传递一个对象。
SqlDbCompare sqlDB = new SqlDbCompare(TextDB1,TextDB2,TextDB3);

sqlDB.Show();
}
}


Hi,

I have 2 forms: Form1 and sqlDBCompare.
I want to access value of 3 textboxes which are in Form1.

Below i have get and set property defined.
Form1.cs code:

public partial class Form1 : Form
    {
        public string TextDB1
        {
            get { return txtMasterDB.Text; }
            set { txtMasterDB.Text = value;

            }
        }

        public string TextDB2
        {
            get { return txtSlaveDB.Text; }
            set { txtSlaveDB.Text = value; }
        }

        public string TextDB3
        {
            get { return txtThirdDB.Text; }
            set { txtThirdDB.Text = value; }
        }


        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlDbCompare sqlDB = new SqlDbCompare();
            sqlDB.Show();
        }
}



sqlDBCompare.cs code

private void button1_Click(object sender, EventArgs e)
{
Form1 form = new Form1();
string db1 = form.TextDB1;
string db2 = form.TextDB2;
string db3 = form.TextDB3;
MessageBox.Show("Value of db1 is:" + db1);

try
{

DataSet ds0 = GetDB1(db1);
DataSet ds1 = GetDB1(db2);
DataSet ds2 = GetDB1(db3);
}
}

In db1,db2 and db3 i am not getting values.

Please let me know where i am going wrong.

Thanks,

解决方案

You are actually getting right values. Just look at your code: you just create a form, and then you get three values from three text boxes. Formally, this is correct, only it makes no practical sense at all, as this is the same as to get some default values. You don't get the user a chance to edit these values.

To get user a change to edit the values, you cannot use a stack variable form at all, because it would bind you to creating a new instance over and over. You really need to create an instance which lives longer. Another option is this: create a form instance and show it as a modal dialog using form.ShowDialog(). This is the answer to your question "where am I going wrong?".

This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA


Your sqlDBCompare.cs Form should not create an instance of Form1 ! It needs dynamic access to the Text content of the TextBox Controls in the instance of Form1 (in this case, apparently, your main Form) that created it.

So, in your constructor for sqlDBCompare.cs:

public partial class Form1 : Form
{
    public sqlDBCompare()
    {
        InitializeComponent();
    }

    // the TextBoxes on the instance of Form1
    public TextBox TextDB1 { get; set; }
    public TextBox TextDB2 { get; set; }
    public TextBox TextDB3 { get; set; }

    // other Form1 Field, Methods, etc.
}

And, in Form1:

// in Form scope
SqlDbCompare sqlDB = new SqlDbCompare();

private void Form1_Load(object sender, EventArgs e)
{
  sqlDB.TextDB1 = this.TextDB1;
  sqlDB.TextDB2 = this.TextDB2;
  sqlDB.TextDB3 = this.TextDB3;
}

Now, in Form sqlDB, if you need access to the Text content of Form1's TextBoxes, you can do this:

private void button1_Click(object sender, EventArgs e)
{
    // assume sqlDB is visible

    string db1 = TextDB1.Text;
    string db2 = TextDB2.Text;
    string db3 = TextDB3.Text;

    MessageBox.Show("Value of db1 is:" + db1);
    
    try
    {
        DataSet ds0 = GetDB1(db1);
        DataSet ds1 = GetDB1(db2);
        DataSet ds2 = GetDB1(db3);
    }
    catch // [1]
    {
        throw new DataException("db access problem");
    }
}

[1] Note that a 'try block must have a 'catch or 'finally block.

What we've done here is "injection:" we have put valid references to the TextBox controls in the instance of the main form, Form1, into the instance of the sqlDBCompare Form.

There are other techniques we could have used to allow the sqlDBCompare Form access to Form1's TextBoxes: we could have injected a reference to a method on Form1 into sqlDBCompare, for example.


I don't know weather it is proper way or not.but you can done with it some thing like this..
Try to use parameterised constructor for 2nd one form & pass related parameters to that.

 public partial class Form1 : Form
    {
        public string TextDB1
        {
            get { return txtMasterDB.Text; }
            set { txtMasterDB.Text = value;
 
            }
        }
 
        public string TextDB2
        {
            get { return txtSlaveDB.Text; }
            set { txtSlaveDB.Text = value; }
        }
 
        public string TextDB3
        {
            get { return txtThirdDB.Text; }
            set { txtThirdDB.Text = value; }
        }
 

        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            // you can pass an object also .
            SqlDbCompare sqlDB = new SqlDbCompare(TextDB1,TextDB2,TextDB3);
            
          sqlDB.Show();
        }
}


这篇关于在C#中从另一个表单访问文本框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆