Textbox and button help please

replacing text in a textbox

Page 1 of 1

7 Replies - 2062 Views - Last Post: 20 October 2012 - 06:32 AM Rate Topic: -----

#1 newsledder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 05-November 09

Textbox and button help please

Posted 05 November 2009 - 10:09 PM

Hi.....I am working on a project and keep running into road blocks and I really need some help...I have spent hours searching google and am not finding what I need and I know it has got to be so simple i am just missing it.

Keep in mind I am very much so a beginner.....

I am making a simple little Vending Machine.. if you picture the keypad on most vending machines you will get the idea of what I am trying to build.....I have selection buttons 2 letters and 2 numbers. "A, B, 1, 2"

I have a textbox that as you push each button will display your selection.....I am having a problem with the numbers..the first "letter" selection is easy and if you push the letter A and then hit B it will just replace the text in the box.....what I am not able to figure out is how to make it do the same thing for the numbers...the second entry. here is my code.....



Private Sub Abutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Abutton.Click
	  SelectionText.Text = "A"
   End Sub

   Private Sub Bbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bbutton.Click
	  SelectionText.Text = "B"
   End Sub



   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
	  If String.IsNullOrEmpty(SelectionText.Text) Then
		 MsgBox("Please select A-F first")
		 SelectionText.Text = ""
	  Else
		 SelectionText.Text = SelectionText.Text & "1"
	  End If
   End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
	  If String.IsNullOrEmpty(SelectionText.Text) Then
		 MsgBox("Please select A-F first")
		 SelectionText.Text = ""
	  Else
		 SelectionText.Text = SelectionText.Text & "2"
	  End If
   End Sub






I dont want to be able to keep pushing one of the number buttons and keep adding digits to the end of what is there already...


Thanks for any help!

This post has been edited by newsledder: 05 November 2009 - 10:11 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Textbox and button help please

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1637
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Textbox and button help please

Posted 05 November 2009 - 10:14 PM

Well if you look at your button click events it's doing exactly what you're telling it to do. You're saying if I click A Button then set the text of the TextBox to "A", and same with the B button. You need to add the value to the end of the value in the TextBox when your button is clicked, take a look at this

Private Sub Abutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Abutton.Click
	If Not String.IsNullOrEmpty(SelectionText.Text) Then
		SelectionText.Text &= "A"
	Else
		SelectionText.Text = "A"
	End If  
End Sub

Private Sub Bbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bbutton.Click
   If Not String.IsNullOrEmpty(SelectionText.Text) Then
		SelectionText.Text &= "B"
	Else
		SelectionText.Text = "B"
	End If  
End Sub



Hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#3 newsledder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 05-November 09

Re: Textbox and button help please

Posted 05 November 2009 - 10:22 PM

The Abutton and Bbutton work just fine....they do what I want them to do....the button1 and button2 dont. they continue to add numbers to the end of what ever is in the box.....If i push A1 the textbox will display "A1" but if I change my mind and push "2" it will now read "A12" I want it to replace the "1" with a "2" and now read "A2".

This post has been edited by newsledder: 05 November 2009 - 10:33 PM

Was This Post Helpful? 0
  • +
  • -

#4 dzone41  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 216
  • Joined: 21-August 09

Re: Textbox and button help please

Posted 06 November 2009 - 06:06 AM

I would test for the length of the string in addition to what you are already doing. "If" the string already has 2 characters when the number is pressed, then it should use string manipulation methods to replace the last character.
Dim strSelection As String = Selection.Text
If Len(strSelection) = 2 Then
	 strSelecetion = Microsoft.VisualBasic.Left(strSelection, 1)
End If
strSelection &= "1"


This will have to be manipulated to fit into your code block, the principal is the same.
This code checks for the Length using Len(String), then "If" the string has 2 characters, keeps the Leftmost character, then add the number to it according to the specific button press.
Hope that helps
Was This Post Helpful? 1
  • +
  • -

#5 newsledder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 05-November 09

Re: Textbox and button help please

Posted 06 November 2009 - 06:46 AM

View Postdzone41, on 6 Nov, 2009 - 05:06 AM, said:

I would test for the length of the string in addition to what you are already doing. "If" the string already has 2 characters when the number is pressed, then it should use string manipulation methods to replace the last character.
Dim strSelection As String = Selection.Text
If Len(strSelection) = 2 Then
	 strSelecetion = Microsoft.VisualBasic.Left(strSelection, 1)
End If
strSelection &= "1"


This will have to be manipulated to fit into your code block, the principal is the same.
This code checks for the Length using Len(String), then "If" the string has 2 characters, keeps the Leftmost character, then add the number to it according to the specific button press.
Hope that helps



You were very helpful thank you!

I had to slightly modify the code but it works how I want it now thanks again!


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
	  Dim Selection As String = SelectionText.Text

	  If String.IsNullOrEmpty(SelectionText.Text) Then
		 MsgBox("Please select A or B first")
		 SelectionText.Text = ""
	  End If

	  If Len(Selection) = 2 Then
		 SelectionText.Text = Microsoft.VisualBasic.Left(SelectionText.Text, 1)
	  Else
		 SelectionText.Text &= "1"

	  End If

   End Sub



Was This Post Helpful? 0
  • +
  • -

#6 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: Textbox and button help please

Posted 06 November 2009 - 07:39 AM

Don't force the user to pick either the letter or the number first - start with the display string as "--" and then in the letter click handler substitute for the first character position, in the number click handler, use the second position. The vend enable is that the string does not contain a "-".
Dim currentText As String = "--"
Display.Text = currentText
...
'<button for number one>_Click handler
currentText(1) = "1"
Display.Text = currentText
...
'<button for letter A>_Click handler
currentText(0) = "A"
Display.Text = currentText
...
'vend_Click handler
If currentText.IndexOf("-") >= 0 Then
  ' we do nothave a valid string to vend
  Exit Sub
End If
' your vend code here


This makes your displayalways 2 characters long and order independent. You could even have a reset buttoin that would change the currentText back to "--"

This post has been edited by mark.bottomley: 06 November 2009 - 07:40 AM

Was This Post Helpful? 0
  • +
  • -

#7 Munkybones  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 31-October 09

Re: Textbox and button help please

Posted 07 November 2009 - 09:43 PM

The code to clear a textbox is
 textbox1.text = "" 
try using this in between the point where you set the value of the textbox to A and where the button being clicked is changing the char to B...this should clear the textbox and then continue the operation to add the B to the line
Was This Post Helpful? 0
  • +
  • -

#8 manos364  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 42
  • Joined: 09-October 12

Re: Textbox and button help please

Posted 20 October 2012 - 06:32 AM

Try this code:
If IsNumeric(TextBox1.Text) = True Then
   TextBox1.Text &= "1" 'or change to "2"
Else
   TextBox1.Text = "1" 'or change to "2"
End If


It's very simple and useful. Please tell me if you have any questions on the code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1