So I have these two forms: Form1 and Form2. The first form has a groupbox with radio buttons in it that selects a dormitory with values. At the bottom of the page is a button that says continue and goes to Form 2. In Form2 there is another button that has a groupbox with radios to select a meal plan. At the bottom of the page there is a button that says calculate. When this button is clicked Form1 should show again and there textbox for total cost will have a value in it for the dormcost and mealcost. It works for getting the meal cost, but not the dorm.
Anybody have any idea why?
Form 1:
Public Class Form1
Public DormCost As Integer
Private Sub ButtonCalculate_Click(sender As Object, e As EventArgs) Handles ButtonCalculate.Click
Dim DormCost As Integer
If RadioAllen.Checked = True Then
DormCost = 1500
ElseIf RadioPike.Checked = True Then
DormCost = 1600
ElseIf RadioFarthing.Checked = True Then
DormCost = 1200
ElseIf RadioUniversity.Checked = True Then
DormCost = 1800
End If
Form2.Show()
End Sub
End Class
Form 2:
Public Class Form2
Dim MealCost As Integer
Dim Total As String
Private Sub ButtonCalculate_Click(sender As Object, e As EventArgs) Handles ButtonCalculate.Click
If Radio7.Checked = True Then
MealCost = 560
ElseIf Radio14.Checked = True Then
MealCost = 1095
ElseIf RadioUnlimited.Checked = True Then
MealCost = 1500
End If
Me.Close()
Total = Convert.ToString(Form1.DormCost + MealCost)
Form1.TextBox1.Text = "$" + Total
End Sub
End Class
Here is another option:
Make DormCost local to form1 and call it say myDormCost and put it as Public to Form2
then when you call
form2.show
right before that do
form2.DormCost = myDormCost
That should work.
You can make a global variable class, which can be used all throughout your program.
Public Class GlobalVariables
Public Shared yourvariable As yourtype
End Class
Put this class at the bottom of one of your forms and you can call the variable form anywhere in the program. GlobalVariables.yourvariable
Related
I am new in Vb.net. I'm still studying the logics in this language. I want to output data in a label.text from form 1 to form 2 with the use of a button. How can I do that while both forms are running?
PS. label.text may change value every time I click the button.
Here are two options.
Use a property setter
Use a method
Note: The code below assumes the following:
Form1: Button (name: Button1)
Form2: Label (name: Label1)
When the button is clicked on Form1, if Form2 isn't open, it opens it. Additionally, the value of the label on Form2 is set.
Option 1: (use a property setter)
Form1.vb
Public Class Form1
Dim counter As Integer = 0
Dim f2 As Form2 = Nothing
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If f2 Is Nothing Then
'create new instance
f2 = New Form2
'show form
f2.Show()
Else
'show window
'if window is minimized, it will "unminimize" it
'f2.WindowState = FormWindowState.Normal
'bring window into focus
'also brings the window to front
'f2.Activate()
End If
'set value
Dim username As String = String.Format("user{0}", counter)
'set property value
f2.Username = username
counter += 1
End Sub
End Class
Form2.vb
Public Class Form2
Dim _username As String = String.Empty
Public Property Username As String
Get
Return _username
End Get
Set(value As String)
_username = value
Label1.Text = value
Label1.Refresh()
End Set
End Property
End Class
Option 2: (use a method)
Form1.vb
Public Class Form1
Dim counter As Integer = 0
Dim f2 As Form2 = Nothing
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If f2 Is Nothing Then
'create new instance
f2 = New Form2
'show form
f2.Show()
Else
'show window
'if window is minimized, it will "unminimize" it
'f2.WindowState = FormWindowState.Normal
'bring window into focus
'also brings the window to front
'f2.Activate()
End If
'set value
Dim username As String = String.Format("user{0}", counter)
'set value using method
f2.SetLabelText(username)
counter += 1
End Sub
End Class
Form2.vb
Public Class Form2
Public Sub SetLabelText(ByVal username As String)
Label1.Text = username
Label1.Refresh()
End Sub
End Class
Resources:
How to: Create a Property (Visual Basic)
I created a class that inherits a timer class because I want to customize the Tick function, and I want to use this specific function in many classes without the need to change the function in all the timers every time.
Public Class FadeInTimer
Inherits Timer
Public Sub New()
MyBase.New()
Me.Enabled = False
Me.Interval = 75
End Sub
Private Sub FadeInTimer_Tick(sender As Object, e As EventArgs) Handles Me.Tick
Dim workingAreaWidth As Integer = Screen.PrimaryScreen.WorkingArea.Width - Me.Width
Me.Opacity += 0.1
If Not Me.Location.X <= workingAreaWidth Then
Me.Location = New Point(Me.Location.X - 30, Me.Location.Y)
End If
Me.Refresh()
If Me.Opacity = 1 Then
Me.Stop()
End If
End Sub
End Class
The purpose of this function is to make a simple fade in when the form is created. The problem is I can't use "Me." because I am in the Timer class, so, how can I make changes to the form from this class.
The first thing to do is to pass an instance of the form to be faded in inside the constructor of the custom timer, save that instance in a global class variable and add the tick handler with AddHandler like so
Public Class FadeInTimer
Inherits System.Windows.Forms.Timer
Dim parent As Form
Public Sub New(p As Form)
MyBase.New()
parent = p
AddHandler MyBase.Tick, AddressOf FadeInTimer_Tick
End Sub
Now, when you need to refer to the 'parent' form you use the parent variable and not the Me statement. Also, every time you need to refer to the timer, you should use MyBase statement
Private Sub FadeInTimer_Tick(sender As Object, e As EventArgs)
Dim workingAreaWidth As Integer = Screen.PrimaryScreen.WorkingArea.Width - Parent.Width
parent.Opacity += 0.1
If Not parent.Location.X <= workingAreaWidth Then
parent.Location = New Point(parent.Location.X - 30, parent.Location.Y)
End If
parent.Refresh()
If parent.Opacity = 1 Then
MyBase.Stop()
End If
End Sub
This could be tested in LinqPad using this code
Sub Main
Dim f As Form = New Form()
Dim t As FadeInTimer = New FadeInTimer(f)
f.Opacity = 0
t.Interval = 150
t.Start()
f.ShowDialog()
End Sub
In a Windows Forms application, I've got a timer with the following code:
Public Class SyncForm
Public pbvalue As Integer = 0
Public pbProcess As String = ""
Sub DataProcess()
pb.Value += 1
pbProcess = "Read, match and compare"
...
pb.Value += 1
pbProcess = "Exporting..."
End Sub
Private Sub timerpb_Tick(sender As Object, e As EventArgs) Handles timerpb.Tick
pb.Value = pbValue
lbReadProcess.Text = pbProcess
lbReadProcess.Refresh()
pb.PerformStep()
End Sub
End Class
I start the DataProcess sub with a button on this form.
During the process, the progress bar is updated correctly, but the label on the form (lbReadProcess), that should indicate the currently running process, not. Can someone give me a hint, why not? Thanks.
I'm having this codes at all forms so I could have a sum of score in my quiz. But when the score form shows up in the end, it shows 0 and is more likely not incrementing when i have a wright or wrong answer. I'm sorry its my first time to make in VB basics. Wish someone could help.
Public Class Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles NextButton1.Click
If Option2.Checked Then
Score.ScoreRight.Text = Score.ScoreRight.Text + 1
Dim Form2 As New Form3
Form3.Show()
Me.Hide()
Else
Score.ScoreWrong.Text = Score.ScoreWrong.Text + 1
Dim Form2 As New Form3
Form3.Show()
Me.Hide()
End If
End Sub
End Class
I'm assuming that Score is a form and that Score.Scoreright and score.scorewrong are textboxes in that form
try this maybe
Public class Score
Dim withevents Form2instance as new Form2
Dim withevents Form3instance as new Form3
Dim rightAnsweres as integer = 0
Dim wronganswers as integer = 0
Public sub Updateresults()
Scoreright.text = rightansweres
ScoreWrong.text = wronganswers
End Sub
Public sub Form2Instance_QuestionAnswered()Handles Form2Instance.QuestionAnswered
if Form2Instance.AnsweredCorrectly = true then
rightansweres = rightansweres+1
else
wronganswers = wronganswers + 1
end if
Form2instance.Hide
UpdateResults()
Form3instance.show
end sub
end class
Now in your form2 and form3 classes you need an event and you need to raise the event when the question is answered.
Public Class Form2
Public event QuestionAnswered()
Property AnsweredCorrectly as integer = False
Sub RunThisAfterYouHaveIndicatedWhetherOrNotTheAnswerWasCorrect()
RaiseEvent QuestionAnswered()
End Sub
end class
Now to explain
You are trying to call a routine inside of a general class type and expecting the results to be updated inside of an active instance of that class. Or so it seems.
You have to have some kind of reference to the instance of the class that you're trying to update.
In this example, The class you're trying to update has a reference to the forms that it needs information from. It receives the information by waiting for the form to raise an event and then handles it's business.
Is Option2 a checkbox? If so, i think you need to use the .checkstatus property of the checkbox instead of .checked
If Option2.checkedstate = checkedstate.checked then
I'm using Visual Studio 2010 Professional.
I have one form (and its associated vb file) and another, separate vb file. When I go to compile and debug my code, my building succeeds, and the form displays, but the "ball" doesn't move.
My startup class:
Public Class Bouncer
Private bouncingBall As Ball
Private Sub CST8333_Lab3_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
bouncingBall = New Ball(Me)
'Me.Controls.Add(Ball)
End Sub
Private Sub Timer_Tick(sender As System.Object, e As System.EventArgs) Handles Timer.Tick
bouncingBall.MoveBall()
End Sub
End Class
My other, separate class:
Public Class Ball
Private ballX As Integer
Private ballY As Integer
Private ballMovementX As Integer
Private ballMovementY As Integer
Private _bouncer As Bouncer
Sub New(bouncer As Bouncer)
_bouncer = bouncer
ballX = 50
ballY = 50
ballMovementX = 5
ballMovementY = 5
End Sub
Public Function GetBallX() As Integer
Return ballX
End Function
Public Sub MoveBall()
If (ballX >= _bouncer.Width) Then
ballMovementX = -ballMovementX
ElseIf (ballX <= 0) Then
ballMovementX = -ballMovementX
End If
If (ballY >= _bouncer.Height) Then
ballMovementY = -ballMovementY
ElseIf (ballY <= 0) Then
ballMovementY = -ballMovementY
End If
ballX += ballMovementX
ballY += ballMovementY
End Sub
End Class
My form displays, but my "ball" doesn't move. What I would like is for the variables and subroutines in my Ball class to control the movement of my Label "ball". Any help, suggestions?
You should probably use a timer instead of the While True loop. The While True loop isn't giving the GUI a chance to update the screen.
Assuming Ball is a control, it needs to be added to the form's collection:
bouncingBall = New Ball(Me)
Me.Controls.Add(bouncingBall)
bouncingBall.MoveBall()
It's unclear what your Ball class is doing. It looks like it's just updating internal variables and not actually moving the control, which is what I'm suspecting your are trying to accomplish.