• 0

[C#] Noob question about loading a variable...


Question

Im in Form1, i have a variable loaded on a textbox on Form2 when i try to do :

  if (this.comboBox1.Text == "X")

    this.textBox1 *= Form2.textBox2.Text;

i get "Form2.textBox2.Text is not accesible because protection level"

er... help! :D

Link to comment
Share on other sites

12 answers to this question

Recommended Posts

  • 0

Controls on a form are created as private by default in C#, so you can't access one form's controls from another form or from another class without changing the access level. For example, textBox2 is probably declared like this in Form2:

private TextBox textBox2 = new TextBox();

If you want this to be accessible from outside of Form2, you can either change "private" to "public" (not very good object-orientedness) or create a property in Form 2 that will return the value of the textbox.

Also, rundkaas is right. You can't multiply strings. You want one of these:

double dResult = Convert.ToDouble(textBox1.Text) * Convert.ToDouble(Form2.textBox2.Text);
textBox1.Text = dResult.ToString();

OR

int nResult = Convert.ToInt32(textBox1.Text) * Convert.ToInt32(Form2.textBox2.Text);
textBox1.Text = nResult.ToString();

Link to comment
Share on other sites

  • 0
Controls on a form are created as private by default in C#, so you can't access one form's controls from another form or from another class without changing the access level. For example, textBox2 is probably declared like this in Form2:

Dannysmurf, you mean that controls are created as protected by default, but protected is not enugh to access it from another form, right?

Link to comment
Share on other sites

  • 0
Dannysmurf, you mean that controls are created as protected by default, but protected is not enugh to access it from another form, right?

Ah yes, they're protected, not private. But when trying to access the variables from another class, the effect is the same. Protected members can be accessed only from derived classes. Otherwise, they function the same as private members.

Link to comment
Share on other sites

  • 0

Put this in your Form2 code:

CODE

public string TextBoxData

{

  get

  {

      return TextBox2.Text;

  }

}

then i can do this ?? on form 1 and will work :? i get something like" a reference to object is needed for the field, mehif or property not static" <-- im transltaing this so not sure if its the exact error

  if (this.comboBox1.Text == "X")

  {

    double dResult = Convert.ToDouble(textBox1.Text) * Convert.ToDouble(Form2.TextBoxData);

    textBox1.Text = dResult.ToString();

  }

tom, what u mean with internal :?

Link to comment
Share on other sites

  • 0
tom, what u mean with internal :?

As dannysmurf said, the textbox in Form2 is probably declared as such:

private TextBox textBox2 = new TextBox();

Simply change the word "private" to "internal" and that object (textBox2) will be available throughout the entire assembly (your exe or dll file).

internal TextBox textBox2 = new TextBox();

Link to comment
Share on other sites

  • 0

wow thx :D

one littl equestion why internal and not public? what ios the diference?

and... how to do :

public string TextBoxData

{

  get

  {

      return TextBox2.Text;

  }

}

but with 16 textboxs... someone suggested me to use an array but i dont know where exactly to put the string[] array = new string[] { textBox1.Text,textBox2.Text........};
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.