25. august 2004 - 10:24Der er
5 kommentarer og 1 løsning
Controls - array ?
Hej
I det gamel vb6 kunne man lave arrays på sine controls, og der med lave en lykke som kunne løbe disse controls igennem ved at kalde controls checkbox(index) - kan man lave noget ligende i VB.net
helt uden problemer.... kontrollerne virker i sig selv som containere, og for f.eks. en form vil du derfor kunne iterere over : frmObject / Me.Controls jeg er ikke skrap til VB, men i C# kunne du lave en foreach(Control c in someControlContainer.Controls) // ...
Du kan lave et array som peger på dine checkboxe som dette eksempel viser:
Imports System Imports System.Drawing Imports System.Windows.Forms
Namespace DefaultNamespace
Public Class MainForm Inherits System.Windows.Forms.Form Private checkBox1 As CheckBox Private checkBox2 As CheckBox Private checkBox3 As CheckBox Private checkBox4 As CheckBox Private checkBoxAll(4) As CheckBox Private button1 As Button Private button2 As Button
Public Shared Sub Main Dim fMainForm As New MainForm fMainForm.ShowDialog() End Sub
Public Sub New() MyBase.New InitializeComponent checkBoxAll(0) = checkBox1 checkBoxAll(1) = checkBox2 checkBoxAll(2) = checkBox3 checkBoxAll(3) = checkBox4 End Sub
Private Sub InitializeComponent() checkBox1 = New CheckBox checkBox2 = New CheckBox checkBox3 = New CheckBox checkBox4 = New CheckBox button1 = New Button button2 = New Button SuspendLayout checkBox1.Location = New Point(50, 50) checkBox1.Size = New Size(100, 25) checkBox1.Text = "Valg 1" checkBox2.Location = New Point(50, 100) checkBox2.Size = New Size(100, 25) checkBox2.Text = "Valg 2" checkBox3.Location = New Point(50, 150) checkBox3.Size = New Size(100, 25) checkBox3.Text = "Valg 3" checkBox4.Location = New Point(50, 200) checkBox4.Size = New Size(100, 25) checkBox4.Text = "Valg 4" button1.Location = New Point(200, 100) button1.Size = New Size(50, 25) button1.Text = "Set" AddHandler button1.Click, AddressOf Button1Click button2.Location = New Point(200, 150) button2.Size = New Size(50, 25) button2.Text = "Clear" AddHandler button2.Click, AddressOf Button2Click ClientSize = New Size(300, 250) Controls.Add(checkBox1) Controls.Add(checkBox2) Controls.Add(checkBox3) Controls.Add(checkBox4) Controls.Add(button1) Controls.Add(button2) Name = "Main Form" Text = "Main Form" ResumeLayout(False) End Sub
Private Sub Button1Click(sender As System.Object, e As System.EventArgs) Dim i As Integer For i = 0 To 3 checkBoxAll(i).Checked = True Next End Sub
Private Sub Button2Click(sender As System.Object, e As System.EventArgs) Dim i As Integer For i = 0 To 3 checkBoxAll(i).Checked = False Next End Sub
End Class End Namespace
men så kan du jo i virkeligheden ligeså godt lave det til et array fra start af som dette eksempel viser:
Imports System Imports System.Drawing Imports System.Windows.Forms
Namespace DefaultNamespace
Public Class MainForm Inherits System.Windows.Forms.Form Private checkBoxAll(4) As CheckBox Private button1 As Button Private button2 As Button
Public Shared Sub Main Dim fMainForm As New MainForm fMainForm.ShowDialog() End Sub
Public Sub New() MyBase.New InitializeComponent End Sub
Private Sub InitializeComponent() Dim i As Integer For i = 0 To 3 checkBoxAll(i) = New CheckBox Next button1 = New Button button2 = New Button SuspendLayout For i = 0 To 3 checkBoxAll(i).Location = New Point(50, 50 + i * 50) checkBoxAll(i).Size = New Size(100, 25) checkBoxAll(i).Text = "Valg " & (i + 1) Next button1.Location = New Point(200, 100) button1.Size = New Size(50, 25) button1.Text = "Set" AddHandler button1.Click, AddressOf Button1Click button2.Location = New Point(200, 150) button2.Size = New Size(50, 25) button2.Text = "Clear" AddHandler button2.Click, AddressOf Button2Click ClientSize = New Size(300, 250) For i = 0 To 3 Controls.Add(checkBoxAll(i)) Next Controls.Add(button1) Controls.Add(button2) Name = "Main Form" Text = "Main Form" ResumeLayout(False) End Sub
Private Sub Button1Click(sender As System.Object, e As System.EventArgs) Dim i As Integer For i = 0 To 3 checkBoxAll(i).Checked = True Next End Sub
Private Sub Button2Click(sender As System.Object, e As System.EventArgs) Dim i As Integer For i = 0 To 3 checkBoxAll(i).Checked = False Next End Sub
End Class End Namespace
men så er du langt ud over hvad din designer kan klare !
For Each Ctrl In Me.Controls if(Ctrl.Name.StartsWith("MyControlArray")) then 'Kræver at du kalder dinne kontroller for MyControlArray1, MyControlArray2 .. MyControlArrayn endif next
Du kan også lave en If(Ctrl.GetType() = GetType(MyControllArray)) then
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.