使用动态创建的控件进行处理 [英] handle with dynamically created controls

查看:43
本文介绍了使用动态创建的控件进行处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我已经声明了一些变量:

将dim变量作为整数
将dim2变暗为整数
将dim3变暗为整数

我创建了一个具有以下位置的数组:

sum1''在索引0
sum2''关于索引1
sum3''关于索引2

问题是如何用数组中的值替换其名称?



如何使用数组中的名称创建变量?

请帮忙.

改善:

我有一个简单的代码,可以动态生成表单上的某些控件:


Hello,

I have declared some variables:

dim variable as integer
dim variable2 as integer
dim variable3 as integer

and i have created an array with positions:

sum1 ''on index 0
sum2 ''on index 1
sum3 ''on index 2

The question is how to replace their names with values from an array?

or

How to create variables with names from an array?

Please help.

Improve:

I have a simple code generating dynamically some controls on the form:


Imports System
Imports System.Windows

Public Class Form1

    Public WithEvents new_button As Button = New Button()
    Public new_label As Label = New Label()
    Public new_text_box As TextBox = New TextBox()

    Private Sub frmDoPakowania_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown

        Dim on_top As Integer
        Dim on_top_tb As Integer
        Dim tb_value As Integer = 0
        on_top = 25
        on_top_tb = 21

        Dim counter As Integer = 2

        Try
            For d = 1 To counter
                new_label = New Label()
                new_text_box = New TextBox()
                new_button = New Button()
                new_label.Text = "Value:"
                new_label.Left = 13
                new_label.Top = on_top
                new_label.Width = 67
                new_label.Height = 13
                Me.Controls.Add(new_label)
                new_text_box.Font = New Font(Me.Font.FontFamily, 8.5, FontStyle.Regular)
                new_text_box.Text = tb_value
                new_text_box.Left = 80
                new_text_box.Top = on_top_tb
                new_text_box.Width = 35
                new_text_box.Height = 20
                new_text_box.ForeColor = Color.Black
                Me.Controls.Add(new_text_box)
                new_button.Text = "Raise up"
                new_button.Font = New Font(Me.Font.FontFamily, 7.75, FontStyle.Regular)
                new_button.Height = 20
                new_button.Left = 128
                new_button.Top = on_top_tb
                AddHandler new_button.Click, AddressOf Me.new_button_Click
                Me.Controls.Add(new_button)
                counter -= 1
                on_top += 23
                on_top_tb += 23
            Next
        Catch ex As Exception
        End Try

        RemoveHandler new_button.Click, AddressOf Me.new_button_Click

    End Sub

    Private Sub new_button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles new_button.Click

        Dim aa As Integer
        aa = new_text_box.Text
        aa = aa + 1
        new_text_box.Text = aa
    End Sub

End Class



我在其他问题中发布了相同的代码,因此对重复出现的内容感到抱歉.

问题是,当我单击任何按钮时,底部文本框中的值只会增加.如您所知,目标是在按钮左侧的文本框中提高价值...

不要建议只有两行,因为变量计数器可以具有不同的值,即20.

我不知道如何处理这个问题,所以我有很多想法,包括带变量的想法,但是我敢肯定,即使我这样做也不会正确工作... :(



I''ve published the same code in other question, so i''m sorry for duplicates.

The problem is that when I click on any button only value in bottom textbox is raising. As you know the target is to raise up value in text box on the left of button...

Don''t suggest that there are only two lines, because variable counter can have different values, i.e. 20.

I don''t know how to handle with this problem so I have many ideas, including this with variables, but I''m sure it will not work correctly even if I will do it... :(

can You help?

推荐答案

您不能-VB不支持指针.
相反,请考虑使用字典:
You can''t - VB does not support pointers.
Instead, consider using a Dictionary:
Dim dict As New Dictionary(Of String, Integer)()
dict.Add("Variable1", 0)
dict.Add("Variable2", 1)
dict.Add("Variable3", 2)
Dim i As Integer = dict("Variable2") + dict("Variable3")
Console.WriteLine(i)




"谢谢,但是您误解了我-肯定是因为数组中的名称. :)我心里还不算数学函数. :)也可能有类似的位置:
boat1
boat2
boat3


啊!如果您了解我要从数组中读取字符串,并使用该字符串中值的名称创建一个变量?您可以使用Reflection来做到这一点,但是要破解它可能是一个简单的螺母,这是一个繁琐的工作,因为您的变量名将无法在编译时使用,因此它们将很难像创建时那样使用. br/>
为什么要这么做?






""thanks but You misunderstood me - surely becasue of names in array. :) i haven''t in my mind sum as math function. :) there also can be positions like:
boat1
boat2
boat3


Ah! If I understand you you want to read strings from an array, and create a variable with the name of the value in the string? You can do that, using Reflection, but it is a cumbersome sledgehammer to crack what it probably a simple nut, because your variable names will not be available as compile time, so they will be as difficult to use as they are to create.

Why do you want to do that?



Private Sub new_button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles new_button.Click

     Dim aa As Integer
     aa = new_text_box.Text
     aa = aa + 1
     new_text_box.Text = aa
 End Sub




在其他问题中,我已经发布了相同的代码,因此,对于重复的内容,我感到抱歉.

问题是,当我单击任何按钮时,底部文本框中的值只会增加.如您所知,目标是在按钮左侧的文本框中提高价值..."


啊!这很容易!

您无需担心名称,只需要获取正确的实例即可.幸运的是,每个事件都会将引发事件的对象作为sender参数传递给您:




"I''ve published the same code in other question, so i''m sorry for duplicates.

The problem is that when I click on any button only value in bottom textbox is raising. As you know the target is to raise up value in text box on the left of button..."


Ah! That makes it easy!

You don''t need to worry about names, you just need to get the correct instance. Fortunately, every Event passes you the object that raised the event as the sender parameter:

Private Sub new_button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles new_button.Click

     Dim t As TextBox = TryCast(sender, TextBox)
     If t IsNot Nothing Then

        Dim aa As Integer
        aa = t.Text
        aa = aa + 1
        t.Text = aa

     End If
 End Sub



编辑-我刚刚意识到...

该事件是一个Button事件,因此不会像以前那样起作用.
frmDoPakowania_Shown函数中添加一行代码:



Edit - I just realized...

The event is a Button event, so it won''t work quite like that.
Add a line of code to your frmDoPakowania_Shown function:

AddHandler new_button.Click, AddressOf Me.new_button_Click
new_button.Tag = new_text_box   ''****** ADD THIS
Me.Controls.Add(new_button)

然后将功能更改为使用按钮及其Tag属性:

Then change the function to use a button, and it''s Tag property:

Private Sub new_button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles new_button.Click
     Dim b as Button = TryCast(sender, Button)
     If b IsNot Nothing Then

        Dim t As TextBox = TryCast(b.Tag, TextBox)
        If t IsNot Nothing Then
           Dim aa As Integer
           aa = t.Text
           aa = aa + 1
           t.Text = aa
        End If
     End If
 End Sub

Tag属性是每个控件都具有的属性,可用于将对象放入其中.在这种情况下,每个按钮都与特定的TextBox有关,因此我们告诉按钮要引用哪个框.


DOH!将new_text_box.Text替换为t.Text-OriginalGriff [/edit]

The Tag property is something each control has, and is available for you to put an object into. In this case, each button is concerned with a specific TextBox, so we tell the button which box to refer to.


[edit]DOH! Replaced new_text_box.Text with t.Text - OriginalGriff[/edit]


您可以通过将名称/对象存储在某种类型的集合中并编写代码以将数据检索到您为其编写代码的对象.但这仍然是我们的追求:为什么?
You could do it by storing the name/object in some type of collection, and write code to retrieve the data into an object that you have written code for. But that still begs the quest: why?


这篇关于使用动态创建的控件进行处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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