How to refer to a control from inside a class?
-
Using VB.NET I have defined a class (the first of many to come if it works). Inside the class I have Public Function Init() Inside the Init funtion I would like to change the caption of some of the objects on the form. Likewise, I would like to fill a combo box with class specific information. What I can't figure out is how do I refer to an object on the form in order to change its caption, or fill the contents of its combo box? In my example I have a button called Test1. If I add inside the Init() function Test1.Text="My test", it doesn't know what to do with it. Since I could do this in VB6, I'm sure there must be a way to do it. I just don't know the proper method. Can anyone tell me the secret? Thanks, Ilan
-
Using VB.NET I have defined a class (the first of many to come if it works). Inside the class I have Public Function Init() Inside the Init funtion I would like to change the caption of some of the objects on the form. Likewise, I would like to fill a combo box with class specific information. What I can't figure out is how do I refer to an object on the form in order to change its caption, or fill the contents of its combo box? In my example I have a button called Test1. If I add inside the Init() function Test1.Text="My test", it doesn't know what to do with it. Since I could do this in VB6, I'm sure there must be a way to do it. I just don't know the proper method. Can anyone tell me the secret? Thanks, Ilan
The controls are members of the form class, you need a reference to the instance of the form to reach the controls in it. You can either send along a reference to the form, or a reference to the specific control, to the method. I would suggest the later, so that the method doesn't have to be bothered with the name of the control.
--- b { font-weight: normal; }
-
The controls are members of the form class, you need a reference to the instance of the form to reach the controls in it. You can either send along a reference to the form, or a reference to the specific control, to the method. I would suggest the later, so that the method doesn't have to be bothered with the name of the control.
--- b { font-weight: normal; }
Thanks for the help, but I'm still too new to understand exactly what you are saying. If I write "Form1" it comes up with a list of suggestions like ActiveForm etc. which don't look like they will help. If I type Form1.Test1.Text="my Test", it doesn't know what I'm talking about. How exactly do I get the reference to the instance of the form? Is there something like GetParent()? This is probably trivial to you, but I'm missing the link. Thanks, Ilan
-
The controls are members of the form class, you need a reference to the instance of the form to reach the controls in it. You can either send along a reference to the form, or a reference to the specific control, to the method. I would suggest the later, so that the method doesn't have to be bothered with the name of the control.
--- b { font-weight: normal; }
You suggest to send along a reference to the form. How do I do this? In the form where the class is called do I do something like myScope.Init(this) where Init is defined something like Public Function Init(CWnd* wnd1) (I know the syntax must be wrong but I'm used to c++.) Preferably I'd like the class to know something about Form1 without my having to explicitly send something, but if I must send it explicity, please give me the exact syntax in order to do so. Thanks, Ilan
-
You suggest to send along a reference to the form. How do I do this? In the form where the class is called do I do something like myScope.Init(this) where Init is defined something like Public Function Init(CWnd* wnd1) (I know the syntax must be wrong but I'm used to c++.) Preferably I'd like the class to know something about Form1 without my having to explicitly send something, but if I must send it explicity, please give me the exact syntax in order to do so. Thanks, Ilan
You don't have to specify when you use pointers/references in C#. All objects are always handled using references. You should specify the data type of the return value from the function, though.
Public Function Init(Form1 mainForm) As String
Now you can call it from Form1:result = myScope.Init(this)
--- b { font-weight: normal; }
-
You don't have to specify when you use pointers/references in C#. All objects are always handled using references. You should specify the data type of the return value from the function, though.
Public Function Init(Form1 mainForm) As String
Now you can call it from Form1:result = myScope.Init(this)
--- b { font-weight: normal; }
Thanks for your help. I think you got slightly confused as to who changes who. I want the function to change the form. In any case your help was enough to get me going. It doesn't recognize 'this', but on the other hand it knows what 'Me' is. Likewise it doesn't know what 'Form1 mainForm' is but it does know what 'ByRef mainForm As Form1' is. As far a return values, I return a simple status value and apparently it has no troubles with integer return values. Something more complicated like a string would most likely demand your syntax. In any case I used: Public Function Init(ByRef mainForm As Form1) Dim stat As Integer = DS_SUCCESS mainForm.Test1.Text = "my Test" and I called it by stat = myScope.Init(Me) and it finally changed the button text. (Not that I really want to change that button's text, but I do want to know how to change other things on demand.) Do you know if I could save the value of mainForm in my class in case I need it outside of the Init function? I could make it a class variable, but the question is if it could change? My gut feeling is that it wouldn't change since it is the main form. Thanks again, Ilan
-
Thanks for your help. I think you got slightly confused as to who changes who. I want the function to change the form. In any case your help was enough to get me going. It doesn't recognize 'this', but on the other hand it knows what 'Me' is. Likewise it doesn't know what 'Form1 mainForm' is but it does know what 'ByRef mainForm As Form1' is. As far a return values, I return a simple status value and apparently it has no troubles with integer return values. Something more complicated like a string would most likely demand your syntax. In any case I used: Public Function Init(ByRef mainForm As Form1) Dim stat As Integer = DS_SUCCESS mainForm.Test1.Text = "my Test" and I called it by stat = myScope.Init(Me) and it finally changed the button text. (Not that I really want to change that button's text, but I do want to know how to change other things on demand.) Do you know if I could save the value of mainForm in my class in case I need it outside of the Init function? I could make it a class variable, but the question is if it could change? My gut feeling is that it wouldn't change since it is the main form. Thanks again, Ilan
You can save the value of mainForm in your class as a class variable. If the mainForm is the first form that is created and the last form that closes then it will not change.
-
Thanks for your help. I think you got slightly confused as to who changes who. I want the function to change the form. In any case your help was enough to get me going. It doesn't recognize 'this', but on the other hand it knows what 'Me' is. Likewise it doesn't know what 'Form1 mainForm' is but it does know what 'ByRef mainForm As Form1' is. As far a return values, I return a simple status value and apparently it has no troubles with integer return values. Something more complicated like a string would most likely demand your syntax. In any case I used: Public Function Init(ByRef mainForm As Form1) Dim stat As Integer = DS_SUCCESS mainForm.Test1.Text = "my Test" and I called it by stat = myScope.Init(Me) and it finally changed the button text. (Not that I really want to change that button's text, but I do want to know how to change other things on demand.) Do you know if I could save the value of mainForm in my class in case I need it outside of the Init function? I could make it a class variable, but the question is if it could change? My gut feeling is that it wouldn't change since it is the main form. Thanks again, Ilan
That is nice and all, but completely bad practice in an Object Oriented language. You're other class is now forever tied to this form and only this form. You'll never be able to reuse it in any other project, or even any other form, unless it's built speicifically with this class in mind. Any code that changes the form should remain in the form's code, not in a seperate class. The form's code is responsible for updating the UI it's showing. If your class has to return a value that changes the form, the form code should be evaluating that return value and making any necessary changes. Keep the UI code in the UI layer, not the business layer.
Dave Kreskowiak Microsoft MVP - Visual Basic
-
That is nice and all, but completely bad practice in an Object Oriented language. You're other class is now forever tied to this form and only this form. You'll never be able to reuse it in any other project, or even any other form, unless it's built speicifically with this class in mind. Any code that changes the form should remain in the form's code, not in a seperate class. The form's code is responsible for updating the UI it's showing. If your class has to return a value that changes the form, the form code should be evaluating that return value and making any necessary changes. Keep the UI code in the UI layer, not the business layer.
Dave Kreskowiak Microsoft MVP - Visual Basic
Thanks Dave. You are right. I have very little experience with Visual Basic so it was a struggle to get ANYTHING to work. However since I'm at the start it should be fairly easy to change it to return things to the form and have the form make the changes. Ilan
-
Thanks for your help. I think you got slightly confused as to who changes who. I want the function to change the form. In any case your help was enough to get me going. It doesn't recognize 'this', but on the other hand it knows what 'Me' is. Likewise it doesn't know what 'Form1 mainForm' is but it does know what 'ByRef mainForm As Form1' is. As far a return values, I return a simple status value and apparently it has no troubles with integer return values. Something more complicated like a string would most likely demand your syntax. In any case I used: Public Function Init(ByRef mainForm As Form1) Dim stat As Integer = DS_SUCCESS mainForm.Test1.Text = "my Test" and I called it by stat = myScope.Init(Me) and it finally changed the button text. (Not that I really want to change that button's text, but I do want to know how to change other things on demand.) Do you know if I could save the value of mainForm in my class in case I need it outside of the Init function? I could make it a class variable, but the question is if it could change? My gut feeling is that it wouldn't change since it is the main form. Thanks again, Ilan
IlanTal wrote:
I think you got slightly confused as to who changes who. I want the function to change the form.
No, I'm not confused about that.
IlanTal wrote:
It doesn't recognize 'this', but on the other hand it knows what 'Me' is. Likewise it doesn't know what 'Form1 mainForm' is but it does know what 'ByRef mainForm As Form1' is.
Right. I'm only programming C# myself, so I mixed up the syntax a bit. Sorry for that. There is no reason to specify ByRef for the reference, though. You are not going to replace the main form with a completeley new form in the method, are you?
IlanTal wrote:
Do you know if I could save the value of mainForm in my class in case I need it outside of the Init function? I could make it a class variable, but the question is if it could change? My gut feeling is that it wouldn't change since it is the main form.
Yes, you could. The reference to the main form won't change as long as the form is open. However, as I suggested before, I reccommend that you just send a reference to the control that the method is going to change. That way it's obvious what the method is going to update, and as the method isn't locked to a specific control you might be able to use the method for more than one control.
--- b { font-weight: normal; }