如何在vb中的windows应用程序中动态创建复选框值? [英] How to get dynamically created checkbox value in windows application in vb ?

查看:167
本文介绍了如何在vb中的windows应用程序中动态创建复选框值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi
如何获取Windows应用程序中动态创建的复选框的值。我是从组框内的db动态创建基于复选框的数据。



和我动态创建的代码复选框是,我只创建了五个复选框。



Hi How to get value of dynamically created check box in windows application.i am create dynamically check box based data from db inside of group box.

And my code for dynamically create check box is ,and only five check box i create.

Dim CheckBoxes As List(Of CheckBox) = New List(Of CheckBox)

       Dim i As Integer = 1
       Dim x As Integer = 10
       Dim y As Integer = 15
       For Each row As DataRow In objdtr.Rows

           Dim chk As New CheckBox
           chk.Name = row("Voltage")
           chk.Text = row("Voltage")
           chk.Location = New Point(x, y)
           chk.Width = 70
           GroupBox1.Controls.Add(chk)
           x = x + 70

       Next





但是如果我勾选这个复选框,在节省时间如何检查盒子价值?







问候

Aravind



but if i check this check boxes ,in save time how to get checked box value ?



Regards
Aravind

推荐答案

在你的保存按钮中点击



In your Save Button Click

For Each chkBox In GroupBox1.Controls.OfType(Of CheckBox)()
 if Ctype(GroupBox1.controls("CheckBox" & i), checkbox).checked = true then
' your Code Here
 end if





如果您需要为复选框创建事件,那么您的代码可能是



If you need to create Event for your Check Box then your code might be

Dim CheckBoxes As List(Of CheckBox) = New List(Of CheckBox)
     
        Dim i As Integer = 1
        Dim x As Integer = 10
        Dim y As Integer = 15
        For Each row As DataRow In objdtr.Rows
 
            Dim chk As New CheckBox
            chk.Name = row("Voltage")
            chk.Text = row("Voltage")
          
            chk.Location = New Point(x, y)
            chk.Width = 70
            GroupBox1.Controls.Add(chk)
            x = x + 70
AddHandler chk.CheckedChanged, AddressOf ChkBox_CheckedChanged
 
        Next


Private Sub ChkBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    Dim chkBox As CheckBox = TryCast(sender, CheckBox)

    If chkBox IsNot Nothing Then

        MessageBox.Show(chkBox.CheckState)

    End If

End Sub

'In Save Button Click
For Each chkBox In GroupBox1.Controls.OfType(Of CheckBox)()
 if Ctype(GroupBox1.controls("CheckBox" & i), checkbox).checked = true then
' your Code Here
 end if
Next


有办法获得它们 - 您可以查看GroupBox.Controls列表并以这种方式识别它们 - 但更好的想法是保留它们的列表,以便您在保存时确切知道它们的位置:

在表单级别创建一个私有的CheckBoxes列表:

There are ways to get them - you can look through the GroupBox.Controls list and identify them that way - but a better idea would be to keep a List of them so that you know exactly where they are when it comes to saving:
Create a private list of CheckBoxes at class level in your form:
Private myBoxes As New List(Of CheckBox)()



将每个框添加到GroupBox时将其添加到列表中:


Add each box to the list when you add it to the GroupBox:

GroupBox1.Controls.Add(chk)
myBoxes.Add(chk)



在保存代码中,使用For Each循环显示框,并保存值:


In your Save code, use a For Each to loop through the boxes, and save the values:

For Each chk As CheckBox In myBoxes
    ' Save them here.
Next


这篇关于如何在vb中的windows应用程序中动态创建复选框值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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