EvilZone
Programming and Scripting => .NET Framework => : Code.Illusionist December 02, 2012, 04:37:17 PM
-
Ok, maybe it will sound wierd to ask this since I wrote the code down, but, I don't understand how it work. I am coding a testing application (for learning only) and the application ask several question one user, he asnwer in textbox1 by clicking one button. There is listbox1 that store those informations. Here is the image of application and the code:
(http://s17.postimage.org/okkkqhrp9/2012_12_02_163706.jpg)Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As String = Microsoft.VisualBasic.Left(Label1.Text, 2)
Dim username As String = TextBox1.Text
Dim age As String = TextBox1.Text
Dim job As String = TextBox1.Text
If x = "1." Then
ListBox1.Items.Add("Username: " & username)
Label1.Text = "2. How old are you?"
ElseIf x = "2." Then
ListBox1.Items.Add("Age: " & age)
Label1.Text = "3. Do you have job?"
ElseIf x = "3." Then
ListBox1.Items.Add("Job: " & job)
Label1.Text = "Great job."
End If
End Sub
End Class
(http://s17.postimage.org/cwqivy2kd/2012_12_02_163728.jpg)
As you can see, the code will work ok, I know that allready. But, If I just don't change the label1.text into another number, the listbox1 will store same value again. Or , if you don't understand what I wanted to say, can someone give me more simple example of application I did here. So I can learn better way to do programming. Thanks.
-
Images dosent work. Use some other uploading service.
If you have code, paste it into code tags here, dont take images of it.
-
Ok, post edited.
-
Okay, I see the problem or rather the.. Way you did it.
Basically you want to use the same button and textbox for various questions. And you use the label1's first two characters to determine what level/question you are on.
Now, you wanted an alternative way of doing this. Do it internally and skip the label all together. Do something like this:
Public Class Form1
Dim qLevel as integer = 0
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim answer As String = TextBox1.Text
If qLevel = 1 Then
ListBox1.Items.Add("Username: " & answer)
Label1.Text = "How old are you?"
qLevel += 1
ElseIf qLevel = 2 Then
ListBox1.Items.Add("Age: " & answer)
Label1.Text = "Do you have job?"
qLevel += 1
ElseIf qLevel = 3 Then
ListBox1.Items.Add("Job: " & answer)
Label1.Text = "Great job."
qLevel += 1
Else
' Msg box here? End of code?
End If
End Sub
End Class
I also changed the way you get your answers, you dont need one variable for each of the answers when the input is the same. Simply use one variable/input, i called it answer. You might want to look into switch() as well, it works very well in these cases.
-
Finaly I understand this. Thank you so much Ande. Yea, the idea to use several variables for same input is, I admit, stupid. Good you told me where i make errors (not like it wont work, just too much code for one simple thing). Thanks again.
EDIT: Just to remind you Ande that the starting point of qLevel should be 0, not 1, if it's 1, it can't start.
-
EDIT: Just to remind you Ande that the starting point of qLevel should be 0, not 1, if it's 1, it can't start.
Hehe yeah, you are entirely right ;)