TabControl Problem

  • C#
  • .NET (FX) 1.0–2.0

Es gibt 13 Antworten in diesem Thema. Der letzte Beitrag () ist von Rikudo.

    TabControl Problem

    Moin,

    Also ich nutze ein custom Tabcontrol das einen Slide Effekt beim TabPage Switchen zeigt, was nicht schlecht aussieht:

    Src Code:

    C#-Quellcode

    1. using System;
    2. using System.Drawing;
    3. using System.Windows.Forms;
    4. public class AnimTab : TabControl
    5. {
    6. int OldIndex;
    7. private int _Speed = 9;
    8. public int Speed
    9. {
    10. get { return _Speed; }
    11. set
    12. {
    13. if (value > 20 | value < -20)
    14. {
    15. MessageBox.Show("Speed needs to be in between -20 and 20.");
    16. }
    17. else
    18. {
    19. _Speed = value;
    20. }
    21. }
    22. }
    23. public AnimTab()
    24. {
    25. SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
    26. }
    27. public void DoAnimationScrollLeft(Control Control1, Control Control2)
    28. {
    29. Graphics G = Control1.CreateGraphics();
    30. Bitmap P1 = new Bitmap(Control1.Width, Control1.Height);
    31. Bitmap P2 = new Bitmap(Control2.Width, Control2.Height);
    32. Control1.DrawToBitmap(P1, new Rectangle(0, 0, Control1.Width, Control1.Height));
    33. Control2.DrawToBitmap(P2, new Rectangle(0, 0, Control2.Width, Control2.Height));
    34. foreach (Control c in Control1.Controls)
    35. {
    36. c.Hide();
    37. }
    38. int Slide = Control1.Width - (Control1.Width % _Speed);
    39. int a = 0;
    40. for (a = 0; a <= Slide; a += _Speed)
    41. {
    42. G.DrawImage(P1, new Rectangle(a, 0, Control1.Width, Control1.Height));
    43. G.DrawImage(P2, new Rectangle(a - Control2.Width, 0, Control2.Width, Control2.Height));
    44. }
    45. a = Control1.Width;
    46. G.DrawImage(P1, new Rectangle(a, 0, Control1.Width, Control1.Height));
    47. G.DrawImage(P2, new Rectangle(a - Control2.Width, 0, Control2.Width, Control2.Height));
    48. SelectedTab = (TabPage)Control2;
    49. foreach (Control c in Control2.Controls)
    50. {
    51. c.Show();
    52. }
    53. foreach (Control c in Control1.Controls)
    54. {
    55. c.Show();
    56. }
    57. }
    58. protected override void OnSelecting(System.Windows.Forms.TabControlCancelEventArgs e)
    59. {
    60. if (OldIndex < e.TabPageIndex)
    61. {
    62. DoAnimationScrollRight(TabPages[OldIndex], TabPages[e.TabPageIndex]);
    63. }
    64. else
    65. {
    66. DoAnimationScrollLeft(TabPages[OldIndex], TabPages[e.TabPageIndex]);
    67. }
    68. }
    69. protected override void OnDeselecting(System.Windows.Forms.TabControlCancelEventArgs e)
    70. {
    71. OldIndex = e.TabPageIndex;
    72. }
    73. public void DoAnimationScrollRight(Control Control1, Control Control2)
    74. {
    75. Graphics G = Control1.CreateGraphics();
    76. Bitmap P1 = new Bitmap(Control1.Width, Control1.Height);
    77. Bitmap P2 = new Bitmap(Control2.Width, Control2.Height);
    78. Control1.DrawToBitmap(P1, new Rectangle(0, 0, Control1.Width, Control1.Height));
    79. Control2.DrawToBitmap(P2, new Rectangle(0, 0, Control2.Width, Control2.Height));
    80. foreach (Control c in Control1.Controls)
    81. {
    82. c.Hide();
    83. }
    84. int Slide = Control1.Width - (Control1.Width % _Speed);
    85. int a = 0;
    86. for (a = 0; a >= -Slide; a += -_Speed)
    87. {
    88. G.DrawImage(P1, new Rectangle(a, 0, Control1.Width, Control1.Height));
    89. G.DrawImage(P2, new Rectangle(a + Control2.Width, 0, Control2.Width, Control2.Height));
    90. }
    91. a = Control1.Width;
    92. G.DrawImage(P1, new Rectangle(a, 0, Control1.Width, Control1.Height));
    93. G.DrawImage(P2, new Rectangle(a + Control2.Width, 0, Control2.Width, Control2.Height));
    94. SelectedTab = (TabPage)Control2;
    95. foreach (Control c in Control2.Controls)
    96. {
    97. c.Show();
    98. }
    99. foreach (Control c in Control1.Controls)
    100. {
    101. c.Show();
    102. }
    103. }
    104. }


    Das ganze funktioniert soweit auch recht nett, aber es gibt ein kleines Problem:
    Beim ersten ansprechen (durchswitchen) der TabPages, tauchen die Controls auf der nächsten Seite schlagartig erst auf wenn der slide effect fertig ist.
    ist man bereits ein zweites mal durchgeswitcht geht das fließend und die controls sind in dem slide effekt zu sehen.
    Kann mir jemand helfen wie ich den Code anpassen muss das auch zu beginn (beim ersten switchen) der slide effekt zu sehen ist?
    Habe mich schon durch gedebugged und diverse Loops getauscht und geändert, aber kein ergebnis erzielt :/
    C# Developer
    Learning C++

    Rikudo schrieb:

    kleines Problem
    Was für und wie viele Controls hast Du denn auf Deinen Tabs?
    Ich hab das mit 6 Buttons probiert und das sieht ganz gut aus.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Checkboxen, buttons, pictureboxen usw.
    Bist du sicher das der Effekt auch zu Beginn funzt?

    Hier eine GIF Animation zur verdeutlichung :

    C# Developer
    Learning C++

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Rikudo“ ()

    Rikudo schrieb:

    zu Beginn
    Ich sehe nichts ungewöhnliches.
    Nimm mal testweise die PictureBoxen raus.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!

    Rikudo schrieb:

    das Problem ist das selbe
    Dann sehe wohl ich nicht das, was Du siehst. :/
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    @RodFromGermany:null
    Also, guck bei der GIF mal genau hin xD.

    Beim 1) Versuch, bin ich zu Beginn auf TabPage1.
    Danach klicke ich auf TabPage2.
    Was passiert hier: Die Controls von TabPage1 sliden nach links weg, und von rechts slided eine weiße fläche herrein wenn diese vollständig erschienen ist, dann tauchen die Controls plötzlich auf.
    -> So soll es nicht sein.

    Beim 2) Versuch, bin ich zu Beginn auf TabPage1.Danach klicke ich wieder auf TabPage2, die Controls sliden nach links weg, und von rechts sliden die Controls von TabPage2 herrein, nicht wie beim 1) Versuch nur eine weiße fläche!!
    -> So soll es sein, also Versuch 2 ist das was ich erreichen möchte, und Versuch 1 das Problem, so wie es nicht sein soll.
    Hab ichs halbwegs verständlich erklärt? :D
    C# Developer
    Learning C++

    Rikudo schrieb:

    eine weiße fläche herrein
    Nö, bei mir definitiv nicht.
    Ist Dein Rechner etwas müde?
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    mal auf den rechts slide bezogen, ist doch glaube der von tab1 auf tab2 oder hab ichn denkfehler..

    was kommt denn bei raus, wenn du statt

    VB.NET-Quellcode

    1. for (a = 0; a >= -Slide; a += -_Speed)
    2. {
    3. G.DrawImage(P1, new Rectangle(a, 0, Control1.Width, Control1.Height));
    4. G.DrawImage(P2, new Rectangle(a + Control2.Width, 0, Control2.Width, Control2.Height));
    5. }
    6. a = Control1.Width;
    7. G.DrawImage(P1, new Rectangle(a, 0, Control1.Width, Control1.Height));
    8. G.DrawImage(P2, new Rectangle(a + Control2.Width, 0, Control2.Width, Control2.Height));
    9. SelectedTab = (TabPage)Control2;
    10. foreach (Control c in Control2.Controls)
    11. {
    12. c.Show();
    13. }
    14. foreach (Control c in Control1.Controls)
    15. {
    16. c.Show();
    17. }


    das so veränderst

    VB.NET-Quellcode

    1. for (a = 0; a >= -Slide; a += -_Speed)
    2. {
    3. G.DrawImage(P1, new Rectangle(a, 0, Control1.Width, Control1.Height));
    4. G.DrawImage(P2, new Rectangle(a + Control2.Width, 0, Control2.Width, Control2.Height));
    5. }
    6. a = Control1.Width;
    7. G.DrawImage(P1, new Rectangle(a, 0, Control1.Width, Control1.Height));
    8. G.DrawImage(P2, new Rectangle(a + Control2.Width, 0, Control2.Width, Control2.Height));
    9. foreach (Control c in Control2.Controls)
    10. {
    11. c.Show();
    12. }
    13. SelectedTab = (TabPage)Control2;
    14. foreach (Control c in Control1.Controls)
    15. {
    16. c.Show();
    17. }


    oder vllt noch

    VB.NET-Quellcode

    1. for (a = 0; a >= -Slide; a += -_Speed)
    2. {
    3. G.DrawImage(P1, new Rectangle(a, 0, Control1.Width, Control1.Height));
    4. G.DrawImage(P2, new Rectangle(a + Control2.Width, 0, Control2.Width, Control2.Height));
    5. }
    6. a = Control1.Width;
    7. foreach (Control c in Control2.Controls)
    8. {
    9. c.Show();
    10. }
    11. G.DrawImage(P1, new Rectangle(a, 0, Control1.Width, Control1.Height));
    12. G.DrawImage(P2, new Rectangle(a + Control2.Width, 0, Control2.Width, Control2.Height));
    13. SelectedTab = (TabPage)Control2;
    14. foreach (Control c in Control1.Controls)
    15. {
    16. c.Show();
    17. }


    Sorry, ich probiere manchmal ohne groß zu überlegen..^^

    Rikudo schrieb:

    Nein?

    C#-Quellcode

    1. _Speed = 2;
    da isses zu sehen.
    ----------------------
    Ich hab mal etwas gespielt und die Tabs seeeehr groß gemacht.
    Dateien
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „RodFromGermany“ ()

    @BitBen :
    Das ändert leider auch nichts :/

    @RodFromGermany :
    Seltsam, ich hab deinen Code genau übernommen aber bei mir klappts nicht.
    Ich hab einmal im Loadevent der Form jede TabPage kurz selektiert, aber der effect ist nicht da :|
    In deinem projekt funzt es aber :/
    C# Developer
    Learning C++

    Rikudo schrieb:

    In deinem projekt funzt es aber
    sogar im Designer.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!