Visual Basic windows forms  
Author Message
Aerouk





PostPosted: Visual Basic Express Edition, Visual Basic windows forms Top

Hey,

I've only started using Visual Basic tonight, and I've never touched any previous form of VB and/or Visual Studio. So this is probably the newbie-ist question you've seen for a few years if not ever.

Anyway, I've got a task set out whereby I must create a testing program as such, with multiple choices. I've created a form for every question, along with a welcome form and a grade form.

My question is simply, how do you link the forms, so that a user can click the button labelled "Continue1" and it opens up form2, within the same space and so on and so forth.

Any help would be more than gratefully received.

Thanks!



Visual Studio Express Editions24  
 
 
ahmedilyas





PostPosted: Visual Basic Express Edition, Visual Basic windows forms Top

well to do this, firstly on each form set the Startup Position of the form in the form properties in designer view to centerscreen

 

secondly on the event of a button click (double click button) simply create an instance of the form you wish to call and Show() it:

 

Dim theForm as new FormNameHere()

theForm.Show()

you may wish to hide() the current form:

 

Me.Hide()

 

does this help

 

remember, if you are going to save your score/mark grades, you would need a way to hold your answers/marking scheme either by perhaps writing to a file everytime (ineffecient) or storing it and adding/subtracting values as you go along.

The latter can be achieved by passing the values from one form to another or using a module class where anyone can access it. This of course perhaps would be a bit too advanced for you



 
 
Aerouk





PostPosted: Visual Basic Express Edition, Visual Basic windows forms Top

Thats excellent!

Form 1 closes and form 2 opens in its-place. Brilliant, thank you

As for passing values between forms, could you possibly enlighten me to how this could be achieved please


 
 
ahmedilyas





PostPosted: Visual Basic Express Edition, Visual Basic windows forms Top

Sure. Firstly, form1 does not close, it's hidden - there is a difference ;-)

To pass one value to another, I guess a way would be to make a public property on each form, getting and setting the values. Example:

 

dim theScore as Integer = 0

public property MyProperty() As Integer

get

   return Me.theScore

end get

 

set(byval value as Integer)

   Me.theScore = value

end set

end property

 

this would be on top of each form, just after your "class" statement/decleration. What this does is creates a property. Property exposes itself to other classes so they can access it and get or set values appropriately.

in this case, we are making a property of type Integer.

So now when you are accessing each form you would do this to SET the score:

 

Dim theForm as New FormNameHere()

theForm.MyProperty = ScoreHere

theForm.Show()

Me.Hide()

 

Remember to ADD/Subtract the score appropriately depending on the users choice in your program, and the way to do that would be:

 

Me.MyProperty = Me.MyProperty + Score

for example.

 

You could also pass the variables through the constructor of the Class and setting them, similar to above but perhaps this maybe a bit advanced for you

To do this, you would again declare a local variable, just after your class decleration:

 

public class Form

   Dim theScore as Integer = 0

..

..

public sub New(ByVal score as Integer)

   Me.theScore = score

   ....

end sub

 

and you would do the same thing when adding score etc...

Now when you create an instance of a form, you would pass it a score:

 

Dim theForm as new FormName(score)

theForm.Show()

Me.Hide()

 

hope this helps.



 
 
Aerouk





PostPosted: Visual Basic Express Edition, Visual Basic windows forms Top

Thanks a bundle once again.

I'll be back with more questions at some point!

Cheers

 

(just fyi, working thru all the above now)


 
 
Aerouk





PostPosted: Visual Basic Express Edition, Visual Basic windows forms Top

hey,

Just to clarify something;

Dim theForm as New FormNameHere()

theForm.MyProperty = ScoreHere

theForm.Show()

Me.Hide()

The italiced line, "theForm", would that be the previous form to the current one, or current form

thanks again!


 
 
ahmedilyas





PostPosted: Visual Basic Express Edition, Visual Basic windows forms Top

no worries, glad I could help. We all learn from the bottom working up!

 
 
Aerouk





PostPosted: Visual Basic Express Edition, Visual Basic windows forms Top

Ooops, I'd hidden a question in there I think you missed, sorry.

Dim theForm as New FormNameHere()

theForm.MyProperty = ScoreHere

theForm.Show()

Me.Hide()

The italiced line, "theForm", would that be the previous form to the current one, or current form

Better shoot off to bed now, its 4am here (sunny England).

Will be looking tomorrow, thanks again.


 
 
ahmedilyas





PostPosted: Visual Basic Express Edition, Visual Basic windows forms Top

that would be the form you are going to show, which you have instantiated.

for example I have 2 forms:

FormA

FormB

I am now in FormA, I now want to show FormB when the user clicks "Next":

Dim theForm as New FormB()

theForm.MyProperty = 1 'Example

theForm.Show() 'will show FormB since "theForm" is an instance of FormB

does this clarify it better




 
 
Aerouk





PostPosted: Visual Basic Express Edition, Visual Basic windows forms Top

hi again,

hit more problems today

here is my form number 3.
basically I need to take the score, which would depend on one of the answers on the previous form, add to it if certain answers were put into the textbox and then pass it onto the next form.

I'm pretty lost now!

Form3:

Public Class Numeracy3
Dim CurrentScore As Integer = 0

Public Property Score() As Integer

Get

Return Me.CurrentScore

End Get

Set(ByVal value As Integer)

Me.CurrentScore = value

End Set

End Property
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

End Sub

Private Sub Continue3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Continue3.Click
Dim Form4 As New Numeracy4()
Form4.Score = CurrentScore
Form4.Show()
Me.Hide()
End Sub

Private Sub Q2A1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Q2A1.TextChanged
Dim answer As String
If Q2A1_TextChanged() = Str(kite, trapezium) Then Score(+1)


End If
End Sub
End Class


 
 
ahmedilyas





PostPosted: Visual Basic Express Edition, Visual Basic windows forms Top

yes I can see! I believe the problem is in your textchanged event.

it should be.....

If Q2A1.Text.Length > 0 then

if Q2A1.Text.Equals(yourTextValueHere) then

Me.CurrentScore = Me.CurrentScore + 1

end if

end if

otherwise everything else looks ok



 
 
Aerouk





PostPosted: Visual Basic Express Edition, Visual Basic windows forms Top

hey, thanks again!

What if I need any of two or three answers That said, I've got an error.. heres the code ive got so far:

Private Sub Q2A1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Q2A1.TextChanged

If Q2A1.Text.Length > 0 Then

If Q2A1.Text.Equals(kite) Then

Me.CurrentScore = Me.CurrentScore + 1

End If

Error: kite is not declared.


 
 
ahmedilyas





PostPosted: Visual Basic Express Edition, Visual Basic windows forms Top

kite should be in quotes - anything to do with strings should be in quotes:

"kite"

if you need to do several conditional checks, as you said, there could be several answers to 1 question, you would do this:

if condition then

'do stuff

else if condition then

'do stuff

else

'conditions don't meet therefore do something

end if

in your case:

if Me.txt.Text.Equals("kite") then

'do something such as add 1 to the score

else if Me.txt.Text.Equals("Balloon") then

' do something else - again add 1 to score

end if

Remember, inputs are case sensitive so you have to make sure that inputs are of the same case as what you are going to compare with ("Kite" is different than "kite")

A better way to reduce code would be to do this:

if Me.txt.Text.ToLower().Equals(value).ToLower() = true OR Me.txt.Text.ToLower().Equals(anotherValue).ToLower() = true then

'Answer given is correct so add 1 to score

else

'this else statement is optional but will be executed when the answer given is incorrect

end if



 
 
Aerouk





PostPosted: Visual Basic Express Edition, Visual Basic windows forms Top

thank you (yet again)

Going onto the final page which displays the users name and total score, how can I pick up the score and display it in a simple label, in this case, Label2

Public Class Numeracy8

Dim CurrentScore As Integer = 0

Public Property Score() As Integer

Get

Return Me.CurrentScore

End Get

Set(ByVal value As Integer)

Me.CurrentScore = value

End Set

End Property

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Close()

End Sub

Private Sub Numeracy8_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Me.Score = CurrentScore

End Sub

Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click

End Sub

End Class


 
 
ahmedilyas





PostPosted: Visual Basic Express Edition, Visual Basic windows forms Top

on your form load, instead of "Me.Score" - that should be removed as you are re-assigning the value back :-)

it should be:

Me.Label2.Text = Me.Score.ToString()

this will get the score and display it on the label.