Windows Develop Bookmark and Share   
 index > Windows Forms Designer > User control doubt
 

User control doubt

I'm new to C#(VB6 background). so forgive me if i'm asking something
wrong.

My user control has a textbox and a label control. Now i want to provide
a backcolor property for the usercontrol which would allow me to change the backcolor of the
textbox. Since user control also contains a backcolor i tried to override
the property like this:

public override Color BackColor
{
  get
  {
    return textBox1.BackColor;
  }
  set
  {
    textBox1.BackColor = value;
  }
}

however when i'm using the control in the client app the designer
doesnot persist the changes i made to this backcolor property. i.e., when i run the application the control reverts to the default property
MigrationUser 1  Wednesday, March 23, 2005 5:06 AM
<b>Edited by KraGiE: Added code tags to format the code.</b>

Here is how I did it (VB):

''' -----------------------------------------------------------------------------
''' <summary>
''' Gets/Sets the BackColor of the grab handle.
''' </summary>
''' <value>System.Drawing.Color.</value>
''' <remarks>
''' </remarks>
''' <history>
''' [ESCHNEIDER0]6/21/2004Created
''' </history>
''' -----------------------------------------------------------------------------
<Description("Gets/Sets the BackColor of the grab handle"), Browsable(True), XmlIgnore(), DefaultValue("System.Drawing.Color.White")> _
Public Property BackgroundColor() As System.Drawing.Color
Get
Return m_BackColor
End Get
Set(ByVal value As System.Drawing.Color)
m_BackColor = value
End Set
End Property

Private Function ShouldSerializeBackGroundColor() As Boolean
Return True
End Function

Private Sub ResetBackGroundColor()
m_BackColor = System.Drawing.Color.White
End Sub

''' -----------------------------------------------------------------------------
''' <summary>
''' Defines a color format.
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' [ESCHNEIDER0]8/6/2004Created
''' </history>
''' -----------------------------------------------------------------------------
Protected Enum ColorFormat
''' -----------------------------------------------------------------------------
''' <summary>
''' NamedColor = 0 , Specifies that the color is a named color.
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' [ESCHNEIDER0]8/6/2004Created
''' </history>
''' -----------------------------------------------------------------------------
NamedColor = 0

''' -----------------------------------------------------------------------------
''' <summary>
''' ARGBColor = 1 , Specifies that the color is a RGB format.
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' [ESCHNEIDER0]8/6/2004Created
''' </history>
''' -----------------------------------------------------------------------------
ArgbColor = 1
End Enum 'ColorFormat

''' -----------------------------------------------------------------------------
''' <summary>
''' Used to serialize/deserialize the BackGroundColor.
''' </summary>
''' <value>String.</value>
''' <remarks>
''' </remarks>
''' <history>
''' [ESCHNEIDER0]8/9/2004Created
''' </history>
''' -----------------------------------------------------------------------------
<Browsable(False), XmlElement("BackGroundColor")> _
Public Property XmlColorType() As String
Get
Return Me.SerializeColor(BackgroundColor)
End Get
Set(ByVal Value As String)
BackgroundColor = Me.DeserializeColor(Value)
End Set
End Property

''' -----------------------------------------------------------------------------
''' <summary>
''' Function used to create a Serialize string for a Color object.
''' </summary>
''' <param name="color">Color.</param>
''' <returns>String.</returns>
''' <remarks>
''' </remarks>
''' <history>
''' [ESCHNEIDER0]8/29/2004Created
''' </history>
''' -----------------------------------------------------------------------------
Protected Shared Function SerializeColor(ByVal color As Color) As String
If color.IsNamedColor Then
Return String.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}:{1}", ColorFormat.NamedColor, color.Name)
Else
Return String.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}:{1}:{2}:{3}:{4}", ColorFormat.ArgbColor, color.A, color.R, color.G, color.B)
End If

End Function 'SerializeColor

''' -----------------------------------------------------------------------------
''' <summary>
''' Function used to convert a SerializeColor string into a Color object.
''' </summary>
''' <param name="colorValue">String.</param>
''' <returns>Color.</returns>
''' <remarks>
''' </remarks>
''' <history>
''' [ESCHNEIDER0]8/29/2004Created
''' </history>
''' -----------------------------------------------------------------------------
Protected Shared Function DeserializeColor(ByVal colorValue As String) As Color
Dim a, r, g, b As Byte

Dim pieces As String() = colorValue.Split(New Char() {":"c})

Dim colorType As ColorFormat = CType([Enum].Parse(GetType(ColorFormat), pieces(0), True), ColorFormat)

Select Case colorType
Case ColorFormat.NamedColor
Return Color.FromName(pieces(1))

Case ColorFormat.ArgbColor
a = Byte.Parse(pieces(1), System.Globalization.CultureInfo.InvariantCulture)
r = Byte.Parse(pieces(2), System.Globalization.CultureInfo.InvariantCulture)
g = Byte.Parse(pieces(3), System.Globalization.CultureInfo.InvariantCulture)
b = Byte.Parse(pieces(4), System.Globalization.CultureInfo.InvariantCulture)

Return Color.FromArgb(a, r, g, b)
End Select
Return Color.Empty

End Function 'DeserializeColor
MigrationUser 1  Wednesday, March 23, 2005 8:45 PM
Thnx for your reply KraGiE

Is serializing the value the only possible way ?

MigrationUser 1  Thursday, March 24, 2005 3:32 AM
Clarification: do you want to really scrap the BackColor property of your usercontrol and have it redirect its settings to the encapsulated textbox?
Is your textbox also serialized/code-gened with context as designerserializationvisibility?
MigrationUser 1  Monday, March 28, 2005 8:56 PM
>>do you want to really scrap the BackColor property of your usercontrol and have it 
>>redirect its settings to the encapsulated textbox? 

Yes. That's exactly what i want to do. Similar to back color i want to redirect settings for fore color, font etc.


>>Is your textbox also serialized/code-gened with context as designerserializationvisibility?

yes. following is the code i'm using

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public override Color BackColor
{
     get
     {
           return textBox1.BackColor;
     }
     set
     {
           textBox1.BackColor= value;
     }
}
MigrationUser 1  Wednesday, March 30, 2005 6:42 AM
BackColor property should not have a Content DesignerSerializationVisibility attribute, it should be its default value (Visible).  

What I mean with "Is the textbox serialized?" is do you have a property such as this:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] 
public TextBox EncapsulatedTextBox
{
   get{ return this.encapsulatedTextBox; }
}
MigrationUser 1  Thursday, March 31, 2005 8:46 PM
No. the textbox is not serialised.

this is how i'm using the textbox in the usercontrol

public class UserControl1 : System.Windows.Forms.UserControl
{
private TextBox textBox1;
....
....
....
public override Color BackColor
{
get {     return textBox1.BackColor;    }

set {     textBox1.BackColor = value;  }
}
}
MigrationUser 1  Friday, April 01, 2005 12:55 AM
The BackColor property is an AmbientProperty where if it's not set, it copies the parent container's BackColor.  Since you have overridden the BackColor property and not set the base.BackColor, it won't persist the changes since internally, there was none for the UserControl's BackColor. you are changing the textbox1's BackColor property which is obviously will not be persisted since the textbox instance is not persisted.

I would rather have another Color property exposed for the TextBox control's BackColor.

public Color TextBoxBackColor
{
//normal get set and not override the usercontrol's backcolor
}

MigrationUser 1  Friday, April 01, 2005 5:19 AM
Thnx a ton for your valuable suggestion.

I do not want to create another property to expose the backcolor for the textbox. so i added base.Backcolor = value

so this is the modified code

public override Color BackColor
{
get   {    return MskControl.BackColor;   }

set
{
base.BackColor = value;
MskControl.BackColor = value;
}

}
MigrationUser 1  Friday, April 01, 2005 6:28 AM

You can use google to search for other answers

Custom Search

More Threads

• Why don't my Usercontrols show up in Toolbox of multi project sln?
• Basic Question on Properties returning a class
• Trying to Pause & Resume LogOff or ShutDown or Restart
• a model of label
• Gradient colored menus&toolbars
• Custom combobox behavior
• DataGridViewComboBox Cell Problem
• what the heck? Clicked on the background image property of a form, and everything went haywire!
• ComboBox in a DesignerActionItemCollection
• Create custom UI type editor for control property ?