Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Software Development
Tag Cloud
access acer asus bios bsod computer crash desktop driver drivers error ethernet excel freeze gaming hard drive hardware hdmi internet laptop malware memory modem monitor motherboard network printer problem ram registry router security slow software sound toshiba trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Software Development >
vb.net combine forms

Reply  
Thread Tools
nsr1's Avatar
Computer Specs
Member with 58 posts.
 
Join Date: Jul 2009
Experience: Intermediate
23-Jan-2010, 01:21 PM #1
vb.net combine forms
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-'s Avatar
Senior Member with 1,943 posts.
 
Join Date: Jul 2008
Location: Earth
Experience: General
24-Jan-2010, 11:44 AM #2
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's Avatar
Computer Specs
Member with 58 posts.
 
Join Date: Jul 2009
Experience: Intermediate
24-Jan-2010, 12:01 PM #3
I'm kinda new to vb.net.
Could you explain how to get in the right scope?
-Fabez-'s Avatar
Senior Member with 1,943 posts.
 
Join Date: Jul 2008
Location: Earth
Experience: General
24-Jan-2010, 12:40 PM #4
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

Code:
ClassName.VariableName 
__________________
Like coding, want help with coding or want to learn coding ? Then the Coders Group is for you
nsr1's Avatar
Computer Specs
Member with 58 posts.
 
Join Date: Jul 2009
Experience: Intermediate
24-Jan-2010, 03:18 PM #5
When I type Form2.t, "text" shows up instead of "time" in the box where it tries to predict what comes next.
nsr1's Avatar
Computer Specs
Member with 58 posts.
 
Join Date: Jul 2009
Experience: Intermediate
24-Jan-2010, 03:21 PM #6
I didnt put public.
nsr1's Avatar
Computer Specs
Member with 58 posts.
 
Join Date: Jul 2009
Experience: Intermediate
24-Jan-2010, 04:30 PM #7
I have 1 more question:
How can i save the options (like checkbox is checked/unchecked & what radiobutton is selected)?
-Fabez-'s Avatar
Senior Member with 1,943 posts.
 
Join Date: Jul 2008
Location: Earth
Experience: General
24-Jan-2010, 05:39 PM #8
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.
__________________
Like coding, want help with coding or want to learn coding ? Then the Coders Group is for you
nsr1's Avatar
Computer Specs
Member with 58 posts.
 
Join Date: Jul 2009
Experience: Intermediate
25-Jan-2010, 04:20 PM #9
Sorry to be a pain but how would you save a checkbox/radiobutton state?
-Fabez-'s Avatar
Senior Member with 1,943 posts.
 
Join Date: Jul 2008
Location: Earth
Experience: General
25-Jan-2010, 04:47 PM #10
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's Avatar
Computer Specs
Member with 58 posts.
 
Join Date: Jul 2009
Experience: Intermediate
29-Jan-2010, 05:42 PM #11
I'm having a hard time coding that. could you please give me an example?
Thank you.
-Fabez-'s Avatar
Senior Member with 1,943 posts.
 
Join Date: Jul 2008
Location: Earth
Experience: General
31-Jan-2010, 11:29 AM #12
This block of code will save the control states defined in ControlsToSave

Code:
  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

Code:
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.
__________________
Like coding, want help with coding or want to learn coding ? Then the Coders Group is for you
nsr1's Avatar
Computer Specs
Member with 58 posts.
 
Join Date: Jul 2009
Experience: Intermediate
31-Jan-2010, 03:14 PM #13
Either I put them in the wrong space or it doesn't work.

Here's the options popup code:
Quote:
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
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

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim ControlStatesFile = FileIO.FileSystem.OpenTextFileReader("config.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
If CheckBox1.CheckState = 0 Then
RadioButton1.Hide()
RadioButton1.Checked = False
RadioButton2.Hide()
RadioButton2.Checked = False
RadioButton3.Hide()
RadioButton3.Checked = False
End If


End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Close()
End Sub

Private Sub CheckBox1_CheckStateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckStateChanged

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

Else

If CheckBox1.CheckState = 1 Then
RadioButton1.Show()
RadioButton2.Show()
RadioButton3.Show()
End If
If RadioButton1.Checked = True Then
time = 60000
ElseIf RadioButton2.Checked = True Then
time = 300000
ElseIf RadioButton3.Checked = True Then
time = 600000
End If
End If
End Sub
End Class
-Fabez-'s Avatar
Senior Member with 1,943 posts.
 
Join Date: Jul 2008
Location: Earth
Experience: General
31-Jan-2010, 04:11 PM #14
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's Avatar
Computer Specs
Member with 58 posts.
 
Join Date: Jul 2009
Experience: Intermediate
01-Feb-2010, 03:26 PM #15
I figured out that only the radio buttons get saved & not the check box when i comment out:
Quote:
If CheckBox1.CheckState = 0 Then
RadioButton1.Hide()
RadioButton1.Checked = False
RadioButton2.Hide()
RadioButton2.Checked = False
RadioButton3.Hide()
RadioButton3.Checked = False
End If
Reply

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 10:23 PM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.