Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Need help with a little VB

Jason

Awesome Bro

So basically, since I've had the whole week off, I haven't done any of my coursework, left it all till the last day (Today, haha), and I'm in the middle of coding one of the programs for an assignment, which so far works fine, except for one error that I'm trying to avoid, basically there are three text boxes, and a button, what I'm trying to do is disable the button until all three text boxes have something inside them.

So far I've tried this;

Code:
 

        If txtPrincipal.Text = "" And txtTerm.Text = "" And txtInterestRate.Text = "" Then

            cmdCalculate.Enabled = False

        Else

            cmdCalculate.Enabled = True

        End If

 

Now, when I start it up, the button is disabled all fine and dandy, but when I enter values into the three text boxes, the button does nothing, it just stays disabled...

Any help would be nice on this,

Cheers.
 

jneul

Member

oh it been ages since i used VB, but have you tried the String.Compare function
with reference to http://msdn.microsoft.com/en-us/library ... 80%29.aspx

Edit - also the code should be in the event handler for the TextChanged or something like that, like i said i can't remember exactly as has been a while now since I used VB, but i know c# has an event handler for TextChanged on Text Boxes, so there you go
The reason being is every time you change the text in the text box the TextChanged event gets triggered, so you should perform your checks there.
 

Jason

Awesome Bro

Hmm I'm afraid I don't quite understand how I'd use it in VB o.O

I mean, String.Compare, as it says on the link, returns an integer, but what I need it to do, is when all three text boxes contain something, I need a button to switch from disabled to enables, and visa versa.
 
You need to specify some things. :D Here you go!

Code:
    Private Sub TextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged

 

        If Me.TextBox1.Text = "" And Me.TextBox2.Text = "" And Me.TextBox3.Text = "" Then

            Me.Button1.Enabled = False

        ElseIf Me.TextBox1.Text <> "" And Me.TextBox2.Text <> "" And Me.TextBox3.Text <> "" Then

            Me.Button1.Enabled = True

        End If

 

End Sub
 

Jason

Awesome Bro

Well I'm still getting the same results o.O

Here, I'll just post all the code to see if you can understand it, lol;

Code:
 

Public Class Form1

 

    Dim Principal As Single

    Dim Term As Byte

    Dim InterestRate As Single

 

    Dim Amount As Single

    Dim TotalInterest As Single

 

    Dim n As Short

 

    Private Sub cmdCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCalculate.Click

 

        lstInterest.Items.Clear()

        Principal = txtPrincipal.Text

        Term = txtTerm.Text

        InterestRate = txtInterestRate.Text

 

        Amount = Principal

 

        For n = 1 To Term

            Amount = Amount + (Amount * InterestRate / 100)

            lstInterest.Items.Add(n & " " & "-" & " " & "£" & Format(Amount, "0.00"))

        Next n

        TotalInterest = Amount - Principal

 

        txtResult.Text = "£" & Format(Amount, ".00")

        txtInterest.Text = "£" & Format(TotalInterest, ".00")

 

    End Sub

 

    Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click

 

        txtPrincipal.Text = ""

        txtTerm.Text = ""

        txtInterestRate.Text = ""

        txtResult.Text = ""

        txtInterest.Text = ""

        lstInterest.Items.Clear()

 

    End Sub

 

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

 

        If txtPrincipal.Text = "" And txtTerm.Text = "" And txtInterestRate.Text = "" Then

            cmdCalculate.Enabled = False

        ElseIf txtPrincipal.Text <> "" And txtTerm.Text <> "" And txtInterestRate.Text <> "" Then

            cmdCalculate.Enabled = True

        End If

 

        'If txtPrincipal.Text = "" And txtTerm.Text = "" And txtInterestRate.Text = "" Then

        ' cmdCalculate.Enabled = False

        'Else

        'cmdCalculate.Enabled = True

        'End If

 

    End Sub

 

End Class

 

 

In Form1_Load, I've commented out my code and left your code in.
 

jneul

Member

^^ RadethDart GOOD EXAMPLE
That is exactly what i meant by the TextChanged event handler, I just did not want to do all the hard work for him, as sometimes you only learn by doing it yourself, you don't often learn by copying other peoples code
Oh by the way the integer the String.Compare function returns tells you whether the two strings are equal or not if i remember right if the result is 0 than the strings are equal so do somethig like this in your event handler. of course what you are doing should be fine, just put it in the event handler and you will see, i am so used to having to compare strings because i use c#, and c# is very different.

If [String].Compare(textBox.Text, "") = 0
button.Enabled = False
Else
button.Enabled = True

Edit:- by the looks of your code you have posted you have not put your checks in the TextChanged event handler that
RadethDart had an example on, you need to change your code so your checks go in the TextChanged event handler and then the program will work as intended
 
Haha, sorry about that, I didn't know you wanted him to find out how to do it himself. :D BTW: Nice job on that shortening of the code. :D
 

jneul

Member

no im not shortening the code lol :biggrin: , i'm probably just confusing him more lol, it's just i'm so used to having to compare strings so i guess it's what i do now
 

Jason

Awesome Bro

Yes, yes you are confusing me...

So basically what you're saying is, there's no simple way for someone like me to do what I want to do ?
 

jneul

Member

noo you got me all wrong. sorry.
you see the event handler RadethDart did yes?? well just pretty much copy that and change the name of the text boxes he has used to your text boxes, and of course the button to your button name, i even have highlighted all the things you need to change in red.

of course this won't help you understand what is going on much.

I suggest you read this, I apologise if it is not very helpful but microsoft's description of how to do things well can be really un-helpful http://msdn.microsoft.com/en-us/library ... anged.aspx

# Private Sub TextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged
#
# If Me.TextBox1.Text = "" And Me.TextBox2.Text = "" And Me.TextBox3.Text = "" Then
# Me.Button1.Enabled = False
# Else
# Me.Button1.Enabled = True
# End If
#
# End Sub

oh and by the way i also am on PSN, sorry if I have come across wrong and i hope you sort this out, and also gain an understanding of it as well.

let me know if you keep on having problems ok :biggrin:
 
Yeah, that is what I enabled you to do, I was to lazy to change my variables to what you were using. :D Sorry about that. I hope that code helps you, jъГìsт.
 

jneul

Member

you was not being lazy, like i said it helps if he does some of the work, that way hopefully he will gain an understanding
don't put yourself down :biggrin:
 
Here is a faster way. Compairing strings takes more memory

# Private Sub TextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged
#
# If Me.TextBox1.Lenght > 0 And Me.TextBox2.Lenght > 0 And Me.TextBox3.Lenght > 0 Then
# Me.Button1.Enabled = False
# Else
# Me.Button1.Enabled = True
# End If
#
# End Sub
 

jneul

Member

Nice and yes you are right comparing strings does take more memory
yeah we was just giving examples not trying to show him the best way to do things :smile: ^^

Lenght should be spelt Length by the way otherwise it will not work
Also i found another error in the code you supplied it should be this way, as the button should ony be enabled when there is text in all the text boxes

# Private Sub TextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged
#
# If Me.TextBox1.Length > 0 And Me.TextBox2.Length > 0 And Me.TextBox3.Length > 0 Then
# Me.Button1.Enabled = True
# Else
# Me.Button1.Enabled = False
# End If
#
# End Sub
 
Yep, just shows that I need sleep. Lol didn't even see that the booleans were backwards. Also that wasn't the best way still.


# Me.Button1.Enabled = (Me.TextBox1.Length > 0 And Me.TextBox2.Length > 0 And Me.TextBox3.Length > 0)

I think anyhow. Not sure if VB can handle that, it's been years since I've played with VB.
 

jneul

Member

hmm well there is only one way to see, try it, lol us programmers make the most funniest of mistakes when we are tired :biggrin:
 
They seem to be over complicating this a bit.

An if statement is a control structure used to alter the flow of the code based upon a Boolean value you provide it. In this case, your Boolean value is evaluated by the following equation, which I've changed a bit:
txtPrincipal.Text.Length > 0 AndAlso txtTerm.Text.Length > 0 AndAlso txtInterestRate.Text.Length > 0

Since you're wanting all conditions to be true in order to enable the command button, AndAlso ensures that it only checks them as far as it needs, if the first is false, then it stays disabled, and doesn't check whether the other two have text, since it requires all three. This process of only evaluating as far as needed, is called short-circuiting.

Since you're assigning the Enabled property to a Boolean value, you can omit the control structure entirely:
cmdCalculate.Enabled = txtPrincipal.Text.Length > 0 AndAlso txtTerm.Text.Length > 0 AndAlso txtInterestRate.Text.Length > 0

The examples they've given you aren't adjusted to use the names you've provided, so here's an example that brings event handlers and short-circuiting together:

Code:
 

[color=blue]Private Sub[/color] CalculateField_TextChanged([color=blue]ByVal[/color] sender [color=blue]As Object[/color], [color=blue]ByVal[/color] e [color=blue]As[/color] [color=gray]System.[/color][color=purple][b]EventArgs[/b][/color]) [color=blue]Handles[/color] txtTerm.TextChanged, txtPrincipal.TextChanged, txtInterestRate.TextChanged

    [color=blue]Me[/color].cmdCalculate.Enabled = [color=blue]Me[/color].txtPrincipal.Text.Length > [color=red]0[/color] [color=blue]AndAlso[/color] [color=blue]Me[/color].txtTerm.Text.Length > [color=red]0[/color] [color=blue]AndAlso[/color] [color=blue]Me[/color].txtInterestRate.Text.Length > [color=red]0[/color]

[color=blue]End Sub[/color]

 
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top