通过vb.net中的文本框在列表框中添加项目 [英] Add item in listbox through text box in vb.net

查看:240
本文介绍了通过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 


这是我的代码. For循环无法正常工作.请纠正它.我正在使用2个文本框,1个列表框,1个按钮.例如:Textbox1 =输入主题数Textbox2 =输入1个主题&按按钮添加到列表框.重复此主题条目直到textbox1值..


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..

推荐答案

它甚至无法编译.在"for"循环中,n应该是整数,并且您正在使用字符串.完全没有道理.哇!

您是要将此字符串解析为某个整数吗?使用类似int.Parse(n)int.TryParse(n)的名称.

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


正如SA所说,您的 n 是字符串,它将如何支持 FOR
确保 txt_theory.Text 仅包含整数值.

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


您要在列表框中添加多个项目吗?我不确定是否可以仅使用一个文本框来完成此操作.每次要添加内容时,都必须单击按钮或其他内容.您可以做的是从txt_theory获取循环数,然后在for循环中显示一个输入框

类似的东西:
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


这篇关于通过vb.net中的文本框在列表框中添加项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆