vb .net newbie. I need to pass a variable to a form from my menu - How?

  • Thread starter Thread starter Robert Johnson
  • Start date Start date
R

Robert Johnson

Hi all. I have a multi-purpose form that I need to run various code in the
load event so I want to send a var string either "R" , "S" or "Q" that will
determine what code to run. I'm sure it's simple but I couldn't find any
examples in the doc's and books that I own.

Could someone give me an example please. This is a mdi child form.
I want to call it like this OpenRSQ(R) or OpenRSQ(S) etc...

Thanks in advance.

Robert

My code is currently:
Private Sub OpenRSQ()

Dim frmRSQ As New frmRSQMain

frmRSQ.MdiParent = Me

frmRSQ.Show()

End Sub
 
Hi all. I have a multi-purpose form that I need to run various code in the
load event so I want to send a var string either "R" , "S" or "Q" that will
determine what code to run. I'm sure it's simple but I couldn't find any
examples in the doc's and books that I own.

Could someone give me an example please. This is a mdi child form.
I want to call it like this OpenRSQ(R) or OpenRSQ(S) etc...

Thanks in advance.

Robert

My code is currently:
Private Sub OpenRSQ()

Dim frmRSQ As New frmRSQMain

frmRSQ.MdiParent = Me

frmRSQ.Show()

End Sub

I would do it with an overloaded constructor (sub new) and use an enum
to prevent passing incorrect values.

i.e.

Public Class MyForm

' I would highly recommend using more descriptive names
Public Enum StartBehavior
StartBehaviorR
StartBehaviorQ
StartBehaviorS
End Enum

Public Sub New()
' No parameters needed for instantiation
InitializeComponent()
End Sub

Public Sub New(ByVal behavior As StartBehavior)
InitializeComponent()

Select Case behavior
Case StartBehaviorR
' Do Something
Case StartBehaviorQ
' Do Something Else
Case StartBehaviorS
' You know the drill
End Select
End Sub

End Class
 
If I understand you you are suggesting, this is to replace my code that
calls the form? It looks like you are thinking of 3 seperate forms where I
am using just one form where I modify lables and fields where necessary in
the Load event. I have a If statement in the Load event that will evaluate
the value of the var passed in if I can figure out how to pass it in... lol.
That is what I need to know. Or did I misunderstand you?

Robert
 
If I understand you you are suggesting, this is to replace my code that
calls the form? It looks like you are thinking of 3 seperate forms where I
am using just one form where I modify lables and fields where necessary in
the Load event. I have a If statement in the Load event that will evaluate
the value of the var passed in if I can figure out how to pass it in... lol.
That is what I need to know. Or did I misunderstand you?

Robert












- Show quoted text -

No, I'm talking about a singleform. The only difference is that I am
passing the parameter in the form's constructor instead of grabbing it
in the load event. Remember Load in an event, and is listened to by an
event handler who's signature is static, therefore you can't add
another parameter to it. You spoke of passing 3 possible commands (P,
Q, and S) to the form as a string, I replaced that as an enumeration
which is more efficient than using a string, and can be much easier to
maintain. I overloaded the constructor so it would not break your
existing code, but just extend your form class so that if you needed
to pass the extra information you could call the overloaded
constructor that has the enumeration parameter.

Thanks,

Seth Rowe
 
Seth, Ok, I see what you are talking about. I sometimes forget that a form
is just a class also...lol. Remember I said I was a newbie... I have been
working in another language for years but since .NET is where it's at have
decide to take the leap.
Thanks for being patient with me

Regards,

Robert
 
Seth, Ok, I see what you are talking about. I sometimes forget that a form
is just a class also...lol. Remember I said I was a newbie... I have been
working in another language for years but since .NET is where it's at have
decide to take the leap.
Thanks for being patient with me

Regards,

Robert

No problem - I'm guessing you're coming from classic Vb?

Thanks,

Seth Rowe
 
No, I 've been working in Clarion but SoftVelocity is behind in bringing out
their .NET version. It's a GREAT tool but lagging a bit in this regard...
they say it's comming but not sure when it will be gold and I need it now.


I added the code but ran into an error:

StartBehaviourR, Q and S are not declared. I thought the Enum is the
delcaration for them.

Robert

Public Class frmRSQMain

Public Enum StartBehavior

StartBehaviorR

StartBehaviorS

StartBehaviorQ

End Enum

Public Sub New()

' This call is required by the Windows Form Designer.

InitializeComponent()

' Add any initialization after the InitializeComponent() call.

End Sub

Public Sub New(ByVal behavior As StartBehavior)

I nitializeComponent()

Select Case behavior

Case StartBehaviorR <--------------- Error here

Me.Text = "Rental"

Me.GradientFormHeader1.DetailText = "Add, Modify Rentals..."

lblRSQNumber.Text = "Rental No."

lblRSQDate.Text = "Rental Date"

lblRSQNumber2.Text = "Rental No."

lblRSQStartDate.Text = "Rental Start"

lblSubTotal.Text = "Rental SubTotal"

btnRentalList.Text = "Rental List"

Case StartBehaviorQ <--------------- Error here

Me.Text = "Quote"

Me.GradientFormHeader1.DetailText = "Add, Modify Quote..."

lblRSQNumber.Text = "Quote No."

lblRSQDate.Text = "Quote Date"

lblRSQNumber2.Text = "Quote No."

lblRSQStartDate.Text = "Quote Start"

lblSubTotal.Text = "Quote SubTotal"

btnRentalList.Text = "Quote List"

Case StartBehaviorS <--------------- Error here

Me.Text = "Sales"

Me.GradientFormHeader1.DetailText = "Add, Modify Sales..."

lblRSQNumber.Text = "Sales No."

lblRSQDate.Text = "Sales Date"

lblRSQNumber2.Text = "Sales No."

lblRSQStartDate.Text = "Sales Start"

lblSubTotal.Text = "Sales SubTotal"

btnRentalList.Text = "Sales List"

End Select

End Sub
 
No, I 've been working in Clarion but SoftVelocity is behind in bringing out
their .NET version. It's a GREAT tool but lagging a bit in this regard...
they say it's comming but not sure when it will be gold and I need it now.

I added the code but ran into an error:

StartBehaviourR, Q and S are not declared. I thought the Enum is the
delcaration for them.

Robert

Public Class frmRSQMain

Public Enum StartBehavior

StartBehaviorR

StartBehaviorS

StartBehaviorQ

End Enum

Public Sub New()

' This call is required by the Windows Form Designer.

InitializeComponent()

' Add any initialization after the InitializeComponent() call.

End Sub

Public Sub New(ByVal behavior As StartBehavior)

I nitializeComponent()

Select Case behavior

Case StartBehaviorR <--------------- Error here

Me.Text = "Rental"

Me.GradientFormHeader1.DetailText = "Add, Modify Rentals..."

lblRSQNumber.Text = "Rental No."

lblRSQDate.Text = "Rental Date"

lblRSQNumber2.Text = "Rental No."

lblRSQStartDate.Text = "Rental Start"

lblSubTotal.Text = "Rental SubTotal"

btnRentalList.Text = "Rental List"

Case StartBehaviorQ <--------------- Error here

Me.Text = "Quote"

Me.GradientFormHeader1.DetailText = "Add, Modify Quote..."

lblRSQNumber.Text = "Quote No."

lblRSQDate.Text = "Quote Date"

lblRSQNumber2.Text = "Quote No."

lblRSQStartDate.Text = "Quote Start"

lblSubTotal.Text = "Quote SubTotal"

btnRentalList.Text = "Quote List"

Case StartBehaviorS <--------------- Error here

Me.Text = "Sales"

Me.GradientFormHeader1.DetailText = "Add, Modify Sales..."

lblRSQNumber.Text = "Sales No."

lblRSQDate.Text = "Sales Date"

lblRSQNumber2.Text = "Sales No."

lblRSQStartDate.Text = "Sales Start"

lblSubTotal.Text = "Sales SubTotal"

btnRentalList.Text = "Sales List"

End Select

End Sub

Fully Qualify them

i.e.

StartBehaviorQ

should be

StartBehavior.StartBehaviorQ

Thanks,

Seth Rowe
 
No, I 've been working in Clarion but SoftVelocity is behind in bringing out
their .NET version. It's a GREAT tool but lagging a bit in this regard...
they say it's comming but not sure when it will be gold and I need it now.

I added the code but ran into an error:

StartBehaviourR, Q and S are not declared. I thought the Enum is the
delcaration for them.

Robert

Public Class frmRSQMain

Public Enum StartBehavior

StartBehaviorR

StartBehaviorS

StartBehaviorQ

End Enum

Public Sub New()

' This call is required by the Windows Form Designer.

InitializeComponent()

' Add any initialization after the InitializeComponent() call.

End Sub

Public Sub New(ByVal behavior As StartBehavior)

I nitializeComponent()

Select Case behavior

Case StartBehaviorR <--------------- Error here

Me.Text = "Rental"

Me.GradientFormHeader1.DetailText = "Add, Modify Rentals..."

lblRSQNumber.Text = "Rental No."

lblRSQDate.Text = "Rental Date"

lblRSQNumber2.Text = "Rental No."

lblRSQStartDate.Text = "Rental Start"

lblSubTotal.Text = "Rental SubTotal"

btnRentalList.Text = "Rental List"

Case StartBehaviorQ <--------------- Error here

Me.Text = "Quote"

Me.GradientFormHeader1.DetailText = "Add, Modify Quote..."

lblRSQNumber.Text = "Quote No."

lblRSQDate.Text = "Quote Date"

lblRSQNumber2.Text = "Quote No."

lblRSQStartDate.Text = "Quote Start"

lblSubTotal.Text = "Quote SubTotal"

btnRentalList.Text = "Quote List"

Case StartBehaviorS <--------------- Error here

Me.Text = "Sales"

Me.GradientFormHeader1.DetailText = "Add, Modify Sales..."

lblRSQNumber.Text = "Sales No."

lblRSQDate.Text = "Sales Date"

lblRSQNumber2.Text = "Sales No."

lblRSQStartDate.Text = "Sales Start"

lblSubTotal.Text = "Sales SubTotal"

btnRentalList.Text = "Sales List"

End Select

End Sub

Looking at your code again I see something that may help you out:

Change your code to this:

Public Class frmRSQMain

Public Enum FormBehavior
Rental
Sales
Quote
End Enum

Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub

Public Sub New(ByVal behavior As FormBehavior)
InitializeComponent()

Me.Text = behavior.ToString()
Me.GradientFormHeader1.DetailText = String.Format("Add, Modify
{0}...", behavior.ToString())
lblRSQNumber.Text = String.Format("{0} No.",
behavior.ToString())
lblRSQDate.Text = String.Format("{0} Date",
behavior.ToString())
lblRSQNumber2.Text = String.Format("{0} No.",
behavior.ToString())
lblRSQStartDate.Text = String.Format("{0} Start",
behavior.ToString())
lblSubTotal.Text = String.Format("{0} SubTotal",
behavior.ToString())
btnRentalList.Text = String.Format("{0} List",
behavior.ToString())
End Sub

End Class

It seems to me all you were doing was changing labels based on the
enum, this will do it without the select case.

Thanks,

Seth Rowe
 
Thanks Seth, why does it need to be fully qualified if it is a public enum?
I'm missing something here I think...

Robert
 
Very elegant Seth. I like it. Learning something new every day.

Thanks again for taking the time to teach me and to re-write the code...

Regards,

Robert
 
Thanks Seth, why does it need to be fully qualified if it is a public enum?
I'm missing something here I think...

Robert

Enums are always fully qualified - I just didn't add the enum name in
my first code (I typed it in the message). When you use the designer
it will automagically complete the first part - so you'll only need to
member (i.e. you type StartBehaviorQ instead of
StartBehavior.StartBehaviorQ). As to why we have to do this I don't
have a good answer - except that it can stop name conflicts between
the enum and other objects.

Thanks,

Seth Rowe
 
Very elegant Seth. I like it. Learning something new every day.

Thanks again for taking the time to teach me and to re-write the code...

Regards,

Robert

Glad to help!

Thanks,

Seth Rowe
 
Back
Top