Click here to Skip to main content
15,895,740 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB
Public Class exam Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
Dim n As String = txt_theory.Text 'get i/p from user for no.of times 
Dim temp As String = txt_theoryent.Text 'get data from user
 Dim i As Integer 
For i = 1 To n 
lst_theory.Items.Add(CType(temp, String))
 Next i 
End Sub 

This is my code. For loop was not working. pls correct it. I am using 2 textbox, 1 listbox, 1 button. for eg: Textbox1 = Enter the no.of subjects Textbox2 = Enter 1 subject & Add to listbox by press button. repeat this subject entry upto textbox1 value..
Posted
Updated 7-Dec-11 20:01pm
v2
Comments
Sergey Alexandrovich Kryukov 8-Dec-11 2:00am    
Tag it! WPF, Silverlight, Forms, APS.NET, what?!
--SA

It cannot even compile. In "for" loop n supposed to be integer, and you are using a string. Makes no sense at all. Wow!

Did you mean to parse this string into some integer? Use something like int.Parse(n) or int.TryParse(n).

SA
 
Share this answer
 
Comments
hari301 8-Dec-11 2:20am    
This also not working. Have any idea?
Vic91 8-Dec-11 2:28am    
try converting it. If the above won't help, (though they should), try
val(n)
hari301 8-Dec-11 2:35am    
Same prob. here any array declaration need???
Sergey Alexandrovich Kryukov 8-Dec-11 10:56am    
It's not working just in your hands. If you still did not put it right, use "Improve question" above and add new code. But first, run it under debugger. When you post it, indicate exactly what problem in what line, post comprehensive error or exception information -- why do I have to even ask about that?

The site if for software developers; and software developers don't say "not working", they provide comprehensive issue report.
--SA
As SA said your n is string, how it will support FOR
Make sure txt_theory.Text contains only integer value.

VB
For i = 1 To convert.ToInt32(n)
lst_theory.Items.Add(temp)
Next i
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Dec-11 10:52am    
I need to clarify that it can be validated by catching exception thrown by int.Parse if the input text is not parsed as a valid integer, or by checking up Boolean returned by int.TryParse. ToInt32 in this code sample works like Parse -- can throw exception. Parse or TraParse is always better as it does not create a false illusion of "conversion". I voted 4.
--SA
So you're trying to add multiple items to the listbox? I'm not sure if you could do that with just one textbox.. you would have to click a button or something everytime you want to add something. What you could do is get the number of loops from the txt_theory and then in the for loop show an inputbox

something like:
VB
For i = 1 To n
     dim text as string
     text=inputbox("Type theory here:")
     lst_theory.Items.Add(CType(text, String))
Next i
 
Share this answer
 
There are different ways to get rid of your error

Medthod 1:

VB
Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
        Dim n As String = txt_theory.Text
        Dim temp As String = txt_theoryent.Text
        Dim i As Integer
        For i = 0 To Val(n)
            lb1.Items.Add(CType(temp, String))
        Next
    End Sub


Method 2:

txt_theory.Text should have to enter integer, so you need to validate it on keypress event to enter only integer values and it can convert to integer when it is integer value
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900