Getting a result to do multiple things in VB.Net

Soldato
Joined
30 Dec 2004
Posts
4,681
Location
Bromley, Kent
As title really.
Code:
Public Class BetweenNumberAdd
    Dim number As Integer
    Dim firstnumber As Integer
    Dim startnumber As Integer
    Dim endnumber As Integer


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If startnumber = 0 Then
            Exit Sub

        End If
        If endnumber = 0 Then
            Exit Sub

        End If

        For firstnumber = startnumber To endnumber

            number = number + firstnumber

        Next firstnumber

        MessageBox.Show(number)

    End Sub

    Private Sub startnumberbox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startnumberbox.TextChanged
        startnumber = Val(startnumberbox.Text)

    End Sub

    Private Sub endnumberbox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles endnumberbox.TextChanged
        endnumber = Val(endnumberbox.Text)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        startnumberbox.Text = " "
        endnumberbox.Text = " "
        number = "0"
        firstnumber = "0"
        startnumber = "0"
        endnumber = "0"
    End Sub
End Class

That's my code at the moment which works for my all mighty number cruncher. I'm not looking to optimize or make it more efficient at the moment.

When the loop scans the values in box 1 and two, if the boxes are blank then it stops the loop, but what i want to happen is exactly this, but also have a message box pop up to say something like "first box empty" on the top box and "second box empty" on the second box, I can't seem to work out how to make the pop up appear in the same snippet of code where the value is checked due to it executing the Exit Sub command.

Hope that makes sense, if not just say.

Thanks all

- Pea0n
 
Many ways you could do it:
Check the values of the text boxes in the textchanged event and enable Button1 if they are both non zero.

Stick a validator control on and use that.

or as BiggglesPiP mentioned put

MessageBox.Show("NonZero")
just before the exit sub calls in you button1_click event.


You might want to check that endnumber > start number
Code:
        If endnumber.CompareTo(startnumber) < 0 Then
            MessageBox.Show("end number must be higher")
            Exit Sub
        End If

Simon
 
Last edited:
OK I have just had a go and unfortunately they don't seem to be what I'm after exactly, although I appreciate the replies.

What it is I need to do is with this bit:
Code:
  If startnumber = 0 Then
            Exit Sub

        End If
Basically, when the loop finds that the number is zero then I want it to only then cancel the subroutine and also display the error. At the moment the code cancels it, but it doesn't pop up the message box. I can of course modify it to make the pop up box, but then the subroutine won't cancel and its this that I can't seem to get both to do in one process. of course if both boxes match the criteria, i.e. first box is say 6 and second box is 13 then in theory nothing should pop up

Hope that makes sense, cheers!

- Pea0n
 
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If startnumber.equals(0) Then
            MessageBox.Show("StartNumber can't be zero")
            Exit Sub
        End If
        If endnumber.equals(0) Then
            MessageBox.Show("EndNumber can't be zero")
            Exit Sub
        End If
        If endnumber.CompareTo(startnumber) < 0 Then
            MessageBox.Show("End number must be higher")
            Exit Sub
        End If

        For firstnumber = startnumber To endnumber

            number = number + firstnumber

        Next firstnumber

        MessageBox.Show(number)

    End Sub

Oh and as your numbers are dim'd as integers your button2_click should probably be:
Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        startnumberbox.Text = string.empty
        endnumberbox.Text = string.empty
        number = 0
        firstnumber = 0
        startnumber = 0
        endnumber = 0
    End Sub
 
Last edited:
OK for a start, the string.empty command was one I was searching for the other day, perfect thanks :)

As for the other code, you sir are a genius, thanks for that.

Is there any recommended reading material for using the xxx.yyy.xxx.aaa.bbb definitions, i.e. string.empty.contains (random one, probably doesn't mean anything)? Like what they do, how to find one that does what you want and how to construct them and use them for getting data, preferably some kind of book I can purchase?

Thanks again!

- Pea0n
 
Everything you may need is on the internet! I use the MSDN site an awful lot. There are white papers and best practices documents.
The help examples and howto's in VS are good as well.
[string].contains() is very useful. string.empty.contains() not so much! :) Unless you really want to check that the empty string is empty!
[string] is any string such as textbox1.Text or myinteger.ToString so textbox1.Text.contains("bob") would return true or false.

Just another thought about your code. I know you wern't asking for optimization but you could remove the TextChanged events entirely and make some of your class level variables sub level instead.

Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim firstnumber As Integer = 0
Dim startnumber As Integer = 0
Dim endnumber As Integer = 0

Integer.TryParse(endnumberbox.Text, endnumber)
Integer.TryParse(startnumberbox.Text, startnumber)

If startnumber.Equals(0) Then
        MessageBox.Show("1")
        Exit Sub
End If


'etc
 
Last edited:
Back
Top Bottom