Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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 7-Dec-11 19:58pm
Edited 7-Dec-11 20:01pm
v2
Comments
SAKryukov at 8-Dec-11 2:00am
   
Tag it! WPF, Silverlight, Forms, APS.NET, what?!
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
hari301 at 8-Dec-11 2:20am
   
This also not working. Have any idea?
Vic91 at 8-Dec-11 2:28am
   
try converting it. If the above won't help, (though they should), try
val(n)
hari301 at 8-Dec-11 2:35am
   
Same prob. here any array declaration need???
SAKryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

As SA said your n is string, how it will support FOR
Make sure txt_theory.Text contains only integer value.
 
For i = 1 To convert.ToInt32(n)
lst_theory.Items.Add(temp)
Next i
  Permalink  
Comments
SAKryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
For i = 1 To n
     dim text as string
     text=inputbox("Type theory here:")
     lst_theory.Items.Add(CType(text, String))
Next i
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

There are different ways to get rid of your error
 
Medthod 1:
 
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
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 768
1 OriginalGriff 420
2 CPallini 275
3 George Jonsson 226
4 Richard Deeming 145
0 OriginalGriff 5,450
1 CPallini 4,500
2 Sergey Alexandrovich Kryukov 4,272
3 George Jonsson 3,057
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 8 Dec 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100