-
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
Public Function isRouteValid(ByRef inForm As System.Windows.Forms.Form, Optional ByRef inAllowALLRoutes As Boolean = True) As Boolean
'Dim localRouteName As String
' Dim localComboRouteText As String
Dim x As System.Windows.Forms.Form
x = My.Forms.frmShippingDesktop
' 'If frmShippingDesktop.Name = inForm.ActiveForm.Name Then
' ' inForm = CType(inForm, x)
' ' localComboRouteText = UCase(Trim(inForm.cmbRoute.Text))
' 'End If
localComboRouteText = UCase(Trim(inForm.cmbRoute.Text))
If Len(localComboRouteText) > 0 Then
If localComboRouteText = xALL_ROUTES And inAllowALLRoutes Then
localRouteName = xALL_ROUTES
inForm.lblRoute.Caption = ""
isRouteValid = True
Else
localRouteName = GetRouteName(localComboRouteText)
If Len(localRouteName) = 0 Then
inForm.lblRoute.Caption = ""
inForm.cmbRoute.SetFocus()
isRouteValid = False
Else
inForm.lblRoute.Caption = " " & Trim(localRouteName)
isRouteValid = True
End If
End If
' Else
' If inAllowALLRoutes Then
' inForm.cmbRoute.Text = xALL_ROUTES
' inForm.lblRoute.Caption = ""
' isRouteValid = True
' Else
' inForm.cmbRoute.Text = ""
' inForm.lblRoute.Caption = ""
' isRouteValid = False
' End If
' End If
isRouteValid = True
End Function
-
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.
-
Thanks for the suggestion. I just tried it, but it still didn't work. I believe the problem is that i am hard coding the controls in the code. i.e.
inform=new frmshippingdesktop
.....inform.cmbroute.text
since it doesn't know "cmbroute.text" is a control until runtime, the code wont allow me to call the control
-
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?
-
 Originally Posted by christianbg
Thanks for the suggestion. I just tried it, but it still didn't work. I believe the problem is that i am hard coding the controls in the code. i.e.
inform=new frmshippingdesktop
.....inform.cmbroute.text
since it doesn't know "cmbroute.text" is a control until runtime, the code wont allow me to call the control
I see I didn't notice your declaration in the function. 'inForm' should be declared as frmShippingDesktop. So your function declaration should look like this.
Public Function isRouteValid(ByRef inForm As frmShippingDesktop, Optional ByRef inAllowALLRoutes As Boolean = True) As Boolean
-
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.
the top is me playing around and trying different things
dim x ....
and
x =....
are options i was trying
Public Function isRouteValid(ByRef inForm As System.Windows.Forms.Form, Optional ByRef inAllowALLRoutes As Boolean = True) As Boolean
'Dim localRouteName As String
' Dim localComboRouteText As String
Dim x As System.Windows.Forms.Form
'x = New frmShippingDesktop
If frmShippingDesktop.Name = inForm.ActiveForm.Name Then
inForm = New frmShippingDesktop
localComboRouteText = UCase(Trim(inForm.cmbRoute.Text))
End If
localComboRouteText = UCase(Trim(inForm.cmbRoute.Text))
If Len(localComboRouteText) > 0 Then
If localComboRouteText = xALL_ROUTES And inAllowALLRoutes Then
localRouteName = xALL_ROUTES
inForm.lblRoute.Caption = ""
isRouteValid = True
Else
localRouteName = GetRouteName(localComboRouteText)
If Len(localRouteName) = 0 Then
inForm.lblRoute.Caption = ""
inForm.cmbRoute.SetFocus()
isRouteValid = False
Else
inForm.lblRoute.Caption = " " & Trim(localRouteName)
isRouteValid = True
End If
End If
' Else
' If inAllowALLRoutes Then
' inForm.cmbRoute.Text = xALL_ROUTES
' inForm.lblRoute.Caption = ""
' isRouteValid = True
' Else
' inForm.cmbRoute.Text = ""
' inForm.lblRoute.Caption = ""
' isRouteValid = False
' End If
' End If
isRouteValid = True
End Function
-
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.
-
thank you that does solve it. i just have to make sure that no other forms get passed to this function. I believe tehre are though. that is why i was trying to do it sinde the function. thank you
-
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.
Private Sub ShowRoute(ByVal frm As Template)
MsgBox(frm.Route)
End Sub
Now you can create forms using your template which all share common code. Even if the UI of each form very's you can still set properties without worrying about the form having a label named label1.
You can prove this by adding a button and the following code to Form1
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim frm As New Form2
MsgBox(frm.Route)
End Sub
End Class
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
-
that's actually the way i we were going to go. Thanks for the help. We had discussed it last night before we left. having a template form with the controls and then just declaring it.
Similar Threads
-
By e_unit in forum VB Classic
Replies: 2
Last Post: 04-21-2005, 08:44 PM
-
Replies: 0
Last Post: 06-18-2002, 02:41 PM
-
Replies: 1
Last Post: 06-12-2002, 03:43 PM
-
By Garry Mc in forum ASP.NET
Replies: 1
Last Post: 03-25-2002, 11:49 AM
-
By Barend Esterhuizen in forum VB Classic
Replies: 1
Last Post: 06-16-2000, 04:39 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks