Use a textbox to store a numeric counter in VB.Net - vb.net

I want to count with a TextBox.
Here is my code:
Public Class Form1
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
OrainsProgressBar1.Increment(1)
If OrainsProgressBar1.Value = 100 Then
Timer3.Start()
Timer1.Stop()
End If
End Sub
Private Sub OrainsTheme1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrainsTheme1.Click
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Timer1.Start()
Timer2.Start()
End Sub
Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
OrainsProgressBar1.Increment(-1)
If OrainsProgressBar1.Value = 0 Then
Timer1.Start()
Timer3.Stop()
End If
End Sub
Private Sub OrainsButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrainsButton1.Click
OrainsTextBox1.Text += 100
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
OrainsTextBox1.Text += 1
End Sub
End Class
But I have an error with OrainsTextBox1.Text += 1. VB says:
'Conversion from string "" to type 'Double' is not valid.'
What is the problem?

In the .Net world, the data types of things matter a lot. Strings (like the .Text property) are NOT numbers. You need to convert. Even if someone only enters the digits 0-9 into the textbox, that's still a string of numeric characters, rather than a number. And what should happen if someone enters random text into that textbox that won't convert to a number type at all?
For this code, I suggest building a property, like this:
Private _orainsValue As Double
Public Property OrainsValue As Double
Get
Return _orainsValues
End Get
Set
_orainsValue = Value
OrainsTextBox1.Text = _orainsValue.ToString()
End Set
End Property
That will let you write code like this and have the expected result shown to the user:
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
OrainsVale += 1
End Sub
Note that it does mean you will want to mark the TextBox disabled,though, because this doesn't account for user data entry.

Instead of doing like this OrainsTextBox1.Text += 1
do like this OrainsTextBox1.Text = Val(OrainsTextBox1.Text) + 1
Because .Text is string. Which will append the 1s as "11111111"

Related

Replace string if followed by any character visual basic

I am new to Visual Basic. I want to use the like operator in a textbox to change a character if it is followed by any other character. But it should be on the key-up event.
Anyone please help me: how I can make the following code work?
Public Class Form1
Dim myString As String
Dim sMatch As Boolean = myString Like "x?"
Private Sub TextBox1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
If sMatch = True Then
TextBox1.Text = TextBox1.Text.Replace(myString, "z")
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myString = "x"
End Sub
End Class
Try this and ask. Your variable myString was never getting it's value from the TextBox. You just set it to x in the load event.
Public Class Form1
Private Sub TextBox1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
If TextBox1.Text Like "x?" Then
TextBox1.Text = TextBox1.Text.Replace(TextBox1.Text, "z")
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = "x"
End Sub
End Class

How to set limit values textbox and show message box when reached maximum limit VB.Net

Sorry for bad english.
I'm beginner in VB.Net, on this question I want to make textbox validation to show messagebox when maximum limit is reached.
below this code
Public Class Form1
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Dim i As Integer
TextBox1.MaxLength = 6
i = TextBox1.MaxLength
If TextBox1.Text.Length > i Then
MsgBox("Maximum is 6 Character")
End If
End Sub
End Class
In form load event set TextBox1.MaxLength = 6
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
TextBox1.MaxLength = 6
End Sub
and use the following code in TextBox1_KeyDown event
Private Sub TextBox1_KeyDown(ByVal sender As Object _
, ByVal e As System.Windows.Forms.KeyEventArgs _
) Handles TextBox1.KeyDown
If Trim(TextBox1.Text).Length = 6 Then
MsgBox("Maximum is 6 Character")
End If
End Sub
Or
Keep TextBox1.MaxLength as system default,if you use below code then no need to alter it's lenghth to 6
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Trim(TextBox1.Text).Length = 6 Then
e.Handled = True
MsgBox("Maximum is 6 Character")
End If
End Sub

How to add items from a MenuStrip event to a ListBox using a For Next Loop

That might have sounded weird so let me explain.
I have a school assignment that has me pulling my hair out. I have to get a collection of 5 facts and have them display to a ListBox using a For Next Loop. The user would use an InputBox to input the facts.
I dont know what to put in the For Next to fetch the string from the InputBox. I'm at my wits end and am falling behind.
Here is what I have so far
Public Class frmWWIIFacts
Private Property RemoveAt As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub AddFactToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddFactToolStripMenuItem.Click
Dim intFact As Integer
Dim strInputFact As String
strInputFact = InputBox("Do you want to add a fact?", "Add a fact")
For
Next
strInputFact = InputBox("Do you want to add a fact?", "Add a fact")
End Sub
Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
Application.Exit()
End Sub
Private Sub ClearListToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearListToolStripMenuItem.Click
lstFacts.Items.Clear()
End Sub
Private Sub RemoveFactToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveFactToolStripMenuItem.Click
End Sub
I've submitted a reddit post requesting some assistance but its gotten me nowhere. https://www.reddit.com/r/learnprogramming/comments/3t614u/vb2015_using_menustrip_to_addremove_items_in_a/
I would love some help on this. Please ask questions if your confused on my method or if you need to know more.
Sounds like you're trying to do this:
Private Sub AddFactToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddFactToolStripMenuItem.Click
Dim intFact As Integer
Dim strInputFact As String
lstFacts.Items.Clear()
For intFact = 1 To 5
strInputFact = InputBox("Please enter a fact:", "Add a fact")
If Not strInputFact = "" Then
lstFacts.Items.Add(strInputFact)
End If
Next
End Sub

IF statement not working as I expected

Hi so im playing with microsoft visual studio 2010 edition and I need to make an if statement work on it.
What im doing is putting a number into a textbox, it then goes into my listbox (however many times I put numbers in like 3-4 numbers). Then it is meant to add them all up and put it into the label after.
Heres the program so far
Public Class Form1
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim Result As Integer
For Each Item As Integer In ListBox1.Items
Result = Result + Item
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Value As String
Value = TextBox1.Text
ListBox1.Items.Add(Value)
End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
Label1.Text = "You have a Balance of " + DialogResult.ToString
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
If (String.a
End Sub
End Class
In Visual Basic .NET (which is what your code looks to be in), If statements are formatted like this:
If condition Then
DoSomeCodeHere()
End If
C# code is formatted as
if (condition)
{
DoSomeCodeHere();
}
Do you mean you wan the sum all the amount in the list box and show it in a label after clicking button 2?
If yes add the below should do.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim Result As Integer
For Each Item As Integer In ListBox1.Items
Result = Result + Item
Next
Label1.Text = "You have a Balance of " + Result.ToString
End Sub

how disable text box from two give text box?

I have 2 text box in one vb form. if txtMaterial was fill i want to disable the txtPackage and vice versa. I use the code below, but it didn't work. could someone fixed it.
Really appreciate it. tq.
Private Sub txtMaterial_TextChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMaterial.TextChanged
txtMaterial.Enabled = True
txtPackage.Enabled = False
End Sub
Private Sub txtPackage_TextChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPackage.TextChanged
txtPackage.Enabled = True
txtMaterial.Enabled = False
End Sub
Make both TextBoxes fire the same handler, then simply set the Enabled() state of each one based on whether the other TextBox has something in it:
Private Sub txtChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMaterial.TextChanged, txtPackage.TextChanged
txtMaterial.Enabled = (txtPackage.TextLength = 0)
txtPackage.Enabled = (txtMaterial.TextLength = 0)
End Sub
*Note the ending of the first line has both controls listed using Handles txtMaterial.TextChanged, txtPackage.TextChanged at the end.
Private Sub txtMaterial_TextChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMaterial.TextChanged
If txtMaterial.Text <> "" Then
txtPackage.Enabled =False
End If
End Sub
based on the comment that you added
Blockquote i want to make it disable as the second one. but in my case if i fill the txtMaterial the txtPackage also can be fill with text at the same time.
the solution to your problem must be this:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtPackage.Enabled = False
End Sub
Private Sub txtMaterial_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMaterial.TextChanged
If Not String.IsNullOrEmpty(txtMaterial.Text) Then
txtPackage.Enabled = True
Else
txtPackage.Enabled = False
End If
End Sub