Entwickler-Ecke

WinForms - Steuerelement korrekt ableiten


LocalDeeJay - Fr 28.03.08 12:40
Titel: Steuerelement korrekt ableiten
Hallöle liebe Community und Christian,

vorhin wollte ich meine MainForm etwas auslagern das ich nicht so viel Code in der Form.cs und im Form.Designer.cs habe, jedoch stoß ich auf einen, ich hoffe, klitzekleinen Fehler...

Ich habe in meiner MainForm, eine Listbox in einem Splitcontainer der sich im 2. Panel befindet. Nun hab ich eine eigene Klasse geschrieben, in der ich eine eigene Listbox definiere und von der ListBox-Klasse ableite. Füge ich dieses Control nun dem Splitcontainer hinzu, werden die Eigenschaften an der ListBox nicht übernommen, Eventhandler funktionieren auch nicht.

Kann mir evtl. jemand helfen?
Hier der Code:

Attribute in der FormKlasse

C#-Quelltext
1:
        private ACListBox playListBox;                    


Instanziierung in dem Standardkonstruktor

C#-Quelltext
1:
2:
                this.playListBox = new ACListBox();
                this.formSplitContainer.Panel2.Controls.Add(this.playListBox);


Die eigens erstellte ListBoxKlasse

C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
    class ACListBox : ListBox
    {

        #region Attributes

        private ListBox acListBox;
        private String pTrackName;

        public String trackName
        {
            get
            {
                return this.pTrackName;
            }
            set
            {
                this.pTrackName = value;
            }
        }

        #endregion

        public ACListBox()
        {
            this.acListBox = new ListBox();
            this.acListBox.Dock = System.Windows.Forms.DockStyle.Fill;
            this.acListBox.FormattingEnabled = true;
            this.acListBox.Location = new System.Drawing.Point(00);
            this.acListBox.Name = "playListBox";
            this.acListBox.Size = new System.Drawing.Size(248420);
            this.acListBox.TabIndex = 0;
            this.acListBox.Click += new EventHandler(acListBox_Click);
        }
        public ACListBox(ListBox listBox)
        {
            this.acListBox = listBox;
        }

        public void getItem()
        {
            this.acListBox.Items.Add(this.trackName);
        }

        private void acListBox_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Blubb");
        }
    }


Kha - Fr 28.03.08 14:05

Schau dir deinen Code noch einmal an: Du leitest von ListBox ab, erstellst allerdings darin noch einmal eine neue ListBox. Was denn nun ;) ? Lasse das acListBox-Feld weg und auch den ganzen Konstruktor-Code, die Platzierung deiner abgeleiteten ListBox übernimmt schließlich der Form-Designer.
ICh würde allerdings keine GUI auslagern, wenn man das Control nur einmal im ganzen Projekt benötigt. Versuch lieber, zuerst die innere Logik des Formulars auszulagern, sodass nur noch wirklich GUI-abhängiger Code verbleibt.


LocalDeeJay - Fr 28.03.08 15:15

Ahhh, jetzt versteh ich das.
Dankeschön...
Weißt ich mag des überhaupt net wenn ich soviel Code in einer Datei hab. Kann ich garnich ab. Aber jetzt weiß ich ja wies geht dangeschööön =)

Gruß Local