|
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; }
|
|
|
|
|
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
|
|
|
|
|
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; }
|
|
|
|
|
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.
|
|
|
|
|
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
|
|
|
|
|
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; }
|
|
|
|
|
i use data grid to show my main information from ADODC,
but i wanna add a function on when user click on the selected line on data grid, the sub information will show at the other list view or data grid...
is it better to show in data grid or list view for the sub catagory?
Exmaple,
MY recent Datagrid show the receipt No. when user click on it, the list of item will show out for that specific Receipt no.
|
|
|
|
|
campbells wrote: is it better to show in data grid or list view for the sub catagory?
it all depends on the requirement.. Datagrid is good option if you are going to edit that information or even for Viewing only. ListView is good for viewing only not for editing.
|
|
|
|
|
do u know how to command on when specific record was click then it will show the sub category ??
i have show my information in the data grid ,now i want to show it sub information when the data grid record was selected..
|
|
|
|
|
How do I create a custom control with a datasource that populates its dropdown with the list of available datasources? There is no dropdown.
|
|
|
|
|
This seems to be the answer:
<category("data"), _
="" attributeprovider(gettype(ilistsource)),="" _=""
="" refreshproperties(refreshproperties.all),="" browsable(true)=""> _
Public Property DataSource() As Object
...
End Property
I earlier had it coded AttributeProvider("IListSource")
|
|
|
|
|
I'm a newbie so I'm sorry if this is a stupid question.
Could someone give me the highlights of what vb.net functions I would use to get XML from a webservice into a dataset on a vb.net windows form ?
I have a skeleton webservice and I can see how I can add a sqldataadapter to it. But I cant figure out how to make the webservice publish the data from the sqldataadapter as XML. Or how I would take that XML and put it into a dataset on the client.
Thanks
|
|
|
|
|
If you are trying to have the webservice provide a typed-dataset to the client you will need to write a function on the webservice to return one. You will need to first create a dataset in the webservice project (right-click the project name, select 'Add new item', select 'Dataset', and configure it to your liking). You can then write a function to return that dataset; for example:
<Webmethod()> _
Public Function GetTypedDataset() as myDataset
Dim TableAdapter1 as myDatasetTableAdapters.TableAdapter1
Dim TableAdapter2 as myDatasetTableAdapters.TableAdapter2
Dim returnDataset as new myDataset
TableAdapter1.Fill(returnDataset.Table1)
TableAdapter2.Fill(returnDataset.Table2)
Return returnDataset
In your client project, you can select 'Add a new datasource', select 'Web Service', and follow the Wizard to select your web service.
The typed-dataset will then be useable in your client application. To fill it, you would call the function demonstrated above. You would also need to write methods on the webservice to handle updating, and call them from your client as needed.
Hope this helps,
Keith
-- modified at 0:06 Wednesday 9th August, 2006
|
|
|
|
|
Thats a great help - thanks
|
|
|
|
|
I'm using Visual Studio 2003 - are tableadapters available in 2003 ? Or should I use an alternative approach ?
Thanks
|
|
|
|
|
Suppose i have two buttons btn1 and btn2 and i have created a click event function of btn1. now if i want to call btn1 click event function when user click btn2 please tell me soon
|
|
|
|
|
|
Another method is to have the same event handler handle the Click event of both buttons. Look at the end of the event handler declaration for one of the buttons Click events and you'll find a Handles clause:
Private Sub Button1_Click(blah, blah) Handles Button1.Click
You can have a single handler handle the Click events of more than one button by doing this:
Private Sub MyClickHandler(blah, blah) Handles Button1.Click, Button2.Click
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Button1_Click(nothing,nothing)
U Xux
|
|
|
|
|
I am writing a VBA macro in Excel, and have declared 2 global string arrays.
I am manipulating these arrays in a couple of function without passing them as params -since they are global- however when I return from the first function my array is losing its elements. Is this code error, or am I missing something with arrays.
|
|
|
|
|
Ok the problem is because when I was debugging I added a watch for the array variables via the array var in the procedure, which would give it a out of context value when out of the proc. So lesson is when using globals to add watches on the global declaration not the var within a proc to ensure you have the value of the global representation.
|
|
|
|