1

I have Form1 that has a textbox and a button. When user clicks the button in Form1, Form2 opens up with a label control that carries the value of textbox in Form1.

What i did is set the textbox modifier of Form1 to Public, but when I call the textbox name of Form1 in Form2, I get an error that says

The name "txtbx1" doesn't exist in the current context

I wonder why since I already set the modifier of txtbx1 to Public.

Quick Note: i tried to instantiate Form1 in Form2 as:

Form1 f1 = new Form1();

and then call

f1.txtbx1.text

The odd thing is Form1 could not be instantiated (not highlighting occurs). On the other hand if i do Form2 f2 = new Form2(); Form2 gets highlighted!

This is how i show Form2 from Form1:

        SetSalary salForm = new SetSalary();
        salForm.ShowDialog();

Note that SetSalary represents Form2.

Any help will be appreciated.

Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
user311509
  • 2,856
  • 12
  • 52
  • 69

6 Answers6

4

make a constructor for form2 that accept string and in calling new form2 pass form1.frm1Textbox.text to contructor then set it to form2.frm2Textbox.text

Form2 form = new Form2(frm1Textbox.text);

in form2 constructor

public class Form2 : Form
{
    public Form2(string text)
    {
        frm2Textbox.Text = text; 
    }
}
Amr Badawy
  • 7,453
  • 12
  • 49
  • 84
1
public partial class Form1 : Form    
{ 
    Form2 f = new Form2();
    private void button1_Click(object sender, EventArgs e)
    {   
        textBox1.Text = f.ans();   /*-- calling function ans() and textBox1.Text is inside form1--*/
    }
}

create a public function ans() in form2....

public partial class Form2 : Form
{
    public string ans()
    {
      string s = textBox1.Text;/*-- assigning value of texBox1.Text to local variable s and textBox1.Text is another text box which is inside form 2 --*/
      return s; // returning s
    }
}
Edwin de Koning
  • 14,209
  • 7
  • 56
  • 74
jay
  • 11
  • 2
0

On Form1

public class Form1 : Form
{
 public Form2()
 {
  InitializeComponent();
 }
 public static string MyTextBoxValue;
 protected void button1_Click(object sender, EventArgs e)
 { MyTextBoxValue = TextBox1.Text;
 }
}

On Form2

public class Form2 : Form
{
 public Form2()
 {
  InitializeComponent();
 }
 private void Form2_Load(object sender, EventArgs e)
 {
  label1.Text=Form1.MyTextBoxValue;
 }
}
Vaibhav Bhootna
  • 166
  • 2
  • 15
  • This can get problematic if you're going to have more than one instance of Form1 open. – MCattle Jan 02 '14 at 21:24
  • Can you please elaborate how it will be problematic? – Vaibhav Bhootna Jan 20 '14 at 07:45
  • Since `MyTextBoxValue` is Static, all instances of Form1 will share the same value. If there's only ever one instance of Form1, this wouldn't be a problem, but if there are multiple instances, then `MyTextBoxValue` may inadvertently hold a value from an instance of Form1 other than the one intended. This is slightly mitigated by the fact that the user has to click a button on an instance of Form1 to set the value in the first place, but it still opens the possibility of transferring the incorrect value. – MCattle Jan 20 '14 at 16:12
  • Yes I agree with you. Well thanks for explaining this issue. I will avoid this approach to do such sort of work. – Vaibhav Bhootna Jan 21 '14 at 07:25
0

To instantiate Form1 from Form2, class Form1 must be declared as public class Form1, or at least internal class Form1 if they are in the same assembly (project).

f1.txtbx1.text won't work because c# is case-sensitive and the property is called Text, not text.

Alternatively, you can declare a constructor with a parameter in Form2, so that you don't have to expose the TextBox member as public:

public class Form2 : Form
{
    public Form2(string text)
    {
        txtbx1.Text = text; //txtbx1 does not need to be public
    }
}

Then in Form1 you call var f2 = new Form2("label text goes here");

Jay
  • 56,361
  • 10
  • 99
  • 123
0

Expose a public property (or set of properties, if you have more than one value to pass in) on Form2 which will then fill the textbox. This hides the implementation detail of how it is displayed, if at all, and it also follows the standard used by built-in form classes. Example:

public class SetSalary {
    public SetSalary() { }
    public string SalaryText {
        get { return txtbox1.Text; }
        set { txtbox1.Text = value; }
    }
}

Then, when launching SetSalary, you do this:

SetSalary form = new SetSalary();
form.SalaryText = srcTextBox.Text;
form.ShowDialog();
siride
  • 200,666
  • 4
  • 41
  • 62
0

Good approach is to use Model-View-Presenter pattern. If you are a beginner (I think You are) then You should learn the basics by-the-book. This can help You minimize bugs and bad engineering, and will also maximize Your skill.

Wojtek Turowicz
  • 4,086
  • 3
  • 27
  • 38