TabPage Multiline Text

  • C#

Es gibt 3 Antworten in diesem Thema. Der letzte Beitrag () ist von RodFromGermany.

    TabPage Multiline Text

    Moin Leute, ich meine nicht die Property Multiline des TabControls, sondern eine der TabPage:

    Auf dieser Seite "Tab Pages with multiline text" fand ich das, was ich suche. Allerdings hab ich da trotz Anmeldung keinen Zugriff auf das beschriebene TabControl.
    Kennt einer von Euch das Projekt und kann mal diese TabPageEx bzw. einen funktionierenden Code anhängen?
    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
    Hab' da 'grad noch was gefunden:
    bytes.com/topic/c-sharp/answer…rap-tab-title-tab-control
    Mick Doherty
    Not supported, but not impossible.
    You'll need to set the text at runtime rather than in the property browser.
    With VisualStyles enabled you can get the string to wrap so long as it does not have the ampersand in the string.
    i.e. "house and\ngarden" will wrap but "house &\ngarden" will not (I have no idea why). You then need to adjust the TabControls padding to allow for the wrap.
    @us4711 So soll es sein, danke.
    Spoiler anzeigen

    C#-Quellcode

    1. // im Designer
    2. this.tabControl1.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed;
    3. this.tabControl1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.tabControl1_DrawItem);
    4. this.tabPage1.Text = "tab\\nPage1"; // hier wird der Text-Break kundgetan

    C#-Quellcode

    1. in der Main-Form
    2. using System.Collections;
    3. using System.Drawing;
    4. using System.Windows.Forms;
    5. namespace WindowsFormsApplication1
    6. {
    7. public partial class Form1 : Form
    8. {
    9. private Hashtable multiLineTabs = new Hashtable();
    10. private bool multiLine;
    11. public Form1()
    12. {
    13. this.InitializeComponent();
    14. }
    15. private void tabControl1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
    16. {
    17. TabControl tabControl = (TabControl)sender;
    18. TabPage currentTab = tabControl.TabPages[e.Index];
    19. Graphics g = e.Graphics;
    20. StringFormat sf = new StringFormat();
    21. sf.Alignment = StringAlignment.Center;
    22. sf.LineAlignment = StringAlignment.Center;
    23. string tabText = currentTab.Text;
    24. RectangleF tabRect = (RectangleF)e.Bounds;
    25. RectangleF textRect = tabRect;
    26. if (e.Index == tabControl.SelectedIndex)
    27. {
    28. tabRect.Inflate(1, 1);
    29. }
    30. g.Clip = new Region(tabRect);
    31. g.Clear(Control.DefaultBackColor);
    32. g.ResetClip();
    33. if (this.multiLine)
    34. {
    35. if (this.multiLineTabs.Contains(currentTab))
    36. {
    37. tabText += "\n" + (string)this.multiLineTabs[currentTab];
    38. }
    39. }
    40. g.DrawString(tabText, e.Font, SystemBrushes.ControlText, textRect, sf);
    41. }
    42. private void Form1_Load(object sender, System.EventArgs e)
    43. {
    44. foreach (TabPage tab in this.tabControl1.TabPages)
    45. {
    46. int newLineIndex = tab.Text.IndexOf(@"\n");
    47. if (newLineIndex != -1)
    48. {
    49. if (!this.multiLine)
    50. {
    51. this.multiLine = true;
    52. this.tabControl1.Padding = new Point(6, tabControl1.Font.Height);
    53. }
    54. this.multiLineTabs.Add(tab, tab.Text.Remove(0, newLineIndex + 2));
    55. tab.Text = tab.Text.Substring(0, newLineIndex);
    56. }
    57. }
    58. }
    59. }
    60. }
    Bilder
    • TabControl.jpg

      5,04 kB, 218×167, 243 mal angesehen
    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!