• Help Making a Calculator in VB
    7 replies, posted
As you can probably guess, I'm new to programming and I am teaching myself VB for my Computing course next year. I'm currently working on a calculator project and everthing is fine until the decimal. Basically I need to ask, if the textbox contains a decimal then don't display another one. My problem is the 'contains' bit. How would I ask if it contains a certain character because at the moment I only know to ask if it is equal. Sorry if this is a bit vague and thanks in advance.
I just went through the same thing for my windows mobile calculator here's my code(in C#, but you should be able to figure out the jist). [code] if(temp.IndexOf('.')==-1) temp += "."; [/code] temp is my string version of the number that shows up on the screen. Make sure you aren't casting your string to a double after every button press though, because it will take your decimal off since it thinks 4. is redundant.
In VB6 it's [code] If InStr(Whatever, ".") = 0 Then ' Whatever has no dot End If [/code]
For VB.NET? [code]If TextBox.Text.Contains("Text") Then Do Something? Else Do moar? End If[/code]
Thanks a lot Zayfox, that's exactly what I needed. I ended up with this code: [code] Private Sub BtnDec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDec.Click If Display.Text.Contains(".") Then Display.Text = Display.Text Else Display.Text = Display.Text & "." End If End Sub[/code] Thanks! [editline]12:32PM[/editline] Ok I have antoher question, not related to the project but to the 'contains' thing. How would you ask if it doesn't contain something? I can't find a way to do negatives anywhere.
I think in vb you'd use If Not Display.Text.Contains(".") Then...
[QUOTE=walpoles93;16094481]Thanks a lot Zayfox, that's exactly what I needed. I ended up with this code: [code] Private Sub BtnDec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDec.Click If Display.Text.Contains(".") Then Display.Text = Display.Text Else Display.Text = Display.Text & "." End If End Sub[/code] Thanks! [editline]12:32PM[/editline] Ok I have antoher question, not related to the project but to the 'contains' thing. How would you ask if it doesn't contain something? I can't find a way to do negatives anywhere.[/QUOTE] In If statements, if you want to check for a false response, you stick "Not" infront of it. So it would be this: [code] Private Sub BtnDec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDec.Click If Not Display.Text.Contains(".") Then 'There were no decimals Display.Text = Display.Text Else 'There was a decimal Display.Text = Display.Text & "." End If End Sub[/code]
It's not using the "Not" function. It's actually this: [code] If TextBox.Text.Contains("Text") = False Then What to do when it doesn't contain "Text" Else What to do when it does contain "Text" End If [/code] Tested and working.
Sorry, you need to Log In to post a reply to this thread.