|
-
Oct 20th, 2009, 05:20 AM
#1
Thread Starter
Registered User
clear function
im learning VB.Net
i have a program with checkboxes in a form. i have a button that outputs a message box with the choices i have ticked on the checkboxes. the button shows the ones i select but also the previous ones. i have another button "clear" but i dont know how to remove the selections from the messagebox
here is my code
Public Class Form1
Dim Message As String
Dim Counter As Integer = 0
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If CheckBox1.CheckState = 1 Then
Message = Message & CheckBox1.Text & vbNewLine
Counter = Counter + 1
End If
If CheckBox2.CheckState = 1 Then
Message = Message & CheckBox2.Text & vbNewLine
Counter = Counter + 1
End If
If CheckBox3.CheckState = 1 Then
Message = Message & CheckBox3.Text & vbNewLine
Counter = Counter + 1
End If
If CheckBox4.CheckState = 1 Then
Message = Message & CheckBox4.Text & vbNewLine
Counter = Counter + 1
End If
If CheckBox5.CheckState = 1 Then
Message = Message & CheckBox5.Text & vbNewLine
Counter = Counter + 1
End If
MsgBox("You have chosen" & vbNewLine & Message)
MsgBox("Counter = " & Counter)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
End Sub
End Class
thanks in advance
-
Oct 20th, 2009, 05:27 AM
#2
Addicted Member
Re: clear function
Hm, if I understand you correctly, all you have to do is set your message-string to nothing and your counter to zero.
Just put something like this in your Button2.Click-event:
Code:
Message = Nothing
Counter = 0
-
Oct 20th, 2009, 05:27 AM
#3
Re: clear function
I would have thought it would just be:
-
Oct 20th, 2009, 05:28 AM
#4
Hyperactive Member
Re: clear function
Try Checkbox.Checked and not CheckState ?
-
Oct 20th, 2009, 05:29 AM
#5
Hyperactive Member
Re: clear function
Nevermind, i think i misread your post... sorry
-
Oct 20th, 2009, 05:33 AM
#6
Thread Starter
Registered User
Last edited by stuart1512; Oct 20th, 2009 at 05:47 AM.
-
Oct 20th, 2009, 05:37 AM
#7
Thread Starter
Registered User
Re: clear function
i have a new problem, i need to put a case statement into the program to test how many boxes are checked and display a message for each one ie if they select 5 of the boxes put PRO in a messagebox
-
Oct 20th, 2009, 05:46 AM
#8
Re: clear function
Create a new thread then
-
Oct 20th, 2009, 05:55 AM
#9
Thread Starter
Registered User
-
Nov 23rd, 2010, 02:35 PM
#10
New Member
Re: clear function
I'm new to VB too and I am guessing you were working through the tutorial here http://www.homeandlearn.co.uk/net/nets4p14.html
I also was stuck there but solved it after reading this thread (thanks to Chris128 mostly). Anyway below is what I did to finally achieve what the tutorial was asking
Public Class Form1
Dim message As String
Dim counter As Integer = 0
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If CheckBox1.CheckState = 1 Then
message = message & CheckBox1.Text & vbNewLine
counter = counter + 1
End If
If CheckBox2.CheckState = 1 Then
message = message & CheckBox2.Text & vbNewLine
counter = counter + 1
End If
If CheckBox3.CheckState = 1 Then
message = message & CheckBox3.Text & vbNewLine
counter = counter + 1
End If
MsgBox("You have chosen " & vbNewLine & message)
MsgBox("You have chosen " & counter & " soaps")
message = String.Empty
Select Case counter
Case 0
MsgBox("not eating")
Case 1
MsgBox("not that hungry")
Case 2
MsgBox("pig")
Case 3
MsgBox("very piggy")
End Select
counter = 0
End Sub
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|