Let say i am creating a math calculator.
One
Two
When i highlight One and Two and click Sum. It will add one and two together (must be store in a variable)
Printable View
Let say i am creating a math calculator.
One
Two
When i highlight One and Two and click Sum. It will add one and two together (must be store in a variable)
Erm, you have to get the item selected (Not sure about the code exactly but this should roughly be correct.)
Add this code into the Calculate button:
Should work, give it a try.Code:MessageBox.Show(listBox1.SelectedItem + listBox2.SelectedItem & " is the calculated answer");
Is this correct?
If i declare a variable called 'One' and a variable called 'Two'.
Example:
Const One as Integer = 1
Const Two As Integer = 2
One = listBox1.SelectedItem
Two = listBox2.SelectedItem
lblShow.Text = One.ToString + Two.ToString
I guess there is some errors there?
Yea what Daney said works aswell, But if you want them stored into Variables, it looks like this:
Its pretty self explanatory.Quote:
Public Class Form1
Dim num1 As Integer
Dim num2 As Integer
Dim answer As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
num1 = ListBox1.SelectedItem
num2 = ListBox2.SelectedItem
answer = num1 + num2
Label1.Text = answer
MessageBox.Show(ListBox1.SelectedItem + ListBox2.SelectedItem & " is the calculated answer")
End Sub
End Class
We get the currently selected item from listbox1 and place it into num1
Then do the same for listbox2 but place the item value into num2
Now we declare another variable called answer and store the result of adding num1 + num2 together.
Then finally we either make a new label and place the value of answer
into its Text property or do what Daney did and make a message box with the answer.
We'd have to change some of the syntax inside the message box to:
Removing the Listbox1.SelectedItem and Listbox2.SelectItem from the syntax and replacing it with our answer variable.Quote:
MessageBox.Show(answer & " is the calculated answer")
Thanks for your reply. Hmm,
let say each listbox have 5 items
Example
Public Class Form1
' ListBox1
Dim num1 As Integer
Dim num2 As Integer
Dim num3 as integer
Dim num4 As Integer
Dim num5 As Integer
' ListBox2
Dim num6 As Integer
Dim num7 As Integer
Dim num8 as integer
Dim num9 As Integer
Dim num10 As Integer
Dim answer As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
num1 = ListBox1.SelectedItem = 1
num2 = ListBox1.SelectedItem = 2
num3= ListBox1.SelectedItem = 3
num4 = ListBox1.SelectedItem = 4
num5 = ListBox1.SelectedItem = 5
num6 = ListBox2.SelectedItem = 1
num7 = ListBox2.SelectedItem = 2
num8= ListBox2.SelectedItem = 3
num9 = ListBox2.SelectedItem = 4
num10 = ListBox2.SelectedItem = 5
Is this correct? What are the best way to shorten all this steps ? Using Array?
Variable can be stored in RAM, variable value can be showed in textbox, listbox, shitbox...
You just need to DIM two vairables, what ever item is selected in listbox1 gets put into the variable.
Can you give more detail to what type of program you are making now?
If its a calculator I'd suggest ditching List Boxes for Text Boxes, so that the use can input any number and add them together.