1

デザイナーでページ1を作成するタブコントロールを使用しています。プログラムでページ上のコントロールを含む新しいタブ ページを作成しています。各ページにはいくつかのパネルがあり、それぞれに 2 つのラジオボタン (1 つははい、もう 1 つはいいえ) があります。visible プロパティが false に設定された最初のパネル内にネストされたパネルがあります。ユーザーが「はい」を選択した場合は、ネストされたパネルの可視プロパティを true に設定して、さらに選択する必要があるラジオボタンをさらにいくつか表示する必要があります。

私の問題は、1 ページ目以外のページでネストされたパネルのプロパティを変更することです。ラジオボタンは検出できますが、ネストされたパネルを表示する方法が見つからないようです。

Public Class ControlProgram

Dim pageindx as integer

 Private Sub btnAddPrgm1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddPrgm1.Click

    Dim newTab As New TabPage()
    pageindx = (TabControl1.TabPages.Count + 1)
    newTab.Text = "Step  " & pageindx
    'define fill panel controls
    Dim newpnlFill As New Panel
    Dim newlblFill As New Label
    Dim newFillY As New RadioButton
    AddHandler newFillY.CheckedChanged, AddressOf CheckforCheckedChanged
    Dim newFillN As New RadioButton
    AddHandler newFillN.CheckedChanged, AddressOf CheckforCheckedChanged

    'add fill panel controls
    With newTab.Controls
        .Add(newpnlFill)
        With newpnlFill
            .Location = New System.Drawing.Point(6, 6)
            .Size = New System.Drawing.Size(171, 137)
            .BorderStyle = BorderStyle.FixedSingle
            .Controls.Add(newlblFill)
            With newlblFill
                .Name = "Fill"
                .Text = "Fill ?"
                .Font = New Font(newlblFill.Font, FontStyle.Bold)

                .Location = New Drawing.Point(5, 3)
            End With
            .Controls.Add(newFillY)
            With newFillY
                .Name = "FillY"
                .Text = "Yes"
                .Location = New Drawing.Point(23, 28)
                .Size = New System.Drawing.Size(43, 17)
            End With
            .Controls.Add(newFillN)
            With newFillN
                .Name = "FillN"
                .Text = "No"
                .Location = New Drawing.Point(88, 28)
                .Size = New System.Drawing.Size(39, 17)
            End With
            .Controls.Add(newpnlFill2)
            With newpnlFill2
                .Location = New System.Drawing.Point(2, 60)
                .Size = New System.Drawing.Size(164, 68)
                .BorderStyle = BorderStyle.FixedSingle
                .Visible = False
            End With
      End With
   End With
Private Sub CheckforCheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    If TypeOf sender Is RadioButton Then

        bEvent = CType(sender, RadioButton).Name
     End If
End Sub

クラス終了

それ以来、あなたの提案を出発点として使用して、デリマの解決策を見つけました。

いくつかの変数を追加しました: Dim rb as Control Dim bEvent as String Dim booFillY as Boolean Dim booFillN as Boolean

また、TabControl TabControl1.TabPages.Add(newTab) を追加しました

これらの変更も行いました:

    Private Sub CheckforCheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    If TypeOf sender Is RadioButton Then

        rb = sender
        bEvent = CType(sender, RadioButton).Name
        If bEvent = "FillY" Then
            Dim newpnlFill2 As Panel = rb.Parent.Controls(3)
            newpnlFill2.Visible = True
        End If
        If bEvent = "FillN" Then


            Dim newpnlFill2 As Panel = rb.Parent.Controls(3)
            newpnlFill2.Visible = False

        End If
    End If
End Sub

これで、作成したタブ ページの [はい] または [いいえ] ラジオボタンをクリックして、ネストされたパネル (newpnlFill2) を表示または非表示にすることができます。ご協力いただきありがとうございます。私は自分でそこにたどり着いたことはないと思います。

4

2 に答える 2

1

あなたが探していたものではないかもしれませんが、あなたがどこへ行く必要があるかを知るのに役立つはずです.

アプリケーションを作成するときは、特定のページのすべてのコントロールのリストを load イベントで作成して、いつでもアクセスできるようにするのが好きです。これは、WinForms がタブページやグループ ボックス内などで子コントロールを表示することについて非常にうるさい場合があるため、役に立ちます。

'Declare this variable within the class for your form (whatever)
Public arrControlsRecursive As New List(Of Control)

'method to recursively check all controls and build to array
Private Sub BuildControlsArrayRecursive(ByVal InControl As Control)
    For Each con As Control In InControl.Controls
        If con.Controls.Count > 0 Then
            BuildControlsArrayRecursive(con)
        End If
        If TypeOf con Is Control Then
            arrControlsRecursive.Add(con)
        End If
    Next
End Sub

'Call from MyBase.Load Event
BuildControlsArrayRecursive(Form1)

たとえば、If ステートメントを次のように変更して、すべてのタブのリストを組み立てることもできます。Is TypeOf con Is TabPage

これで、このコレクションをループするか、LINQ でクエリを実行できます。first または single メソッドを呼び出して、単一のコントロールを検索します。必要な型にキャストし、フォーム内の任意のコントロールに対して何でも実行します。

于 2013-03-07T02:15:09.693 に答える
0

何にアクセスしたいのかよくわかりません。最初に話すのは

1 ページ目以外のページでネストされたパネルのプロパティを変更する

したがって、他のタブにアクセスしたいと思います。次に、次のように話します。

パネルを表示する方法が見つからないようです

とにかく、ここに2つの解決策があります:

他のパネルにアクセスします。

Private Sub CheckforCheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    If TypeOf sender Is RadioButton Then
        bEvent = CType(sender, RadioButton).Name '**Where is "bEvent" declared??**

        Dim newpnlFill2 as Panel = bEvent.Parent.Controls(3), Panel)
        newpnlFill2.Visible = bEvent.Checked
     End If
End Sub

それへのアクセスParentは newpnlFill になり、それへのアクセスControls(3)は になりますnewpnlFill2

他のタブにアクセスします。

Private Sub CheckforCheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    If TypeOf sender Is RadioButton Then
        bEvent = CType(sender, RadioButton).Name '**Where is "bEvent" declared??**

        Dim TabControl as TabControl = bEvent.Parent.Parent.Parent, TabControl)
        'Then, you can access all of the other tabs with:
        'TabControl.TabPages(n)
     End If
End Sub

これはnewTab、TabControl に追加する場所を想定しています。newTabTabControl に決して追加しないことがわかります。したがって、表示Parentされることはありません。newpnlFillnewTab

とにかく、それは本当にひどいものです。タブが常にこの方法で作成されていると想定しているためです。たとえば、 の newpnlFillに別のパネルを追加する場合、それはパネル内の 4 番目のコントロールではなくなるため、アクセス コードを変更する必要があります。

私のアドバイスは、TabPage から継承する独自の UserControl を作成することです。このようにして、変更するパネルを常に参照するプライベート変数を作成できます。さらに、btnAddPrgm1_Click イベントはより明確になり、クラス コンストラクターでページのビルドを移動します。何かのようなもの:

Private Sub btnAddPrgm1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddPrgm1.Click
    Dim newTab As New MyTabPage()
    pageindx = (TabControl1.TabPages.Count + 1)
    newTab.Text = "Step  " & pageindx
    TabControl1.TabPages.Add(newTab)
End Sub
于 2013-03-05T13:05:12.797 に答える