none
要如何讓產生一個控制項之後就不能在產生了? RRS feed

  • 問題

  • 要如何讓產生一個控制項之後就不能在產生了?
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim BTN As Button = New Button
            BTN.Show()
            BTN.Left = 12
            BTN.Top = 12
            BTN.Width = 110
            BTN.Height = 25
            BTN.Text = "動態產生的Button"
            Me.Controls.Add(BTN)
            BTN.Parent = Panel1
            AddHandler BTN.Click, AddressOf BTN_Click
        End Sub
    上面這段程式碼可不可以加入一個判斷式,如果已經產生過了就不能在產生了?
    如果你對這方面沒有興趣,那就不要強迫自己做這方面的事.
    2009年9月29日 上午 08:14

解答

  • Hi!
    假如你是要限制 [ Button.Text = 動態產生的Button 的按鈕 ] 只能產生一次的話,可以參考以下程式碼
    另外,也許你在新增按鈕的時候,可以把 Name 也設定進去,方便做識別

       
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            For Each con In Me.Panel1.Controls
                If con.Text = "動態產生的Button" Then
                    MessageBox.Show("已經產生過了")
                    Return
                End If
            Next

            Dim BTN As Button = New Button
            BTN.Show()
            BTN.Left = 12
            BTN.Top = 12
            BTN.Width = 110
            BTN.Height = 25
            BTN.Text = "動態產生的Button"
            Me.Controls.Add(BTN)
            BTN.Parent = Panel1
            AddHandler BTN.Click, AddressOf BTN_Click
        End Sub

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

        End Sub




    假如你是要限定 Panel 內產生一個控制項之後就不能在產生了,可以這樣做

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If Me.Panel1.Controls.Count = 0 Then
                Dim BTN As Button = New Button
                BTN.Show()
                BTN.Left = 12
                BTN.Top = 12
                BTN.Width = 110
                BTN.Height = 25
                BTN.Text = "動態產生的Button"
                Me.Controls.Add(BTN)
                BTN.Parent = Panel1
                AddHandler BTN.Click, AddressOf BTN_Click
            End If
        End Sub

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

        End Sub


    .NET菜鳥自救會 http://www.dotblogs.com.tw/chou/

    2009年9月29日 上午 08:24
    版主

所有回覆

  • Hi!
    假如你是要限制 [ Button.Text = 動態產生的Button 的按鈕 ] 只能產生一次的話,可以參考以下程式碼
    另外,也許你在新增按鈕的時候,可以把 Name 也設定進去,方便做識別

       
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            For Each con In Me.Panel1.Controls
                If con.Text = "動態產生的Button" Then
                    MessageBox.Show("已經產生過了")
                    Return
                End If
            Next

            Dim BTN As Button = New Button
            BTN.Show()
            BTN.Left = 12
            BTN.Top = 12
            BTN.Width = 110
            BTN.Height = 25
            BTN.Text = "動態產生的Button"
            Me.Controls.Add(BTN)
            BTN.Parent = Panel1
            AddHandler BTN.Click, AddressOf BTN_Click
        End Sub

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

        End Sub




    假如你是要限定 Panel 內產生一個控制項之後就不能在產生了,可以這樣做

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If Me.Panel1.Controls.Count = 0 Then
                Dim BTN As Button = New Button
                BTN.Show()
                BTN.Left = 12
                BTN.Top = 12
                BTN.Width = 110
                BTN.Height = 25
                BTN.Text = "動態產生的Button"
                Me.Controls.Add(BTN)
                BTN.Parent = Panel1
                AddHandler BTN.Click, AddressOf BTN_Click
            End If
        End Sub

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

        End Sub


    .NET菜鳥自救會 http://www.dotblogs.com.tw/chou/

    2009年9月29日 上午 08:24
    版主
  • 用個全域變數控制也行
    Public Class Form2
        Dim x As Integer = 0
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            If x = 0 Then
                Dim BTN As Button = New Button
                BTN.Show()
                BTN.Left = 12
                BTN.Top = 12
                BTN.Width = 110
                BTN.Height = 25
                BTN.Text = "動態產生的Button"
                Me.Controls.Add(BTN)
                x=x+1
            End If
        End Sub
    End Class

    另一個就是利用EventHandler的新增/移除
    在產生Button後把 Click Event的Handler移掉
    學而不思則罔, 思而不學則殆.
    如果你一直都看不懂、不想學習看懂、抗拒看懂MSDN Library的話,那你最好放棄想要寫好程式這件事
    2009年9月29日 上午 08:32
    版主