Results 1 to 10 of 10

Thread: clear function

  1. #1

    Thread Starter
    Registered User
    Join Date
    Sep 2009
    Location
    England
    Posts
    112

    Exclamation 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

  2. #2
    Addicted Member
    Join Date
    Jun 2009
    Posts
    245

    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

  3. #3
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: clear function

    I would have thought it would just be:
    vb Code:
    1. Message = String.Empty
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  4. #4
    Hyperactive Member
    Join Date
    May 2005
    Posts
    307

    Re: clear function

    Try Checkbox.Checked and not CheckState ?

  5. #5
    Hyperactive Member
    Join Date
    May 2005
    Posts
    307

    Re: clear function

    Nevermind, i think i misread your post... sorry

  6. #6

    Thread Starter
    Registered User
    Join Date
    Sep 2009
    Location
    England
    Posts
    112

    Re: clear function

    tftggfvggvcgcv
    Last edited by stuart1512; Oct 20th, 2009 at 05:47 AM.

  7. #7

    Thread Starter
    Registered User
    Join Date
    Sep 2009
    Location
    England
    Posts
    112

    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

  8. #8
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: clear function

    i have a new problem
    Create a new thread then
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  9. #9

    Thread Starter
    Registered User
    Join Date
    Sep 2009
    Location
    England
    Posts
    112

    Re: clear function

    sorry, sorted it out

  10. #10
    New Member
    Join Date
    Nov 2010
    Posts
    1

    Red face 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
  •  



Click Here to Expand Forum to Full Width