If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Passing of Forms

I am trying to pass a form to another form in vb.net, dynamically while i hard code the controls on the form is there a way to do this. Here is a code sample
Please excuse the comments they are from me trying new things, plus the code originates from VB6.
thank you

You'll want to create a new instance of frmShippingDesktop. To do this just use x = new frmShippingDesktop. You forgot the new keyword and also if you try to use My.Forms.frmShippingDesktop it won't work. If you still need help passing it to another form let me know. But if you can pass a variable to another form then you know how to pass a your form.

I read your code again and noticed X is never used, except for one line which was commented. This is the function that's actually recieving the form right? What is the problem? Are you getting an error while passing a form to it?

first let me say that, this all worked in vb 6. Now i am trying to take a form in this function and manipulate the combobox controls and other controls on the form but it won't allow me to pass x the form i want it to be and manipulate the controls throught the code, or i don't know how to yet.

Check the post I made just above your last post. The reason you can't access the controls on the form is because you declared 'inForm' as a form in your function declaration. Because of that 'inForm' acts like a generic form and not an instance of frmShippingDesktop. It needs to be declared as frmShippingDesktop.

Well you could still pass in a generic form but you'll need to cast it in order to access the controls. In your case you would do something like this inside your code. Dim frm as frmShippingDesktop = trycast(inForm,frmShippingDesktop)

Then you could use frm to access all the controls. The problem with this method is it sounds like you want to send in multiple different forms all with similiar controls? Is that a correct assumption. This would get very tricky and at the moment I can't think of a solution. I'm sure there might be one but I just don't know of anything. I'll let you know if an idea comes to mind, but for now I think your stuck with passing in a frmShippingDesktop.

thanks i jsut tried Dim frm as frmShippingDesktop = trycast(inForm,frmShippingDesktop)
but that didn't work either. I have several issues to work through in getting this project up and working VB.net it is definitly a different atmosphere. Thanks for the help so far

Hmm...That code should have worked. Maybe if you post the whole function I could see why there was a problem.

I've been thinking about your problem a little and I think I know how you should solve it. Correct me if I'm wrong but you want to create a function that will take a form and then check/change values on this form. You've succesfully been able to pass in frmShippingDesktop but you want to allow for different types of forms that contain the same type of data but might not be identical to this form. Thus you don't want to limit the form being passed as frmShippingDesktop?

If that is an incorrect assesment then the rest of this post is probably a waste of time, but I think that's what you want. I finally figured out what I was trying to do earlier which is create a base form that you can build off of. It's pretty easy to do in fact. Exactly how you implement everything is up to you but the idea is this. As a quick example try following these steps.

1) Create a new project
2) Add a new form to your project called 'Template'
-Replace the code with this-

Public Class Template
Overridable Property Route() As String
Get
Return String.Empty
End Get
Set(ByVal value As String)

End Set
End Property
End Class

3) Save the project and then select build
4) Add a new form (form2) only this time select inherited form (when asked to choose a form select template)

At this point you've added a form that inherits from template. For the template I've added a property 'Route' which is overridable. The new form will have this same property because it's inheriting from template. In the form you just made add this code and also add a label.

Public Overrides Property Route() As String
Get
Return Label1.Text
End Get
Set(ByVal value As String)
Label1.Text = value
End Set
End Property

This helps you in a number of ways. First off you have encapsulated 'Route' it no longer matters if the text is stored in a label, a textbox or just a variable. This is very good practice. You can implement it differently for each form you make yet it always looks the same in code. All you have to do to set it is write form2.route = "A route".

The second importantce is that you can now create a function like this.

Run the program and click the button. You should see a message 'Label1" Once you've done this go and change Form2. Add a textbox and put some text in it. Then change route property to

Public Overrides Property Route() As String
Get
Return textbox1.text
End Get
Set(ByVal value As String)
textbox1.text() = value
End Set
End Property

Now re-run the program and click the button. You should see a message with text from the textbox. However, from form1's perspective nothing has changed. Which is really the goal here.

Wow, that was alot of writing. I hope I made things clear. If not just ask for clarification, but I think this will help you accomplish what you want. Here is a link to a site I found on form inheritance. It may explain things better than I could. http://www.codeproject.com/dotnet/Vi...nheritance.asp