IF statement not working as I expected - vb.net

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

Related

Use a textbox to store a numeric counter in 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"

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

how do i save time in datagridview in vb.net 2010 [Newbie student]

Example i press start time and the time connected in textbox that it will show when i start the time same with out time,and when i press save it wont add to datagridview. Please help me with my problem. This is my save code:
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Table1BindingSource.AddNew()
Me.Validate()
Me.Table1BindingSource.EndEdit()
Me.Table1TableAdapter.Update(Me.OpenDataSet.Table1)
End Sub
Ok, I solved your problem....
Here is how your form should look like :
And now you need to add the "Time" columns to the datagridview:
And now... follow my steps through the image:
and here is the code :
Public Class Form1
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Table1BindingSource.Rows.Add()
Table1BindingSource.Rows(Table1BindingSource.Rows.Count - 1).Cells(0).Value = TextBox1.Text & Table1BindingSource.Rows.Count
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CountTimer.Tick
TextBox1.Text += 1
End Sub
End Class
I Hope My Answer Was Useful To You :)

Get Own control values in visual basic 10

I'm looking for a way to get the data of control (in this case a Trackbar) inside the trackbar itself.
I got this:
Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
Label1.Text = TrackBar1.Value
End Sub
But I want to replace that "trackBar1." with something like
Label1.Text = THIS_CONTROL.Value
Is this possible in Vb10 ?
Thanks
Use the sender argument:
Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
Dim THIS_CONTROL As TrackBar = DirectCast(sender, TrackBar)
Label1.Text = THIS_CONTROL.Value
End Sub

Adding Multiple Items From One Listbox to Another VB.Net

I am able to only move single items from one listbox to another with this code. I tried with both MultiSimple & MultiExtended SelectionMode.
How do I select multiple items and then move them?
Private Sub cmdAdd_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs
) Handles cmdAdd.Click
Dim i As Integer = Listbox1.SelectedIndex
If i = -1 Then
Exit Sub 'skip if no item is selected
End If
Listbox2.Items.Add(Listbox1.Items(i))
Listbox1.Items.RemoveAt(i)
End Sub
You need to use SelectedIndices or SelectedItems.
Private Sub cmdAdd_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs
) Handles cmdAdd.Click
Dim selectedItems = (From i In ListBox1.SelectedItems).ToArray()
For Each selectedItem In selectedItems
Listbox2.Items.Add(selectedItem)
Listbox1.Items.Remove(selectedItem)
Next
End Sub
Note the use of a Linq query to get list of selected items as an array. Using an array is required to prevent "Collection changed" exceptions. You may need to add a reference to System.Linq.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ComboBox1.Items.Add("SanDiego")
ComboBox1.Items.Add("BeverlyHills")
ComboBox1.Items.Add("Florida")
ComboBox1.Items.Add("NewYork")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim s As String
s = ComboBox1.SelectedItem
ListBox1.Items.Add(s)
ComboBox1.Items.Remove(s)
End Sub
Private Sub cmdAdd_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs
) Handles cmdAdd.Click
For Each selectedItem In (From i In ListBox1.SelectedItems).Tolist()
Listbox2.Items.Add(selectedItem)
Listbox1.Items.Remove(selectedItem)
Next
End Sub