BlueRage Posted September 6, 2004 Share Posted September 6, 2004 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 More sharing options...
0 rundkaas Posted September 6, 2004 Share Posted September 6, 2004 why are you using '*=' opperator, isn't that for multeplying numbers Link to comment Share on other sites More sharing options...
0 smurfiness Posted September 6, 2004 Share Posted September 6, 2004 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 More sharing options...
0 rundkaas Posted September 6, 2004 Share Posted September 6, 2004 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 More sharing options...
0 smurfiness Posted September 6, 2004 Share Posted September 6, 2004 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 More sharing options...
0 BlueRage Posted September 6, 2004 Author Share Posted September 6, 2004 can i have a little example of "create a property in Form 2 that will return the value of the textbox" ? thx :D Link to comment Share on other sites More sharing options...
0 John Veteran Posted September 6, 2004 Veteran Share Posted September 6, 2004 Put this in your Form2 code: public string TextBoxData { get { return TextBox2.Text; } } Link to comment Share on other sites More sharing options...
0 Glowstick Posted September 6, 2004 Share Posted September 6, 2004 Or just declare it as internal. Link to comment Share on other sites More sharing options...
0 BlueRage Posted September 6, 2004 Author Share Posted September 6, 2004 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 More sharing options...
0 John Veteran Posted September 7, 2004 Veteran Share Posted September 7, 2004 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 More sharing options...
0 BlueRage Posted September 8, 2004 Author Share Posted September 8, 2004 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 More sharing options...
0 John Veteran Posted September 8, 2004 Veteran Share Posted September 8, 2004 Making it public would break encapsulation. Link to comment Share on other sites More sharing options...
0 RichMercer Posted September 8, 2004 Share Posted September 8, 2004 Internal makes the object accesible to all classes within the same assembley. Public goes beyond that and makes it accesible to anything. Link to comment Share on other sites More sharing options...
Question
BlueRage
Im in Form1, i have a variable loaded on a textbox on Form2 when i try to do :
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