Autor Beitrag
DareDevil
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 70

Windows7
C# (VS 2010)
BeitragVerfasst: Mi 13.07.11 09:30 
Ich habe ein UserControl (UC1) in dem oben rechts ein Button mit dem Anchorwert Top/Right.
Dann habe ich noch ein UserControl (UC2) welchen von UC1 abgeleitet ist.

Im Formsdesigner wird der Button immer oben rechts im UC2 angezeigt, egal welche Größe UC2 hat.
Jedoch wenn ich nun das Programm starte, stimmt die Button Position im UC2 zur Laufzeit nicht mehr überein. Der Button ist nun oben mittig.

Hat vielleicht jemand ne Idee woran dies liegt.

Gruß
Pascal
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4798
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Mi 13.07.11 09:44 
Hallo DareDevil,

das kann ich mir nur so erklären, daß du irgendwo im Code den Anchor veränderst (vom Designer wird ja nur der Konstruktor - und dessen InitializeComponent-Methode - aufgerufen).
DareDevil Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 70

Windows7
C# (VS 2010)
BeitragVerfasst: Mi 13.07.11 09:57 
Hi Th69,

der Anchor wird leider nicht im Code verändert.

Wenn ich zur Laufzeit die Größe von UC2 verändere bewegt sich der Button mit, er wird halt nur initial nicht an der richtigen Position erstellt/angezeigt.
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4798
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Mi 13.07.11 11:04 
Hat dein UserControl UC2 evtl. initial eine andere Größe als das Basis-UserControl UC1?
Ansonsten überprüfe mal alle (fett angezeigten) Eigenschaften vom UC1, UC2 sowie dem Button im Eigenschaftenfenster (evtl. testweise mal einige Eigenschaften wieder auf 'default' setzen).
DareDevil Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 70

Windows7
C# (VS 2010)
BeitragVerfasst: Mi 13.07.11 11:34 
Ja UC2 hat eine andere initial Größe als UC1.
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4798
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Mi 13.07.11 12:39 
Dann scheint das wohl den Fehler hervorzurufen.
Könntest du mal die Initialgröße von UC1 so groß (oder sogar größer) als das UC2 machen?
DareDevil Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 70

Windows7
C# (VS 2010)
BeitragVerfasst: Mi 13.07.11 13:11 
Ich denke mal den Effect hast dir schon fast gedacht.

Der Button ist nun außerhalb des sichtbaren Bereiches von UC2.
Bleibt mir wohl nichts anderes übrig als die Position beim erstellen selber zu berechnen.
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4798
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Mi 13.07.11 13:29 
Probiere mal aus, was passiert, wenn du die Anchor-Zuweisung nach der InitializeComponents() machst:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
UC1()
{
  InitializeComponents();

  button.Anchor = AnchorStyles.Top | AnchorStyles.Right;
}

(und dann die Anchor-Zuweisung per Designer wieder auf 'default', d.h. Top|Left setzt).

Ich hatte auch schon manchmal eigenartige Effekte, wenn man bestimmte WinForms-Eigenschaften per Designer (in Kombination) setzt...
DareDevil Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 70

Windows7
C# (VS 2010)
BeitragVerfasst: Mi 13.07.11 13:48 
Hilft leider nicht.
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4798
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Mi 13.07.11 14:25 
Tja, dann bleibt wohl nur im Resize-Ereignis des UC1 die Position selber berechnen...
DareDevil Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 70

Windows7
C# (VS 2010)
BeitragVerfasst: Mi 13.07.11 14:36 
Anchor vom Button auf Top Right stellen.
Nach dem Einfügen des UC2 in die Form, die Position des Buttons anpassen. Danach geht alles wie es soll mit dem Anchor.