textbox control
-
Heyas all, I have project that has a mainform and on it is a read-only textbox control. I have another source file that hold a class I made myself. I'm trying to access the textbox control that is on the mainform from my class file, and I keep getting erros that it's inacceble. The code wizard automatically creates the control as private. I have a method in my class that simply does this:
void vWriteToTextbox(void) { // write to textbox on mainform I'm trying somsthing like... // MainForm.textbox1.Text = "Hello"; }
and like I said it's not working. How can I access the control from my file (which of course is in the same project)? Thanks, John -
Heyas all, I have project that has a mainform and on it is a read-only textbox control. I have another source file that hold a class I made myself. I'm trying to access the textbox control that is on the mainform from my class file, and I keep getting erros that it's inacceble. The code wizard automatically creates the control as private. I have a method in my class that simply does this:
void vWriteToTextbox(void) { // write to textbox on mainform I'm trying somsthing like... // MainForm.textbox1.Text = "Hello"; }
and like I said it's not working. How can I access the control from my file (which of course is in the same project)? Thanks, JohnJohn L. DeVito wrote:
How can I access the control from my file
Why would you want to do this? You can either make the control varialbe "textbox1"
public
, or create a get-function (or property) to return the textbox variable like this:public TextBox box { get { return textbox1; } }
You also need to have a reference to your main form, you can't just doMainForm.something
unless something is declaredstatic
regards -
Heyas all, I have project that has a mainform and on it is a read-only textbox control. I have another source file that hold a class I made myself. I'm trying to access the textbox control that is on the mainform from my class file, and I keep getting erros that it's inacceble. The code wizard automatically creates the control as private. I have a method in my class that simply does this:
void vWriteToTextbox(void) { // write to textbox on mainform I'm trying somsthing like... // MainForm.textbox1.Text = "Hello"; }
and like I said it's not working. How can I access the control from my file (which of course is in the same project)? Thanks, JohnOkay, the easy way to fix this is select the
TextBox
in the form designer and change the value of the Design >> Modifiers property topublic
. While that's the easy way, it breaks the encapsulation principle of OOD. May I suggest creating a property in your main form that exposes the value of theTextBox
? For example, in your main form's class declaration, provide the following declaration.public string MyInterestingTextBoxValue
{
set
{
this.textBox1.Text = value;
}
}Then, you can change your method above to
private void vWriteToTextbox()
{
// Write to textbox on main form
MainForm.MyInterestingTextBoxValue = "Hello";
}"we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty
-
Okay, the easy way to fix this is select the
TextBox
in the form designer and change the value of the Design >> Modifiers property topublic
. While that's the easy way, it breaks the encapsulation principle of OOD. May I suggest creating a property in your main form that exposes the value of theTextBox
? For example, in your main form's class declaration, provide the following declaration.public string MyInterestingTextBoxValue
{
set
{
this.textBox1.Text = value;
}
}Then, you can change your method above to
private void vWriteToTextbox()
{
// Write to textbox on main form
MainForm.MyInterestingTextBoxValue = "Hello";
}"we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty
Thanks Curtis I like the idea of wrapping it in a property. I know I could have changed the access identifier but I really REALLY didn't want to do that. Appreciate the time, works great. Thanks again. :) Thanks, John
-
Thanks Curtis I like the idea of wrapping it in a property. I know I could have changed the access identifier but I really REALLY didn't want to do that. Appreciate the time, works great. Thanks again. :) Thanks, John
John, my pleasure. Hope all turns out well. Happy coding! "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty
-
Okay, the easy way to fix this is select the
TextBox
in the form designer and change the value of the Design >> Modifiers property topublic
. While that's the easy way, it breaks the encapsulation principle of OOD. May I suggest creating a property in your main form that exposes the value of theTextBox
? For example, in your main form's class declaration, provide the following declaration.public string MyInterestingTextBoxValue
{
set
{
this.textBox1.Text = value;
}
}Then, you can change your method above to
private void vWriteToTextbox()
{
// Write to textbox on main form
MainForm.MyInterestingTextBoxValue = "Hello";
}"we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty
Heyas Curtis, I've actually come across a bit of a problem, VS is saying that in order for me to set this property value I must have an instantiated object. I don't understand that. the MainForm is going to be instantied when it runs. Here's my code: In MainForms Class description I have...
internal String txtStatusValue{ set{ this.txtStatus.Text = value;}}
and then in my class file I have..internal void vMyFunction() { MainForm.txtStatusValue = "Blah Blah Blah"; }
I don't get it, could I trouble you for but more assistance? Thanks, John -
Heyas Curtis, I've actually come across a bit of a problem, VS is saying that in order for me to set this property value I must have an instantiated object. I don't understand that. the MainForm is going to be instantied when it runs. Here's my code: In MainForms Class description I have...
internal String txtStatusValue{ set{ this.txtStatus.Text = value;}}
and then in my class file I have..internal void vMyFunction() { MainForm.txtStatusValue = "Blah Blah Blah"; }
I don't get it, could I trouble you for but more assistance? Thanks, Johnsounds like your calling the txtStatusValue before the mainform is instantiated or before the variable you're using to call the mainform propery is initialized. do something like this:
public class MainForm: Form { public static MainForm myMainForm = null; public MainForm() { myMainForm = this; // first line of code in ctor (before InitializeComponent() and before other objects get ctor'd } internal public string txtStatusValue { set { txtStatus.Text = value; } } } public MyClass { public void someFunc() { MainForm.myMainForm.txtStatusValue = "foo"; } }
hope this helps...