UserControl-Properties setzen sich immer wieder zurück (SmartTag/ActionList))

  • C#

Es gibt 10 Antworten in diesem Thema. Der letzte Beitrag () ist von TRiViUM.

    UserControl-Properties setzen sich immer wieder zurück (SmartTag/ActionList))

    Hallo liebe Community,

    ich habe ein Problem mit meinem UserControl, wo Ihr vielleicht eine Erklärung dafür parat habt.

    Mein UserControl besteht im Prinzip aus 2 Labels.
    Um diese 2 Labels beschreiben zu können, habe ich 2 Properties vom Typ String angelegt.

    Verweis auf System.Design erforderlich.

    Das UserControl:
    Spoiler anzeigen

    C#-Quellcode

    1. [Designer(typeof(WindowsTitleDesigner))]
    2. public partial class WindowsTitle : UserControl
    3. {
    4. public WindowsTitle()
    5. {
    6. InitializeComponent();
    7. Dock = DockStyle.Top;
    8. }
    9. [EditorBrowsable(EditorBrowsableState.Always )]
    10. [Browsable(true)]
    11. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    12. public string Title
    13. {
    14. get { return lblTitle.Text; }
    15. set { lblTitle.Text = value; }
    16. }
    17. [EditorBrowsable( EditorBrowsableState.Always )]
    18. [Browsable( true )]
    19. [DesignerSerializationVisibility( DesignerSerializationVisibility.Content )]
    20. public string Description
    21. {
    22. get { return lblDescription.Text; }
    23. set { lblDescription.Text = value; }
    24. }
    25. }
    26. public class Line : Control
    27. {
    28. public Line()
    29. {
    30. SetStyle( ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint, true );
    31. lineColor = Color.Black;
    32. LineAlignment = Alignment.Horizontal;
    33. Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
    34. Size = new Size( 250, 1 );
    35. }
    36. public enum Alignment
    37. {
    38. Vertical,
    39. Horizontal,
    40. }
    41. private Color lineColor;
    42. private Alignment lineAlignment;
    43. public Color LineColor
    44. {
    45. get { return lineColor; }
    46. set
    47. {
    48. lineColor = value;
    49. Invalidate();
    50. }
    51. }
    52. public Alignment LineAlignment
    53. {
    54. get { return lineAlignment; }
    55. set
    56. {
    57. lineAlignment = value;
    58. Invalidate();
    59. }
    60. }
    61. protected override void OnPaint(PaintEventArgs e)
    62. {
    63. base.OnPaint( e );
    64. if (lineAlignment == Alignment.Horizontal)
    65. e.Graphics.DrawLine( new Pen( new SolidBrush( lineColor ), 1 ), new Point( 0, Height / 2 ), new Point( Width, Height / 2 ) );
    66. else if (lineAlignment == Alignment.Vertical)
    67. e.Graphics.DrawLine( new Pen( new SolidBrush( lineColor ), 1 ), new Point( 0, Height / 2 ), new Point( 0, Height / 2 ) );
    68. }
    69. protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
    70. {
    71. base.SetBoundsCore( x, y, width, height, specified );
    72. }
    73. }
    74. public class WindowsTitleDesigner : System.Windows.Forms.Design.ControlDesigner
    75. {
    76. #region variables
    77. private DesignerActionListCollection lists;
    78. #endregion
    79. #region callbacks
    80. public override DesignerActionListCollection ActionLists
    81. {
    82. get
    83. {
    84. if (lists == null)
    85. {
    86. lists = new DesignerActionListCollection
    87. {
    88. new WindowsTitleActionList( this.Component )
    89. };
    90. }
    91. return lists;
    92. }
    93. }
    94. #endregion
    95. }
    96. public class WindowsTitleActionList : DesignerActionList
    97. {
    98. #region constructor
    99. public WindowsTitleActionList(IComponent component) : base( component )
    100. {
    101. _ctrl = (WindowsTitle)component;
    102. this.designerActionSvc = (DesignerActionUIService)GetService( typeof( DesignerActionUIService ) );
    103. }
    104. #endregion
    105. #region variables
    106. private readonly WindowsTitle _ctrl;
    107. private readonly DesignerActionUIService designerActionSvc = null/* TODO Change to default(_) if this is not a reference type */;
    108. #endregion
    109. #region properties
    110. public string Title
    111. {
    112. get
    113. {
    114. return _ctrl.Title;
    115. }
    116. set
    117. {
    118. _ctrl.Title = value;
    119. }
    120. }
    121. public string Description
    122. {
    123. get
    124. {
    125. return _ctrl.Description;
    126. }
    127. set
    128. {
    129. _ctrl.Description = value;
    130. }
    131. }
    132. #endregion
    133. #region callbacks
    134. public override System.ComponentModel.Design.DesignerActionItemCollection GetSortedActionItems()
    135. {
    136. DesignerActionItemCollection items = new DesignerActionItemCollection();
    137. //items.Add( new DesignerActionHeaderItem( "--- ActionList ---" ) );
    138. items.Add( new DesignerActionHeaderItem( "Eigenschaften" ) );
    139. items.Add( new DesignerActionPropertyItem( "Title", "Titel:", "Eigenschaften", "Der anzuzeigende Titel." ) );
    140. items.Add( new DesignerActionPropertyItem( "Description", "Beschreibung:", "Eigenschaften", "Die anzuzeigende Beschreibung." ) );
    141. return ( items );
    142. }
    143. #endregion
    144. }


    Und der Designer-Teil:
    Spoiler anzeigen

    C#-Quellcode

    1. partial class WindowsTitle
    2. {
    3. /// <summary>
    4. /// Erforderliche Designervariable.
    5. /// </summary>
    6. private System.ComponentModel.IContainer components = null;
    7. /// <summary>
    8. /// Verwendete Ressourcen bereinigen.
    9. /// </summary>
    10. /// <param name="disposing">True, wenn verwaltete Ressourcen gelöscht werden sollen; andernfalls False.</param>
    11. protected override void Dispose(bool disposing)
    12. {
    13. if (disposing && ( components != null ))
    14. {
    15. components.Dispose();
    16. }
    17. base.Dispose( disposing );
    18. }
    19. #region Vom Komponenten-Designer generierter Code
    20. /// <summary>
    21. /// Erforderliche Methode für die Designerunterstützung.
    22. /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
    23. /// </summary>
    24. private void InitializeComponent()
    25. {
    26. this.lblDescription = new System.Windows.Forms.Label();
    27. this.lblTitle = new System.Windows.Forms.Label();
    28. this.line2 = new Line();
    29. this.SuspendLayout();
    30. //
    31. // lblDescription
    32. //
    33. this.lblDescription.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
    34. | System.Windows.Forms.AnchorStyles.Left)
    35. | System.Windows.Forms.AnchorStyles.Right)));
    36. this.lblDescription.AutoEllipsis = true;
    37. this.lblDescription.Location = new System.Drawing.Point(12, 39);
    38. this.lblDescription.Name = "lblDescription";
    39. this.lblDescription.Size = new System.Drawing.Size(221, 33);
    40. this.lblDescription.TabIndex = 6;
    41. this.lblDescription.Text = "Description";
    42. //
    43. // lblTitle
    44. //
    45. this.lblTitle.AutoSize = true;
    46. this.lblTitle.Font = new System.Drawing.Font("Segoe UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    47. this.lblTitle.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(51)))), ((int)(((byte)(153)))));
    48. this.lblTitle.Location = new System.Drawing.Point(12, 12);
    49. this.lblTitle.Name = "lblTitle";
    50. this.lblTitle.Size = new System.Drawing.Size(39, 21);
    51. this.lblTitle.TabIndex = 7;
    52. this.lblTitle.Text = "Title";
    53. //
    54. // line2
    55. //
    56. this.line2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
    57. | System.Windows.Forms.AnchorStyles.Right)));
    58. this.line2.LineAlignment = Line.Alignment.Horizontal;
    59. this.line2.LineColor = System.Drawing.Color.Gainsboro;
    60. this.line2.Location = new System.Drawing.Point(12, 75);
    61. this.line2.Name = "line2";
    62. this.line2.Size = new System.Drawing.Size(221, 1);
    63. this.line2.TabIndex = 8;
    64. this.line2.Text = "line2";
    65. //
    66. // WindowsTitle
    67. //
    68. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    69. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    70. this.BackColor = System.Drawing.Color.White;
    71. this.Controls.Add(this.lblDescription);
    72. this.Controls.Add(this.lblTitle);
    73. this.Controls.Add(this.line2);
    74. this.Name = "WindowsTitle";
    75. this.Size = new System.Drawing.Size(245, 78);
    76. this.ResumeLayout(false);
    77. this.PerformLayout();
    78. }
    79. #endregion
    80. private System.Windows.Forms.Label lblDescription;
    81. private System.Windows.Forms.Label lblTitle;
    82. private Line line2;
    83. }


    Mein Problem ist nun, dass sich beide Properties, also Title und Description immer wieder zurücksetzen, wenn ich das Projekt neu kompiliere oder starte...

    Weiß jemand, warum ?(

    Dieser Beitrag wurde bereits 7 mal editiert, zuletzt von „TRiViUM“ ()

    Dann lass die Zeilen#14 und #23 weg ([DesignerSerializationVisibility(DesignerSerializationVisibility.Content)])
    Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von „VaporiZed“, mal wieder aus Grammatikgründen.

    Aufgrund spontaner Selbsteintrübung sind all meine Glaskugeln beim Hersteller. Lasst mich daher bitte nicht den Spekulatiusbackmodus wechseln.
    Bei mir schon. Ich habe nur Deinen Code kopiert.

    Ich lad das gleich mal als Testprojekt hoch.
    Dateien
    • WinFormsCS.zip

      (27,11 kB, 39 mal heruntergeladen, zuletzt: )
    Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von „VaporiZed“, mal wieder aus Grammatikgründen.

    Aufgrund spontaner Selbsteintrübung sind all meine Glaskugeln beim Hersteller. Lasst mich daher bitte nicht den Spekulatiusbackmodus wechseln.
    Interessant, Dein Projekt funktioniert bei mir auch 8|
    Kann sowas mit den VS-Einstellungen zusammen hängen?

    Okay, also scheinbar ist mein Projekt irgendwie verbogen...Danke für den Hinweis!

    Edit:
    Jetzt geht es plötzlich in meinem Projekt auch.
    Ich hab das Gefühl, dass VS nicht wirklich neu kompiliert hat, obwohl es im Ausgabefenster so stand, nachdem ich F6 oder auf Starten geklickt habe...
    Erst, nachdem ich eine Änderung im Designer gemacht habe, hat es funktioniert...

    @VaporiZed
    Zu früh gefreut.
    Wenn ich Dein Projekt mehrmals starte und zwischendrin die Texte ändere, verschwindet irgendwann mein Text wieder :(

    Hier eine Gif-Animation:

    Dieser Beitrag wurde bereits 4 mal editiert, zuletzt von „TRiViUM“ ()

    Zuminest da lässt sich das Problem schneller identifizieren. Wenn der Text nicht in der FormX.Designer.VB geändert wird, sondern nur auf dem GUI, dann ist es keine dauerhafte Änderung. Ändere ich die Eigenschaften des UserControls, klappt es. Gehe ich über den SmartTag, ist die Änderung temporär. Daher ist jetzt im SmartTag das Problem zu suchen.
    Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von „VaporiZed“, mal wieder aus Grammatikgründen.

    Aufgrund spontaner Selbsteintrübung sind all meine Glaskugeln beim Hersteller. Lasst mich daher bitte nicht den Spekulatiusbackmodus wechseln.
    Das habe ich noch nicht geprüft, habe die Änderungen bislang auch nur über den SmartTag gemacht.
    Diese Info war das, was ich gebraucht habe.
    Ich suche das Problem nun im SmartTag, danke für den Hinweis! :)

    Edit:
    Ich dachte, es hätte evtl. auch was damit zu tun, dass ich von UserControl erbe (in Kombination mit SmartTag/ActionList).
    Also habe ich das Steuerelement nur von Control erben lassen und alles selber gezeichnet.
    Spoiler anzeigen

    C#-Quellcode

    1. using System.Drawing;
    2. using System.Windows.Forms;
    3. using System.ComponentModel;
    4. using System.ComponentModel.Design;
    5. using System.Windows.Forms.Design;
    6. namespace ControlSandbox.Ui.Controls
    7. {
    8. [Designer( typeof( WindowsTitleDesigner ) )]
    9. public class WindowsTitle : Control
    10. {
    11. #region constructor
    12. public WindowsTitle()
    13. {
    14. SetStyle( ControlStyles.OptimizedDoubleBuffer |
    15. ControlStyles.ResizeRedraw |
    16. ControlStyles.FixedHeight |
    17. ControlStyles.SupportsTransparentBackColor |
    18. ControlStyles.UserPaint
    19. , true );
    20. Dock = DockStyle.Top;
    21. _title = "Title";
    22. _description = "Description";
    23. Height = 65;
    24. }
    25. #endregion
    26. #region constants
    27. private readonly Color TITLE_COLOR = Color.FromArgb( 0, 51, 153 );
    28. private readonly Font TITLE_FONT = new Font( "Segoe UI", 12f );
    29. private readonly Color DESCRIPTION_COLOR = Color.FromArgb( 0, 0, 0 );
    30. private readonly Font DESCRIPTION_FONT = new Font( "Segoe UI", 8.25f );
    31. private const int TITLE_DESCRIPTION_SPACE = 7;
    32. private const int BORDER_SPACE = 12;
    33. private readonly Color LINE_COLOR = Color.Gainsboro;
    34. #endregion
    35. #region properties
    36. private string _title;
    37. public string Title
    38. {
    39. get { return _title; }
    40. set
    41. {
    42. _title = value;
    43. Invalidate();
    44. }
    45. }
    46. private string _description;
    47. [Editor( typeof( MultilineStringEditor ), typeof( System.Drawing.Design.UITypeEditor ) )]
    48. public string Description
    49. {
    50. get { return _description; }
    51. set
    52. {
    53. _description = value;
    54. Invalidate();
    55. }
    56. }
    57. #endregion
    58. #region callbacks
    59. protected override void OnPaint(PaintEventArgs e)
    60. {
    61. base.OnPaint( e );
    62. e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
    63. e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
    64. e.Graphics.PageUnit = GraphicsUnit.Display;
    65. e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighSpeed;
    66. StringFormat format = new StringFormat() { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Near };
    67. SizeF titleSize = e.Graphics.MeasureString( _title, TITLE_FONT );
    68. Rectangle titleRect = new Rectangle( BORDER_SPACE, BORDER_SPACE, Width - BORDER_SPACE, (int)titleSize.Height );
    69. //e.Graphics.DrawString( _title, TITLE_FONT, new SolidBrush( TITLE_COLOR ), titleRect, format );
    70. TextRenderer.DrawText( e.Graphics, _title, TITLE_FONT, titleRect, TITLE_COLOR, TextFormatFlags.Default );
    71. SizeF descriptionSize = e.Graphics.MeasureString( _description, DESCRIPTION_FONT );
    72. Rectangle descriptionRect = new Rectangle( BORDER_SPACE +2 , titleRect.Y + titleRect.Height + TITLE_DESCRIPTION_SPACE, Width - BORDER_SPACE - BORDER_SPACE - 2, Height - 1 - (int)descriptionSize.Height );
    73. //e.Graphics.DrawString( _description, DESCRIPTION_FONT, new SolidBrush( DESCRIPTION_COLOR ), descriptionRect, format );
    74. TextRenderer.DrawText( e.Graphics, _description, DESCRIPTION_FONT, descriptionRect, DESCRIPTION_COLOR, TextFormatFlags.Default );
    75. e.Graphics.DrawLine( new Pen( LINE_COLOR ), BORDER_SPACE, Height -1, Width - BORDER_SPACE - 1, Height-1 );
    76. }
    77. #endregion
    78. }
    79. public class WindowsTitleDesigner : System.Windows.Forms.Design.ControlDesigner
    80. {
    81. #region variables
    82. private DesignerActionListCollection lists;
    83. #endregion
    84. #region properties
    85. private WindowsTitle HostControl
    86. {
    87. get { return (WindowsTitle)Control; }
    88. }
    89. #endregion
    90. #region callbacks
    91. public override DesignerActionListCollection ActionLists
    92. {
    93. get
    94. {
    95. if (lists == null)
    96. {
    97. lists = new DesignerActionListCollection
    98. {
    99. new WindowsTitleActionList( this.Component )
    100. };
    101. }
    102. return lists;
    103. }
    104. }
    105. public override SelectionRules SelectionRules
    106. {
    107. get
    108. {
    109. //SelectionRules selRules = SelectionRules.Moveable;
    110. //rules = rules | SelectionRules.none
    111. return base.SelectionRules;
    112. }
    113. }
    114. #endregion
    115. }
    116. public class WindowsTitleActionList : DesignerActionList
    117. {
    118. #region constructor
    119. public WindowsTitleActionList(IComponent component) : base( component )
    120. {
    121. _ctrl = (WindowsTitle)component;
    122. this.designerActionSvc = (DesignerActionUIService)GetService( typeof( DesignerActionUIService ) );
    123. }
    124. #endregion
    125. #region variables
    126. private readonly WindowsTitle _ctrl;
    127. private readonly DesignerActionUIService designerActionSvc = null/* TODO Change to default(_) if this is not a reference type */;
    128. #endregion
    129. #region properties
    130. public string Title
    131. {
    132. get
    133. {
    134. return _ctrl.Title;
    135. }
    136. set
    137. {
    138. _ctrl.Title = value;
    139. }
    140. }
    141. public string Description
    142. {
    143. get
    144. {
    145. return _ctrl.Description;
    146. }
    147. set
    148. {
    149. _ctrl.Description = value;
    150. }
    151. }
    152. #endregion
    153. #region callbacks
    154. public override System.ComponentModel.Design.DesignerActionItemCollection GetSortedActionItems()
    155. {
    156. DesignerActionItemCollection items = new DesignerActionItemCollection();
    157. //items.Add( new DesignerActionHeaderItem( "--- ActionList ---" ) );
    158. items.Add( new DesignerActionHeaderItem( "Eigenschaften" ) );
    159. items.Add( new DesignerActionPropertyItem( "Title", "Titel:", "Eigenschaften", "Der anzuzeigende Titel." ) );
    160. items.Add( new DesignerActionPropertyItem( "Description", "Beschreibung:", "Eigenschaften", "Die anzuzeigende Beschreibung." ) );
    161. return ( items );
    162. }
    163. #endregion
    164. }
    165. }


    Aber wie @VaporiZed bereits festgestellt hat, gehen die Werte nur verloren, wenn man sie über den SmartTag ändert.

    Vielleicht hat @Gather noch eine Idee?
    Von Ihm habe ich das Tutorial mit der ActionListe: Control-Techniken: ActionListe und ControlDesigner hinzufügen und benutzen

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „TRiViUM“ ()

    Ich bin gerade auch verwundert. Ich habe herausgefunden, dass wenn man das Control entfernt, dann Debugt, dann es neu platziert,
    beim ersten Mal die ActionListe funktioniert, und die Eigenschaft wirksam verändert wird.

    Versucht man dies allerdings ein zweites Mal, kommt es zu besagtem Fehler.

    Edit:// Außerdem ändert die ActionListe ebenfalls die Eigenschaft visuell. Beim Debuggen jedoch nicht,im Designer Eigenschaftsfenster wurde sie jedoch übernommen.
    Mfg: Gather
    Private Nachrichten bezüglich VB-Fragen werden Ignoriert!


    Also ich hab in meinen ActionLists immer noch nach jeder Änderung einer Property dem Designer explizit mitgeteilt, dass sich das Control geändert hat: github.com/stefan-baumann/Aero…gnerActionListBase.cs#L93
    Vielleicht ist das der fehlende Aspekt?
    @nafets
    Guter Hinweis, daran hab ich gar nicht mehr gedacht...
    ...allerdings leider auch ohne Verbesserung :/

    Hier noch mal das aktuelle Control:
    Spoiler anzeigen

    C#-Quellcode

    1. using System.Drawing;
    2. using System.Windows.Forms;
    3. using System.ComponentModel;
    4. using System.ComponentModel.Design;
    5. using System.Windows.Forms.Design;
    6. namespace ControlSandbox.Ui.Controls
    7. {
    8. [Designer( typeof( WindowsTitleDesigner ) )]
    9. public class WindowsTitle : Control
    10. {
    11. #region constructor
    12. public WindowsTitle()
    13. {
    14. SetStyle( ControlStyles.OptimizedDoubleBuffer |
    15. ControlStyles.ResizeRedraw |
    16. ControlStyles.FixedHeight |
    17. ControlStyles.SupportsTransparentBackColor |
    18. ControlStyles.UserPaint
    19. , true );
    20. Dock = DockStyle.Top;
    21. _title = "Title";
    22. _description = "Description";
    23. Height = 65;
    24. }
    25. #endregion
    26. #region constants
    27. private readonly Color TITLE_COLOR = Color.FromArgb( 0, 51, 153 );
    28. private readonly Font TITLE_FONT = new Font( "Segoe UI", 12f );
    29. private readonly Color DESCRIPTION_COLOR = Color.FromArgb( 0, 0, 0 );
    30. private readonly Font DESCRIPTION_FONT = new Font( "Segoe UI", 8.25f );
    31. private const int TITLE_DESCRIPTION_SPACE = 7;
    32. private const int BORDER_SPACE = 12;
    33. private readonly Color LINE_COLOR = Color.Gainsboro;
    34. #endregion
    35. #region properties
    36. private string _title;
    37. public string Title
    38. {
    39. get { return _title; }
    40. set
    41. {
    42. _title = value;
    43. Invalidate();
    44. }
    45. }
    46. private string _description;
    47. [Editor( typeof( MultilineStringEditor ), typeof( System.Drawing.Design.UITypeEditor ) )]
    48. public string Description
    49. {
    50. get { return _description; }
    51. set
    52. {
    53. _description = value;
    54. Invalidate();
    55. }
    56. }
    57. #endregion
    58. #region callbacks
    59. protected override void OnPaint(PaintEventArgs e)
    60. {
    61. base.OnPaint( e );
    62. e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
    63. e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
    64. e.Graphics.PageUnit = GraphicsUnit.Display;
    65. e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighSpeed;
    66. StringFormat format = new StringFormat() { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Near };
    67. SizeF titleSize = e.Graphics.MeasureString( _title, TITLE_FONT );
    68. Rectangle titleRect = new Rectangle( BORDER_SPACE, BORDER_SPACE, Width - BORDER_SPACE, (int)titleSize.Height );
    69. //e.Graphics.DrawString( _title, TITLE_FONT, new SolidBrush( TITLE_COLOR ), titleRect, format );
    70. TextRenderer.DrawText( e.Graphics, _title, TITLE_FONT, titleRect, TITLE_COLOR, TextFormatFlags.Default );
    71. SizeF descriptionSize = e.Graphics.MeasureString( _description, DESCRIPTION_FONT );
    72. Rectangle descriptionRect = new Rectangle( BORDER_SPACE +2 , titleRect.Y + titleRect.Height + TITLE_DESCRIPTION_SPACE, Width - BORDER_SPACE - BORDER_SPACE - 2, Height - 1 - (int)descriptionSize.Height );
    73. //e.Graphics.DrawString( _description, DESCRIPTION_FONT, new SolidBrush( DESCRIPTION_COLOR ), descriptionRect, format );
    74. TextRenderer.DrawText( e.Graphics, _description, DESCRIPTION_FONT, descriptionRect, DESCRIPTION_COLOR, TextFormatFlags.Default );
    75. e.Graphics.DrawLine( new Pen( LINE_COLOR ), BORDER_SPACE, Height -1, Width - BORDER_SPACE - 1, Height-1 );
    76. }
    77. #endregion
    78. }
    79. public class WindowsTitleDesigner : System.Windows.Forms.Design.ControlDesigner
    80. {
    81. #region variables
    82. private DesignerActionListCollection lists;
    83. #endregion
    84. #region properties
    85. private WindowsTitle HostControl
    86. {
    87. get { return (WindowsTitle)Control; }
    88. }
    89. #endregion
    90. #region callbacks
    91. public override DesignerActionListCollection ActionLists
    92. {
    93. get
    94. {
    95. if (lists == null)
    96. {
    97. lists = new DesignerActionListCollection
    98. {
    99. new WindowsTitleActionList( this.Component )
    100. };
    101. }
    102. return lists;
    103. }
    104. }
    105. public override SelectionRules SelectionRules
    106. {
    107. get
    108. {
    109. //SelectionRules selRules = SelectionRules.Moveable;
    110. //rules = rules | SelectionRules.none
    111. return base.SelectionRules;
    112. }
    113. }
    114. #endregion
    115. }
    116. public class WindowsTitleActionList : DesignerActionList
    117. {
    118. #region constructor
    119. public WindowsTitleActionList(IComponent component) : base( component )
    120. {
    121. _ctrl = (WindowsTitle)component;
    122. this.designerActionSvc = (DesignerActionUIService)GetService( typeof( DesignerActionUIService ) );
    123. }
    124. #endregion
    125. #region variables
    126. private readonly WindowsTitle _ctrl;
    127. private readonly DesignerActionUIService designerActionSvc = null/* TODO Change to default(_) if this is not a reference type */;
    128. #endregion
    129. #region properties
    130. public string Title
    131. {
    132. get
    133. {
    134. return _ctrl.Title;
    135. }
    136. set
    137. {
    138. _ctrl.Title = value;
    139. designerActionSvc.Refresh(_ctrl);
    140. }
    141. }
    142. public string Description
    143. {
    144. get
    145. {
    146. return _ctrl.Description;
    147. }
    148. set
    149. {
    150. _ctrl.Description = value;
    151. designerActionSvc.Refresh(_ctrl);
    152. }
    153. }
    154. #endregion
    155. #region callbacks
    156. public override System.ComponentModel.Design.DesignerActionItemCollection GetSortedActionItems()
    157. {
    158. DesignerActionItemCollection items = new DesignerActionItemCollection();
    159. //items.Add( new DesignerActionHeaderItem( "--- ActionList ---" ) );
    160. items.Add( new DesignerActionHeaderItem( "Eigenschaften" ) );
    161. items.Add( new DesignerActionPropertyItem( "Title", "Titel:", "Eigenschaften", "Der anzuzeigende Titel." ) );
    162. items.Add( new DesignerActionPropertyItem( "Description", "Beschreibung:", "Eigenschaften", "Die anzuzeigende Beschreibung." ) );
    163. return ( items );
    164. }
    165. #endregion
    166. }
    167. }


    BTW:
    Ich habe gerade noch festgestellt, dass, wenn ich die Eingabe in der ActionListe mit der ENTER-Taste bestätige, ich im Control-Click-Event lande.
    Kommt mir auch nicht ganz normal vor :huh:

    EDIT:

    @Gather
    @VaporiZed

    ENDLICH HABE ICH DIE URSACHE DES PROBLEMS HERAUSGEFUNDEN!

    Die Lösung besteht darin, nicht direkt die Control-Eigenschaft in der Klasse DesignerActionList zu beschreiben, sondern mittels TypeDescriptor:

    C#-Quellcode

    1. public CorporateDesign.Styles ButtonStyle
    2. {
    3. get { return _control.ButtonStyle; }
    4. //set { _control.Eigenschaft= value; } // funzt nicht
    5. set { TypeDescriptor.GetProperties(Component)["Eigenschaft"].SetValue(Component, value); }
    6. }


    Damit ist auch das komische Verhalten weg, dass wenn man ein String über den SmartTag eingibt und dann mit ENTER bestätigt, dass man im Control-Click-Event landet.

    Darauf gekommen bin ich, als ich mir mit ILSpy mal ein Windows-Standard-Control mit SmartTag (Picturebox in meinem Fall) angeschaut hatte...

    Hier die aktualisierte Klasse:
    Spoiler anzeigen

    C#-Quellcode

    1. using System.Drawing;
    2. using System.Windows.Forms;
    3. using System.ComponentModel;
    4. using System.ComponentModel.Design;
    5. using System.Windows.Forms.Design;
    6. namespace WesTestControlsExample.Ui.Controls
    7. {
    8. [Designer(typeof(WindowsTitleDesigner))]
    9. public class WindowsTitle : Control
    10. {
    11. #region constructor
    12. public WindowsTitle()
    13. {
    14. SetStyle(ControlStyles.OptimizedDoubleBuffer |
    15. ControlStyles.ResizeRedraw |
    16. ControlStyles.FixedHeight |
    17. ControlStyles.SupportsTransparentBackColor |
    18. ControlStyles.UserPaint
    19. , true);
    20. Dock = DockStyle.Top;
    21. _title = "Title";
    22. _description = "Description";
    23. Height = 65;
    24. }
    25. #endregion
    26. #region constants
    27. private readonly Color TITLE_COLOR = Color.FromArgb(0, 51, 153);
    28. private readonly Font TITLE_FONT = new Font("Segoe UI", 12f);
    29. private readonly Color DESCRIPTION_COLOR = Color.FromArgb(0, 0, 0);
    30. private readonly Font DESCRIPTION_FONT = new Font("Segoe UI", 8.25f);
    31. private const int TITLE_DESCRIPTION_SPACE = 7;
    32. private const int BORDER_SPACE = 12;
    33. private readonly Color LINE_COLOR = Color.Gainsboro;
    34. #endregion
    35. #region properties
    36. private string _title;
    37. public string Title
    38. {
    39. get { return _title; }
    40. set
    41. {
    42. _title = value;
    43. Invalidate();
    44. }
    45. }
    46. private string _description;
    47. [Editor(typeof(MultilineStringEditor), typeof(System.Drawing.Design.UITypeEditor))]
    48. public string Description
    49. {
    50. get { return _description; }
    51. set
    52. {
    53. _description = value;
    54. Invalidate();
    55. }
    56. }
    57. #endregion
    58. #region callbacks
    59. protected override void OnPaint(PaintEventArgs e)
    60. {
    61. base.OnPaint(e);
    62. e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
    63. e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
    64. e.Graphics.PageUnit = GraphicsUnit.Display;
    65. e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighSpeed;
    66. StringFormat format = new StringFormat() { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Near };
    67. SizeF titleSize = e.Graphics.MeasureString(_title, TITLE_FONT);
    68. Rectangle titleRect = new Rectangle(BORDER_SPACE, BORDER_SPACE, Width - BORDER_SPACE, (int)titleSize.Height);
    69. //e.Graphics.DrawString( _title, TITLE_FONT, new SolidBrush( TITLE_COLOR ), titleRect, format );
    70. TextRenderer.DrawText(e.Graphics, _title, TITLE_FONT, titleRect, TITLE_COLOR, TextFormatFlags.Default);
    71. SizeF descriptionSize = e.Graphics.MeasureString(_description, DESCRIPTION_FONT);
    72. Rectangle descriptionRect = new Rectangle(BORDER_SPACE + 2, titleRect.Y + titleRect.Height + TITLE_DESCRIPTION_SPACE, Width - BORDER_SPACE - BORDER_SPACE - 2, Height - 1 - (int)descriptionSize.Height);
    73. //e.Graphics.DrawString( _description, DESCRIPTION_FONT, new SolidBrush( DESCRIPTION_COLOR ), descriptionRect, format );
    74. TextRenderer.DrawText(e.Graphics, _description, DESCRIPTION_FONT, descriptionRect, DESCRIPTION_COLOR, TextFormatFlags.Default);
    75. e.Graphics.DrawLine(new Pen(LINE_COLOR), BORDER_SPACE, Height - 1, Width - BORDER_SPACE - 1, Height - 1);
    76. }
    77. #endregion
    78. }
    79. public class WindowsTitleDesigner : System.Windows.Forms.Design.ControlDesigner
    80. {
    81. #region variables
    82. private DesignerActionListCollection lists;
    83. #endregion
    84. #region properties
    85. private WindowsTitle HostControl
    86. {
    87. get { return (WindowsTitle)Control; }
    88. }
    89. #endregion
    90. #region callbacks
    91. public override DesignerActionListCollection ActionLists
    92. {
    93. get
    94. {
    95. if (lists == null)
    96. {
    97. lists = new DesignerActionListCollection
    98. {
    99. new WindowsTitleActionList( this.Component )
    100. };
    101. }
    102. return lists;
    103. }
    104. }
    105. public override SelectionRules SelectionRules
    106. {
    107. get
    108. {
    109. //SelectionRules selRules = SelectionRules.Moveable;
    110. //rules = rules | SelectionRules.none
    111. return base.SelectionRules;
    112. }
    113. }
    114. #endregion
    115. }
    116. public class WindowsTitleActionList : DesignerActionList
    117. {
    118. #region constructor
    119. public WindowsTitleActionList(IComponent component) : base(component)
    120. {
    121. _ctrl = (WindowsTitle)component;
    122. this.designerActionSvc = (DesignerActionUIService)GetService(typeof(DesignerActionUIService));
    123. }
    124. #endregion
    125. #region variables
    126. private readonly WindowsTitle _ctrl;
    127. private readonly DesignerActionUIService designerActionSvc = null/* TODO Change to default(_) if this is not a reference type */;
    128. #endregion
    129. #region properties
    130. public string Title
    131. {
    132. get
    133. {
    134. return _ctrl.Title;
    135. }
    136. set
    137. {
    138. //_ctrl.Title = value; // funzt nicht
    139. TypeDescriptor.GetProperties(Component)["Title"].SetValue(Component, value);
    140. designerActionSvc.Refresh(_ctrl);
    141. }
    142. }
    143. public string Description
    144. {
    145. get
    146. {
    147. return _ctrl.Description;
    148. }
    149. set
    150. {
    151. //_ctrl.Description = value; // funzt nicht
    152. TypeDescriptor.GetProperties(Component)["Description"].SetValue(Component, value);
    153. designerActionSvc.Refresh(_ctrl);
    154. }
    155. }
    156. #endregion
    157. #region callbacks
    158. public override System.ComponentModel.Design.DesignerActionItemCollection GetSortedActionItems()
    159. {
    160. DesignerActionItemCollection items = new DesignerActionItemCollection();
    161. //items.Add( new DesignerActionHeaderItem( "--- ActionList ---" ) );
    162. items.Add(new DesignerActionHeaderItem("Eigenschaften"));
    163. items.Add(new DesignerActionPropertyItem("Title", "Titel:", "Eigenschaften", "Der anzuzeigende Titel."));
    164. items.Add(new DesignerActionPropertyItem("Description", "Beschreibung:", "Eigenschaften", "Die anzuzeigende Beschreibung."));
    165. return (items);
    166. }
    167. #endregion
    168. }
    169. }

    Dieser Beitrag wurde bereits 10 mal editiert, zuletzt von „TRiViUM“ ()