0

フォームの Panel コントロール内に配置されたものTableLayoutPanel(1 行、3 列) があります。

私のフォームにも 1 つのコマンド ボタンがあります。ボタンがクリックされるたびに、ラベル (1 列目)、テキストボックス (2 列目)、およびボタン (3 列目) が動的に作成されます。

次のような操作を実行したい:

ボタン(各行の3列目)をクリックするとLABEL+TEXTBOX+BUTTON、他のコントロールをそのままにして、関係する行を削除する必要があります。

誰かが私を解決するのを手伝ってくれますか?

4

2 に答える 2

0

ボタンをクリックすると、他の2つのコントロールを参照する必要があります

私のおすすめ - -

'craete a class to hold all controls in single object
Public Class MyCtls
    Public mLBL As Label
    Public mTXT As TextBox
    Public mBTN As Button

    Public Sub New(ByVal LBL As Label, ByVal TXT As TextBox, ByVal BTN As Button)
        MyBase.New()
        mLBL = LBL
        mTXT = TXT
        mBTN = BTN
    End Sub
End Class

'While creating new row create class reference and store it somewhere in accessed control
'For example we are using tag prosperity of button for this
'Now add handler for this button
Private Sub CreateNewRow()
    Dim nRow As MyCtls = New MyCtls(New Label, New TextBox, New Button)
    'set postition and add to parrent
    nRow.mBTN.Tag = nRow
    AddHandler nRow.mBTN.Click, AddressOf mBTN_Click
End Sub

'now for removing this row
Private Sub mBTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim thisRow As MyCtls = DirectCast(CType(sender, Button).Tag, MyCtls)
    'remove handler first
    RemoveHandler thisRow.mBTN.Click, AddressOf mBTN_Click

    'remove controls from its parent and dispose them
    yourparentcontrol.Controls.Remove(thisRow.mLBL)
    yourparentcontrol.Controls.Remove(thisRow.mTXT)
    yourparentcontrol.Controls.Remove(thisRow.mBTN)

    'dispose them all
    thisRow.mLBL.Dispose() 'do as this
End Sub
于 2012-11-23T12:08:53.297 に答える
0

3 つの要素 (ラベル、テキスト ボックス、ボタン) をすべて持つ独自のユーザー コントロールを作成し、メイン ボタンがクリックされるたびにこのコントロールを追加することをお勧めします。その後、イベント ハンドラーを「行」コントロールのボタン クリックに接続し、メイン フォームから処理することができます。以下の一般的なアイデア:

ユーザー コントロールに次を追加する必要があります。

Public Event MyButtonClicked

Public Sub MyButtonClick() Handles MyButtonClick
    Raise Event MyButtonClicked(Me)
End Sub

メインフォームには、そのようなものがあります

Public Sub CreateNewRow() Handles MainButton.Click
    Dim NewRow as New MyUserControl
    AddHandler NewRow.Click, AddressOf RemoveRow
    FlowLayoutPanel.Controls.Add(NewRow)
End Sub

Public Sub RemoveRow(ByRef Row as MyUserControl)
    FlowLayoutPanel.Controls.Remove(Row)
End Sub

これにより、デザイナを使用して 1 つの行を設計できます。また、すべての機能 (検証など) を小さな「行コントロール」内にコーディングして、コードを少しきれいにすることもできます。

于 2012-11-23T12:02:10.010 に答える