# vb.net combine forms



## nsr1 (Jul 18, 2009)

Hello,

Is there a way to combine 2 forms so that when a person clicks a button the 2nd form opens up & pass a value to the timer in the 1st form?


----------



## -Fabez- (Jul 28, 2008)

You can access variables from another form in a Visual Basic project, provided they are in the right scope. You can do this through the forms class.


----------



## nsr1 (Jul 18, 2009)

I'm kinda new to vb.net.
Could you explain how to get in the right scope?


----------



## -Fabez- (Jul 28, 2008)

If you want to share a variable with another class, a good place to declare the variable is globally. In order to make sure a variable is global, declare it outside of any event handler, function, sub, etc. After you have done this it will become accessible to other forms and classes by using the following code


```
[I]ClassName.VariableName[/I]
```


----------



## nsr1 (Jul 18, 2009)

When I type Form2.t, "text" shows up instead of "time" in the box where it tries to predict what comes next.


----------



## nsr1 (Jul 18, 2009)

I didnt put public.


----------



## nsr1 (Jul 18, 2009)

I have 1 more question:
How can i save the options (like checkbox is checked/unchecked & what radiobutton is selected)?


----------



## -Fabez- (Jul 28, 2008)

You can either use the public keyword or set your IntelliSense to show all available options if the variable is not being shown. If it still does not show, then try manually typing the variable name. Controls should retain thier state while the program is running, however in order to keep thier state when the program ends you could write the states to a file.


----------



## nsr1 (Jul 18, 2009)

Sorry to be a pain but how would you save a checkbox/radiobutton state?


----------



## -Fabez- (Jul 28, 2008)

You would create a file and write each of the controls states to it when your program exits. Then when your program starts again, read the file to determine the state of each control.


----------



## nsr1 (Jul 18, 2009)

I'm having a hard time coding that. could you please give me an example?
Thank you.


----------



## -Fabez- (Jul 28, 2008)

This block of code will save the control states defined in ControlsToSave


```
Dim ControlsToSave() As String = {"ChkOne", "ChkTwo", "ChkThree", "ChkFour", "RdoOne", "RdoTwo", "RdoThree", "RdoFour"}
        Dim ControlStatesFile = FileIO.FileSystem.OpenTextFileWriter("Data.txt", False)
        For Each FormControl In Me.Controls
            If Array.LastIndexOf(ControlsToSave, FormControl.name) <> -1 Then
                ControlStatesFile.Write(FormControl.name.ToString + ":" + IIf(FormControl.checked, "True", "False") + vbNewLine)
            End If
        Next
        ControlStatesFile.Flush()
        ControlStatesFile.Close()
        End
```
And this block of code will restore the controls


```
Dim ControlStatesFile = FileIO.FileSystem.OpenTextFileReader("Data.txt")
        Dim ControlStates As String = ""
        While ControlStatesFile.Peek <> -1
            ControlStates += ControlStatesFile.ReadLine() + ":"
        End While
        ControlStatesFile.Close()
        Dim ControlsToRestore() As String = ControlStates.Split(":")
        For Each FormControl In Me.Controls
            For Count As Integer = 0 To ControlsToRestore.Length - 1 Step 2
                If FormControl.name = ControlsToRestore(Count) Then
                    FormControl.checked = ControlsToRestore(Count + 1)
                End If
            Next
        Next
```
The two blocks of code above will only work with radio and checkbox controls, as these are the only controls you specified, although it can easily be expanded to include other types of controls. If this is for a school, college, etc project please make sure you understand how the code works instead of just copying and pasting it.


----------



## nsr1 (Jul 18, 2009)

Either I put them in the wrong space or it doesn't work.

Here's the options popup code:


> Public Class Form2
> Public time As Integer = 10000
> 
> Private Sub Form_close(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Leave
> ...


----------



## -Fabez- (Jul 28, 2008)

Do you get an error, if so what is it ? Try moving my code to restore the controls state to a different function other than Form1_Load, as the controls don't exist fully yet.


----------



## nsr1 (Jul 18, 2009)

I figured out that only the radio buttons get saved & not the check box when i comment out:


> If CheckBox1.CheckState = 0 Then
> RadioButton1.Hide()
> RadioButton1.Checked = False
> RadioButton2.Hide()
> ...


----------



## -Fabez- (Jul 28, 2008)

This following line should be changed to reflect the names of your controls, RadioButton1 for example.


```
Dim ControlsToSave() As String = {"Chkbx", "RdoOne", "RdoTwo", "RdoThree"}
```


----------



## nsr1 (Jul 18, 2009)

that makes sense


----------



## nsr1 (Jul 18, 2009)

I get the same result:
Not saving the checkbox state.


----------



## -Fabez- (Jul 28, 2008)

This piece of code needs attention


```
Private Sub Form_close(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Leave
        Dim ControlsToSave() As String = {"Chkbx", "RdoOne", "RdoTwo", "RdoThree"}
 
        Dim ControlStatesFile = FileIO.FileSystem.OpenTextFileWriter("config.txt", False)
        For Each FormControl In Me.Controls
            If Array.LastIndexOf(ControlsToSave, FormControl.name) <> -1 Then
                ControlStatesFile.Write(FormControl.name.ToString + ":" + IIf(FormControl.checked, "True", "False") + vbNewLine)
            End If
        Next
        ControlStatesFile.Flush()
        ControlStatesFile.Close()
        End
    End Sub
```
As this event is triggered when the form close's, there is no need for the end command. You should move all code from this function to the vent handler for the button, etc that starts the exit procedure. Is the settings file generated in your debug environment?


----------



## nsr1 (Jul 18, 2009)

That worked!
But when i click the close button, both forms close. I'll try to figure it out.


----------



## -Fabez- (Jul 28, 2008)

Instead of using End, try using Me.Close(), it might make a difference.


----------



## nsr1 (Jul 18, 2009)

yup, now the "time" values won't go to form1 or form1 isn't seeing form2's checkbox is checked & you have to click the radio button twice to change the selection .
Never ends..hope you don't mind helping a dummy like me lol. I'll put your name in the credits


----------



## -Fabez- (Jul 28, 2008)

Try closing the second form from the first form when the time values have been retrieved. This will require you to change Me.Close() to _FormName._Close()


----------



## nsr1 (Jul 18, 2009)

when I try form2.close(), it says Error 1 'WindowsApplication1.Form2' cannot refer to itself through its default instance; use 'Me' instead.


----------



## -Fabez- (Jul 28, 2008)

If the first form does not pass the time variable to the second form when it close's itself, remove the Me.Close() statement from the first form. Then in the function that retrieves the time variable, add _FirstFormName._Close()


----------



## nsr1 (Jul 18, 2009)

Actually, i want the time variable to pass from form2 to form1. form2 is a options pop-up.


----------



## -Fabez- (Jul 28, 2008)

The same principle applies, just add _OtherFormName._Close() to the function on the first form.


----------



## nsr1 (Jul 18, 2009)

That doesn't work.
Here's the problem:
form1 doesn't see whether checkbox is checked or not & what radio button is checked.


----------



## -Fabez- (Jul 28, 2008)

How do you know this ? Do you get an error or do the control names not appear in IntelliSense, etc ?


----------



## nsr1 (Jul 18, 2009)

Form1 code:


> Public Class Form1
> 
> Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
> WebBrowser1.Navigate("https://www.google.com/voice/m")
> ...


When I comment out If Form2.CheckBox1.CheckState = 0 Then
Timer1.Enabled = False
ElseIf Form2.CheckBox1.CheckState = 1 Then
Timer1.Enabled = True
End If, it will auto refresh every 10 seconds (default time) instead of the user-defined time.


----------



## nsr1 (Jul 18, 2009)

hello ?


----------



## -Fabez- (Jul 28, 2008)

I thought the idea of the form was to refresh every user defined interval ? Can you post the other forms code please ?


----------



## nsr1 (Jul 18, 2009)

it is but i just put a default for giggles.

Form2:


> Public Class Form2
> Public time As Integer = 10000
> Public check As String
> Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
> ...


The problem is Form1 isn't seeing form2's checkbox state or what radio button is picked.


----------



## aolujumu (Feb 18, 2010)

I did a little Vb.net
and i think the classes and libraries are very vague and not 
as flexible as Php scripting
but visual basics has nice GUI i love its GUI functions they stand out 
But yea its a good language though just not my personal favourite


----------



## -Fabez- (Jul 28, 2008)

If you close the form before checking the property of a control on the form then the check will fail. Try keeping both forms open to see if that makes a difference.


----------

