如何在vb.net 2008中处理动态创建的TextBoxControl的事件 [英] How to Handel Events of dynamically created TextBoxControl in vb.net 2008

查看:107
本文介绍了如何在vb.net 2008中处理动态创建的TextBoxControl的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了以下类来动态创建文本框数组.
以下是TextBoxArray.VB文件

I have created following class to create Textbox Array Dynamically.
Below is TextBoxArray.VB file

Public Class TextBoxArray

    Inherits System.Collections.CollectionBase
    Private ReadOnly HostForm As System.Windows.Forms.Control
    Dim aTextBox As New System.Windows.Forms.TextBox
    Dim nIndex As Integer
    Dim nText As String
    Dim nLocation As Point
    Dim nParent As System.Windows.Forms.Control
    Dim nVisible, nEnable As Boolean
    Dim nSize As Size
    Dim nBorderStyle As BorderStyle
    Dim nTextAlign As ContentAlignment
    Dim nFont As Font
    Dim nName as string 

    Public Sub New(ByVal host As System.Windows.Forms.Control)
        HostForm = host
        Me.AddTextBox()
    End Sub

    Public Function AddTextBox() As System.Windows.Forms.TextBox
        ' Create a new instance of the Button class.

        ' Add the button to the collection's internal list.
        aTextBox = New TextBox
        Me.List.Add(aTextBox)
        ' Add the button to the controls collection of the form 
        ' referenced by the HostForm field.
        HostForm.Controls.Add(aTextBox)
        ' Set intial properties for the button object.
        With aTextBox
            .Location = nLocation
            .Tag = Me.Count
            .Text = nText
            '.BackColor = Color.Transparent
            .BorderStyle = Windows.Forms.BorderStyle.FixedSingle
            'aTextBox.Parent = nParent
            .Name = "TextBox" & Me.Count.ToString
            nName = .Name
            .Visible = False
            'AddHandler aTextBox.Click, AddressOf TextBox_Click
        End With
        Return aTextBox
    End Function

    Public Sub RemoveTextBox()
        ' Check to be sure there is a button to remove.
        While Me.Count > 0
            HostForm.Controls.Remove(Me(Me.Count - 1))
            Me.List.RemoveAt(Me.Count - 1)
        End While
    End Sub

    Default Public ReadOnly Property Item(ByVal Index As Integer) As  _
    System.Windows.Forms.TextBox
        Get
            Return CType(Me.List.Item(Index), System.Windows.Forms.TextBox)
        End Get
    End Property

    Public Property Index() As Integer
        Get
            Return nIndex
        End Get
        Set(ByVal value As Integer)
            nIndex = value
        End Set
    End Property

    Public Property Size() As Size
        Get
            Return nSize
        End Get
        Set(ByVal value As Size)
            nSize = value
            aTextBox.Size = value
        End Set
    End Property

    Public Property Location() As Point
        Get
            Return nLocation
        End Get
        Set(ByVal value As Point)
            nLocation = value
            aTextBox.Location = nLocation
        End Set
    End Property

    Public Property Text() As String
        Get
            Return nText
        End Get
        Set(ByVal value As String)
            nText = value
            aTextBox.Text = value
        End Set
    End Property

    Public Property Visible() As Boolean
        Get
            Return nVisible
        End Get
        Set(ByVal value As Boolean)
            nVisible = value
            aTextBox.Visible = value
        End Set
    End Property

    Public Property BorderStyle() As BorderStyle
        Get
            Return nBorderStyle
        End Get
        Set(ByVal value As BorderStyle)
            nBorderStyle = value
            aTextBox.BorderStyle = value
        End Set
    End Property

    Public Property AlignText() As ContentAlignment
        Get
            Return nTextAlign
        End Get
        Set(ByVal value As ContentAlignment)
            nTextAlign = value
            aTextBox.TextAlign = value
        End Set
    End Property

    Public Property Parent() As System.Windows.Forms.Control
        Get
            Return nParent
        End Get
        Set(ByVal value As System.Windows.Forms.Control)
            nParent = value
            aTextBox.Parent = value
        End Set
    End Property

    Public Property Enable()
        Get
            Return nEnable
        End Get
        Set(ByVal value)
            nEnable = value
            aTextBox.Enabled = value
        End Set
    End Property

    Public Property Font() As Font
        Get
            Return nFont
        End Get
        Set(ByVal value As Font)
            nFont = value
            aTextBox.Font = nFont
        End Set
    End Property

    Public Property Name() As String
        Get
            Return nName
        End Get
        Set(ByVal value As String)
            nName = value
            aTextBox.Name = nName
        End Set
    End Property

End Class



要动态显示3个文本框,请在主窗体中执行以下步骤,



And to Display 3 Textbox Dynamically I do following steps in Main Form,

Public Class Main

    Dim txt(3) As TextBoxArray

    Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim Left, Top As Integer
        Left = 22
        Top = 30
        For I = 0 To 2
            txt(I) = New TextBoxArray(Me)
            txt(I).Location = New Point(Left, Top)
            txt(I).Visible = True
            Top += 30
        Next
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For I As Integer = 0 To 2
            MsgBox("TextBox:" & I & txt(I).Text)
        Next
    End Sub
End Class



上面的代码成功显示了3个文本框,但是我的问题是我不知道如何获取在文本框中输入的文本.
我使用Button1来获取tetxbox和



Above code displays 3 text box successfully but my problem is i don''t know how to get the text entered inside the text box.
I used Button1 to get the value of tetxbox and

txt(Index).Text


的值 在文本框中不会给我任何可用的内容.

有人可以帮我吗?
我只想检索在文本框中输入的文本.


will not gives me anything available in the text box.

Can anybody help me on this.
I just wants to retrieve the text entered in the text box.

推荐答案

很抱歉让您感到厌烦,但这几乎是一种编码恐怖. 要创建TextBoxArray,将满足以下条件:
Sorry to break this to you, but this is pretty much a coding horror.
To create an Array of TextBoxes the following would have sufficed:
' Creates an Array that has room for 3(!) TextBoxes.
Dim textBoxArray(2) As TextBox
' They still have to be set, like following:
textBoxArray(0) = New TextBox
' Alternatively you can use a List(Of TextBox).


您在冗余(和可怕的内容)TextBoxArray Class中定义的所有Properties已经是TextboxProperties.
您可以像这样设置它们:


All the Properties you have defined in your redundant (and horribly written) TextBoxArray Class are already Properties of the Textbox.
You can set them like this:

' Assuming there is a TextBox at the first index of your array
' textBoxArray(0) returns a TextBox.
textBoxArray(0).Visible = False
textBoxArray.Location = New Point(20, 30)
' Etc.
' Possibly do this in a For Loop.


此外,在您的For Loop中,每次都会创建一个新的Array,从而使您失去旧的Array(及其引用).
要获取任何TextBox es的 Text Property,只需索取.


Furthermore in your For Loop you create a new Array every time, making you lose your old Array (and references to it).
To get the Text Property of any one of the TextBoxes, simply ask for it.

' Use MessageBox rather than MsgBox. MsgBox is a pre-.NET version of the MessageBox.
MessageBox.Show("TextBox1's text is: " & textBoxArray(0).Text)


这与Events无关(如您的问题标题所述).
要动态处理Event,请使用AddHandler RemoveHandler 关键字.


This has nothing to do with Events (as your question title states).
To dynamically handle Events use the AddHandler and RemoveHandler keywords.

' To add an Event Handler:
AddHandler textBoxArray(0).TextChanged, AddressOf TextBox_TextChanged
' To remove an Event Handler:
RemoveHandler textBoxArray(0).TextChanged, AddressOf TextBox_TextChanged

' The Method that handles it:
Private Sub TextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
   ' Code to handle the Event here.
End Sub


上面的代码表明您不了解一些基本的编程规则.
我建议您开始阅读一些东西.买一本书,在互联网上阅读一些教程和文章(CodeProject是一个不错的起点!).
希望对您有帮助,祝您好运!


Your code above shows that you have not understood some basic programming rules.
I suggest you start reading up on things. Buy a book, read some tutorials and articles on the internet (CodeProject is a good place to start!).
Hope it helps and good luck!


这篇关于如何在vb.net 2008中处理动态创建的TextBoxControl的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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