If Statements and other Forms

Joined
Jan 5, 2008
Messages
8
Programming Experience
Beginner
Hello all. I've made a application and basically I need it to accept inputs from another Form. For example, I have frmMain and frmProfile and I'm trying to make Radio Buttons on Profile effecting frmMain.

Example:

VB.NET:
Public Class frmMain


    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If frmProfile.rdbFire.Checked Then
            btnBar.Text = "Barfira"
        ElseIf frmProfile.rdbWater.Checked Then
            btnBar.Text = "Barwatera"
        ElseIf frmProfile.rdbAero.Checked Then
            btnBar.Text = "Baraera"
        ElseIf frmProfile.rdbThunder.Checked Then
            btnBar.Text = "Barthundra"
        ElseIf frmProfile.rdbBlizzard.Checked Then
            btnBar.Text = "Barblizzara"
        End If

    End Sub
That doesn't work, btnBar is still just displaying it's original text. Any help?

EDIT: Nvm, found out what I did wrong. The code wasn't incorrect but where it was is.
I moved it to frmProfile and to make changes once Apply is clicked. Thank you anyways.
 
Last edited by a moderator:
You still shouldn't do it that way. Better programming practice is to make all your windows controls private and use public properties to modify and return values. It may seem more tedious but it does help improve your code and minimise runtime errors (you know that the problem can only come from the property - in your case, having all the controls decalred public means that problems can come from anywhere outside the form.

For example:
VB.NET:
Public Property Result() As integer
  Get
    Return CInt(me.myTextBox.text)
  End Get
  Set (Byval  value as Integer)
    myTextBox.Text = Value.toString("#0.00")
  End Set
End Property
This adds more functionality as the form itself controls formatting, in this case, rather than the one supplying the data. Also, the calling procedure "knows" that it must supply an integer.

One drawback. You may want to do some validation on the text box first so that only an numerical value is entered by the user before the property is read otherwise an exception will be thrown.
 
Last edited by a moderator:
Controls in form classes are declared with Friend access modifier and thus available from within same assembly. The problem is you use default form instance here while when you showed the profile form you most likely created and used a new instance.
So if you instead of "dim f as new frmProfile, f.show" did "frmProfile.show" you could later get the correct values from frmProfile.thecontrol.propertyvalue. Same goes if you switch around the order and access frmMain.,.
 
Back
Top