将值从一个表单中的一个文本框传递到另一个表单中的另一个文本框 [英] to pass value from one textbox in one form to another textbox in another form

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

问题描述

大家好....请帮帮我...我遇到这个问题....



有2种表格。

在一种形式(form1)中,有3个文本框(tb1,tb2,tb3),每个文本框对应一个按钮。对于tb1-btn1,tb2-btn2和tb3-btn3

在第二种形式(form2)中,有一个文本框(tb4)和一个按钮(btn4)...

当我点击form1中的按钮(比如说btn1)时,form2应该显示。当在文本框中输入一些文本(tb4)并单击按钮(btn4)时,该文本应显示在form1中的文本框(tb1)中,该文本框对应于首先单击的按钮(btn1).... ...就像btn2和btn3那样....问题是我输入的tb4显示在所有3个文本框中(tb1,tb2,tb3)......

我不想要它像dat ....我想要的是......当点击btn1时,必须仅在tb1中显示值,类似地,当点击btn2时,必须在tb2 ....中显示,因此对于btn3 ...

这是在C#.net Windows应用程序

这是代码r .......





hi all....please do help me...am stuck with this problem....

There are 2 forms.
In one form(form1),there are 3 textboxes(tb1,tb2,tb3) and corresponding to each there is a button..i.e: for tb1-btn1,tb2-btn2,and tb3-btn3
In second form(form2),there is one textbox(tb4) and one button(btn4)...
When i click on a button(say btn1)in form1,form2 should be displayed.and when some text is entered in textbox(tb4) and button(btn4) is clicked that text should be displayed in the textbox(tb1) in form1 which is corresponding to the button(btn1) clicked first.......like that for btn2 and btn3....The prob is what i enter in tb4 is displaying in all 3 textboxes(tb1,tb2,tb3)...
I dont want it like dat....What i want is....When btn1 is clicked value must be displayed only in tb1,similarly when btn2 is clicked value must be shown in tb2....and so for btn3...
This is in C#.net Windows Application
Here is the code i tried......


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

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.Show();

        }

        internal void valuefromform2(string p)
        {
            textBox1.Text = p;
        }
}
}










public partial class Form2 : Form
    {
        Form1 f1;
        public Form2()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (f1 == null)
                f1 = new Form1();
            f1.valuefromform2(textBox1.Text);
            f1.Show();
        }


    }







pls帮助我......我被困在这里...没有这个我不能在我的项目中继续...请帮助...




pls do help me....am stuck here...without this i cant go further in my project...pls pls...

推荐答案

humm我会说你做了世界上最简单的任务的复杂方法之一。



你听说过C#中的属性吗? ??

如果是我的话。我将Text2中的TextBox作为Get only属性公开,并使用该对象在我的父表单中更新。

例如:



humm I would say you have done one of the complex way to doing a simplest task in the world.

Have you heard about Properties in C#. ??
If it was me. I would expose the TextBox from the Form2 as a Get only property and use that object to update in my parent form.
eg:

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

   private void button1_Click(object sender, EventArgs e)
   {
      Form2 f2 = new Form2();
      if( f2.ShowDialog() == DialogResult.OK)
      {
        textBox1.text = f2.TxtBox.Text;
      }
    }   
}

public partial class Form2 : Form
{
    public TextBox TxtBox
    {
      get {return textBox1;}
    }
    public Form2()
    {
       InitializeComponent();
    }
}


您好,



更改 Form2 上课:

Hi,

Change the Form2 class into this:
public partial class Form2 : Form
    {
        Form1 f1 = null;
        public Form2(Form1 _form1)
        {
            InitializeComponent();
            f1 = _form1;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (f1 == null)
                f1 = new Form1();
            f1.valuefromform2(textBox1.Text);
            f1.Show();
        }
 

    }



并更改 button1_Click 功能如下:


And change the button1_Click function into this:

private void button1_Click(object sender, EventArgs e)
       {
           Form2 f2 = new Form2(this);
           f2.Show();

       }



希望这会有所帮助。


Hope this helps.


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



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



另请参阅:

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

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



-SA
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


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

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