Fragensteller
Breite für CustomToolstripControl berechnen.

Allgemeine Diskussion
-
Hallo,
warum ist die Breite meines Controls immer anders? Die Breite ändert sich bis zu einem Höchstwert,
danach ist sie immer gleich.
[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ToolStrip | ToolStripItemDesignerAvailability.StatusStrip)] public class ComboBoxEx : ToolStripControlHost { private FlowLayoutPanel controlPanel; private MyPanel panel = new MyPanel(); private const int MinWidth = 100; private const int MaxWidth = 200; private const int MinHeight = 22; private const int MaxHeight = 25; public string Titel { get { return panel.Titel; } set { panel.Titel = value; } } public ComboBoxEx() : base(new FlowLayoutPanel()) { // this.AutoSize = false; this.Margin = new Padding(4, 1, 4, 2); controlPanel = (FlowLayoutPanel)base.Control; controlPanel.BackColor = Color.Transparent; controlPanel.SizeChanged += new EventHandler(controlPanel_SizeChanged); controlPanel.Controls.Add(panel); } void controlPanel_SizeChanged(object sender, EventArgs e) { int width = Math.Max(controlPanel.Size.Width, MinWidth); width = Math.Min(width, MaxWidth); int height = Math.Max(controlPanel.Size.Height, MinHeight); height = Math.Min(height, MaxHeight); panel.Size = new Size(width - Margin.Horizontal, height - Margin.Vertical); } internal class MyPanel : UserControl { public MyPanel() { } string titel = "ComboBoxEx"; public string Titel { get { return titel; } set { titel = value; } } protected override Size DefaultSize { get { return new Size(100, 22); } } protected override void OnPaintBackground(PaintEventArgs e) { //base.OnPaintBackground(e); e.Graphics.FillRectangle(new SolidBrush(this.BackColor), e.ClipRectangle); } protected override void OnPaint(PaintEventArgs e) { //base.OnPaint(e); Rectangle rect = new Rectangle(e.ClipRectangle.Location, new System.Drawing.Size(e.ClipRectangle.Width - 1, e.ClipRectangle.Height - 1)); e.Graphics.DrawRectangle(new Pen(Color.Black), rect); e.Graphics.DrawString(Titel, this.Font, new SolidBrush(this.ForeColor), new PointF(3, 3)); } } }
hier wurden 4 ComboBoxEx-Controls dem Toolstrip hinzugefüget.
// // comboBoxEx1 // this.comboBoxEx1.BackColor = System.Drawing.Color.Transparent; this.comboBoxEx1.Margin = new System.Windows.Forms.Padding(4, 1, 4, 2); this.comboBoxEx1.Name = "comboBoxEx1"; this.comboBoxEx1.Size = new System.Drawing.Size(98, 28); this.comboBoxEx1.Text = "comboBoxEx1"; this.comboBoxEx1.Titel = "ComboBoxEx"; // // comboBoxEx2 // this.comboBoxEx2.BackColor = System.Drawing.Color.Transparent; this.comboBoxEx2.Margin = new System.Windows.Forms.Padding(4, 1, 4, 2); this.comboBoxEx2.Name = "comboBoxEx2"; this.comboBoxEx2.Size = new System.Drawing.Size(98, 28); this.comboBoxEx2.Text = "comboBoxEx2"; this.comboBoxEx2.Titel = "ComboBoxEx"; // // comboBoxEx3 // this.comboBoxEx3.BackColor = System.Drawing.Color.Transparent; this.comboBoxEx3.Margin = new System.Windows.Forms.Padding(4, 1, 4, 2); this.comboBoxEx3.Name = "comboBoxEx3"; this.comboBoxEx3.Size = new System.Drawing.Size(104, 28); this.comboBoxEx3.Text = "comboBoxEx3"; this.comboBoxEx3.Titel = "ComboBoxEx"; // // comboBoxEx4 // this.comboBoxEx4.BackColor = System.Drawing.Color.Transparent; this.comboBoxEx4.Margin = new System.Windows.Forms.Padding(4, 1, 4, 2); this.comboBoxEx4.Name = "comboBoxEx4"; this.comboBoxEx4.Size = new System.Drawing.Size(106, 28); this.comboBoxEx4.Text = "comboBoxEx4"; this.comboBoxEx4.Titel = "ComboBoxEx";
Die Breite ist jedesmal anders! Warum?
Wenn ich Form1.Designer schließe und wieder öffne sind alle gleich. Aber nur 98 breit.
// // comboBoxEx1 // this.comboBoxEx1.BackColor = System.Drawing.Color.Transparent; this.comboBoxEx1.Margin = new System.Windows.Forms.Padding(4, 1, 4, 2); this.comboBoxEx1.Name = "comboBoxEx1"; this.comboBoxEx1.Size = new System.Drawing.Size(98, 28); this.comboBoxEx1.Text = "comboBoxEx1"; this.comboBoxEx1.Titel = "ComboBoxEx"; // // comboBoxEx2 // this.comboBoxEx2.BackColor = System.Drawing.Color.Transparent; this.comboBoxEx2.Margin = new System.Windows.Forms.Padding(4, 1, 4, 2); this.comboBoxEx2.Name = "comboBoxEx2"; this.comboBoxEx2.Size = new System.Drawing.Size(98, 28); this.comboBoxEx2.Text = "comboBoxEx2"; this.comboBoxEx2.Titel = "ComboBoxEx"; // // comboBoxEx3 // this.comboBoxEx3.BackColor = System.Drawing.Color.Transparent; this.comboBoxEx3.Margin = new System.Windows.Forms.Padding(4, 1, 4, 2); this.comboBoxEx3.Name = "comboBoxEx3"; this.comboBoxEx3.Size = new System.Drawing.Size(98, 28); this.comboBoxEx3.Text = "comboBoxEx3"; this.comboBoxEx3.Titel = "ComboBoxEx"; // // comboBoxEx4 // this.comboBoxEx4.BackColor = System.Drawing.Color.Transparent; this.comboBoxEx4.Margin = new System.Windows.Forms.Padding(4, 1, 4, 2); this.comboBoxEx4.Name = "comboBoxEx4"; this.comboBoxEx4.Size = new System.Drawing.Size(98, 28); this.comboBoxEx4.Text = "comboBoxEx4"; this.comboBoxEx4.Titel = "ComboBoxEx";
Danke
- Typ geändert Aleksander Chalabashiev Freitag, 6. November 2015 08:37 keine Rückmeldung
Alle Antworten
-
Hallo
ich hab das mal ausprobiert und kann dein Problem so nicht reproduzieren (benutze VS 2015) beim hinzufügen hab ich immer
new System.Drawing.Size(98, 28);
und wenn ich andere Werte benutze werden die auch gespeichert.
Allerdings ist mir was anderes aufgefallen weiß aber nicht ob es mit deinem Problem zusammenhängt nachdem ich das Projekt einmal kompiliere spinnt der Form Designer rum und man kann quasi nichts mehr im Designer sehen oder bearbeiten außer man schließt und öffnet das Projekt wieder das scheint aber ein bekanntes Problem zu sein https://stackoverflow.com/questions/8856394/net-custom-control-toolstripcontrolhost-wreaks-havoc-on-the-designer.
- Bearbeitet B.Keilmann Donnerstag, 22. Oktober 2015 17:13
-
Hallo nochmal,
hab auch nochmal ein bisschen rumprobiert und auch bei mir gab es jetzt diese komischen Einträge da wurde sogar die Höhe einmal geändert !?
new System.Drawing.Size(102, 25);
new System.Drawing.Size(104, 28);
usw.
also für mich sieht das einfach verdammt stark nach einem Bug aus vor allem weil wie sie ja schon geschrieben haben nachdem man die Form1.Designer schließt und wieder öffnet die richtigen Werte gesetzt sind dann aber auch wieder mal nicht.
Diesen Teil im Code hab ich auch mal angepasst
this.Margin = new Padding(4, 1, 4, 2);
zu
this.Margin = new Padding(2, 1, 2, 2);
Dann konnte man auch die richtige DefaultSize von 100 als Breite benutzen aber auch nicht immer wie gesagt kommt mir alles sehr komisch vor und ich sehe da auch sonst keinen offensichtlichen Fehler.
- Bearbeitet B.Keilmann Donnerstag, 22. Oktober 2015 18:43
-
Hallo mf_hw20,
bist Du hier weitegekommen?
Gruß
Aleksander
Bitte haben Sie Verständnis dafür, dass im Rahmen dieses Forums, welches auf dem Community-Prinzip „IT-Pros helfen IT-Pros“ beruht, kein technischer Support geleistet werden kann oder sonst welche garantierten Maßnahmen seitens Microsoft zugesichert werden können.
- Bearbeitet Aleksander Chalabashiev Mittwoch, 4. November 2015 09:49