Hallo,
ich bin noch nicht so lange in der C# Welt Unterwegs, und mein Hauptproblem scheint das Objektorientierte Denken zu sein
Ich bin dabei eine Windows Anwendung zu programmieren. Diese ist so aufgebaut das ein SplitContainer das Haupt Formular in 2 Bereiche teilt (rechts/links). Im linken Bereich befindet sich die Navigation. Im rechten Bereich lade ich UserControls in ein Panel.
Im Linken Bereich lade ich ein User Control bestückt mit Buttons, das sieht so aus:
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: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74:
| namespace PetersCSharpApp.UserControls.Customer{ public partial class CustomerBar : UserControl { public CustomerBar() { InitializeComponent(); }
private void CustomerBar_Click(object sender, OutlookStyleControls.OutlookBar.ButtonClickEventArgs e) { int idx = outlookBarCustomer.Buttons.IndexOf(e.SelectedButton); switch (idx) { case 0: PanelControl PC = new PanelControl(); SplitContainer mySplitContainer = FindObjectbyName("splitContainerForm1"); PC.SetPanelContentControl(new PetersCSharpApp.UserControls.Customer.CustomerAdd(), mySplitContainer); break; case 1: PanelControl PC1 = new PanelControl(); SplitContainer mySplitContainer1 = FindObjectbyName("splitContainerForm1"); PC1.SetPanelContentControl(new PetersCSharpApp.UserControls.Customer.CustomerList(), mySplitContainer1); break; default: MessageBox.Show("Klick Button" + (int)idx); break; }
} public SplitContainer FindChildByName(ref string name, Control par) {
SplitContainer c = null; foreach (SplitContainer cc in par.Controls) {
if (cc.Name == name) return cc;
if (cc.Controls.Count > 0) { c = FindChildByName(ref name, cc); if (c != null) return c; }
} return null; }
public SplitContainer FindObjectbyName(string name) { SplitContainer c = null;
if (this.Parent.Parent.Parent.Parent.Parent != null && this.Parent.Parent.Parent.Parent.Parent.Controls.Count > 0) foreach (SplitContainer cc in this.Parent.Parent.Parent.Parent.Parent.Controls) { MessageBox.Show(cc.Name); if (cc.Name == name) return cc; if (cc.Controls.Count > 0) { c = FindChildByName(ref name, cc); if (c != null) return c;
} } return null; } }} |
Hier meine Klasse welche das ein/ausblenden der Panels steuert:
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: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58:
| namespace PetersCSharpApp.UserControls.Customer{ class PanelControl {
public void SetPanelBarControl(Control newControl, SplitContainer mySplitContainer) {
Panel panel = mySplitContainer.Panel2;
panel.SuspendLayout(); while (panel.Controls.Count > 0) { Control oldControl = panel.Controls[0]; panel.Controls.RemoveAt(0); oldControl.Dispose(); } panel.Controls.Add(newControl); newControl.Dock = DockStyle.Fill; newControl.Visible = true; newControl.Focus();
panel.ResumeLayout(true); }
public void SetPanelContentControl(Control newControl, SplitContainer mySplitContainer) {
Panel panel = mySplitContainer.Panel2;
panel.SuspendLayout(); while (panel.Controls.Count > 0) { Control oldControl = panel.Controls[0]; panel.Controls.RemoveAt(0); oldControl.Dispose(); } panel.Controls.Add(newControl); newControl.Dock = DockStyle.Fill; newControl.Visible = true; newControl.Focus();
panel.ResumeLayout(true); }
}} |
Das funktioniert auch wunderbar. Nun aber habe ich ein anderes UserControl mit eigener Klasse, dieses beinhaltet einen Button, ein Klick auf diesen Button soll mir die gleiche Funktion ausführen wie oben wenn man im Linken Bereich einen Button anklickt:
C#-Quelltext
1: 2: 3:
| PanelControl PC = new PanelControl(); SplitContainer mySplitContainer = FindObjectbyName("splitContainerForm1"); PC.SetPanelContentControl(new PetersCSharpApp.UserControls.Customer.CustomerAdd(), |
new Panel Control geht klar... aber die Methode FindObjectbyName wird natürlich nicht gefunden weil diese oben in meiner Klasse CustomerBar ist.
Nun wollte ich diese Methode in die Klasse PanelControl hinterlegen, dort funktioniert Sie aber nicht mehr weil keine Definition für "Parent" gefunden wurde .
Die Funktion soll das Object splitContainerForm1 finden und es dann an SetPanelContentControl weitergeben um es dort zu steuern.
Ich weiß nun nicht wo ich diese Methode zentral unterbringen kann oder wie ich diese in der Klasse PanelControl implementiere so das ich diese nicht in mehreren User Controls kopieren muss. Da ich die Methode aber öfter aufrufen möchte von verschiedenen UserControls aus , wäre es doof diese jedes mal zu kopieren.
Vielleicht hat jemand ein rat für mich . Danke
Gruß
elTorito