Since most of my container controls use a transparent background color, I created a simple base class for them:
public partial class BaseControl : UserControl {
public BaseControl() {
this.BackColor = Color.Transparent;
}
[DefaultValue(typeof(Color), "Transparent")]
public override Color BackColor {
get { return base.BackColor; }
set { base.BackColor = value; }
}
}
My problem is when either inheriting or containing one of them, the designer keeps adding a line in InitializeComponent():
control.BackColor = System.Drawing.SystemColors.Control;
When I right-click and choose Reset in the properties window, the property is reset to Transparent, but when I reopen the window the designer immediately sets it back to Control.
Is there another attribute I need to mention for the default to stick?
I want to access fdtlc.Controls instead of fdtlc.flpFlightList.Controls
public partial class FlightDetailListControl : UserControl
{
public ControlCollection Controls //Error circular control reference has been made
{
get
{
return flpFlightList.Controls; // flpFlightList is a FlowLayoutPanel
}
}
public FlightDetailListControl()
{
InitializeComponent();
}
}
FlightDetailListControl and FlowLayoutPanel are both Controls, so they both already have a ControllCollection named Controls inherited from UserControl. You'll have to pick another name for your property in FlightDetailListControl.
I have created a own control derived from UserControl and I wanted to make the Dock property a read only, and by trials & errors I came with something like this:
public partial class Header : UserControl
{
public Header()
{
InitializeComponent();
base.Dock = DockStyle.Top;
}
/// <summary>
/// Gets the DockStyle of the control
/// </summary>
[Browsable(false)]
[ReadOnly(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new DockStyle Dock
{
get
{
return base.Dock;
}
private set
{
base.Dock = DockStyle.Top;
}
}
}
is this a correct way to do this ? Simply: I want the control to always be docked to top (since it's a header :))
The private set you have is not well implemented, as it sets base.Dock to a hard-coded value instead of the given value. Either remove it completely or make it
private set
{
base.Dock = value;
}
Note, however, that users of your Header class could still cast it to UserControl and thus set the Dock property.
There is no 100% way to prevent this.
remove the set block and everything should work as desired:
public new DockStyle Dock
{
get
{
return base.Dock;
}
}
I need to understand how to utilize a ToolTip with a custom UserControl. Just creating the ToolTip on a form and assigning the specific control a ToolTip (via SetToolTip) obviously will not work.
What properties do I need to give the custom UserControl in order to assign ToolTip text to it? Do I need to add a ToolTip on the usercontrol form? How can I go about doing this?
Please provide a code sample or something for me to visualize.
Thank you!
Put a ToolTip on your UserControl (use the designer, just like you would put one on a form), and add a public property to your UserControl like:
public string TextBoxHint
{
get
{
return toolTip1.GetToolTip(textBox1);
}
set
{
toolTip1.SetToolTip(textBox1, value);
}
}
Create SetToolTip method in user control and set tooltip for each user control's subcontrol:
public partial class SomeUserControl : UserControl
{
public void SetToolTip(ToolTip toolTip)
{
string text = toolTip.GetToolTip(this);
toolTip.SetToolTip(subControl1, text);
toolTip.SetToolTip(subControl2, text);
// ...
}
}
Set tooltip text for user control instance in parent control designer. This adds in .designer file:
this.toolTip1.SetToolTip(this.someUserControl1, "Some text.");
Call SetToolTip method of user control instance with ToolTip parent control instance from parent control's constructor:
public partial class ParentForm : Form
{
public ParentForm()
{
InitializeComponent();
someUserControl1.SetToolTip(toolTip1);
}
}
This is the correct way to implement a serialized ToolTip property:
public partial class YourControlClass : UserControl
{
// Serialized property.
private ToolTip toolTip = new System.Windows.Forms.ToolTip();
// Public and designer access to the property.
public string ToolTip
{
get
{
return toolTip.GetToolTip(this);
}
set
{
toolTip.SetToolTip(this, value);
}
}
Is there a simple way to implement databinding when neither of both classes is of type Control?
In my case, I would like to bind a variable to a property of a custom ToolStripButton.
EDIT for clarification: when binding to a Control, I can use Control's DataBindings collection. However, I am searching for a way to bind properties regardless of the source and target Type.
EDIT: using winforms
You can probably do this by using Truss.
Truss provides WPF-style databinding for any class that implements INotifyPropertyChanged. It gives you a bit more flexibility in this, since it doesn't restrict the classes to being derived from a specific base class.
I use this Implemetation of IBindableComponent on the ToolStripButton, found here. The BindableToolStripButton allows you to use Databinding like with a normal Control.
[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ToolStrip | ToolStripItemDesignerAvailability.StatusStrip)]
public class BindableToolStripButton : ToolStripButton, IBindableComponent
{
public BindableToolStripButton()
: base() { }
public BindableToolStripButton(String text)
: base(text) { }
public BindableToolStripButton(System.Drawing.Image image)
: base(image) { }
public BindableToolStripButton(String text, System.Drawing.Image image)
: base(text, image) { }
public BindableToolStripButton(String text, System.Drawing.Image image, EventHandler onClick)
: base(text, image, onClick) { }
public BindableToolStripButton(String text, System.Drawing.Image image, EventHandler onClick, String name)
: base(text, image, onClick, name) { }
#region IBindableComponent Members
private BindingContext bindingContext;
private ControlBindingsCollection dataBindings;
[Browsable(false)]
public BindingContext BindingContext
{
get
{
if (bindingContext == null)
{
bindingContext = new BindingContext();
}
return bindingContext;
}
set
{
bindingContext = value;
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ControlBindingsCollection DataBindings
{
get
{
if (dataBindings == null)
{
dataBindings = new ControlBindingsCollection(this);
}
return dataBindings;
}
}
#endregion
}
Assuming you have a class MyClass implementing INotifyPropertyChanged, use it just like you would when binding to a control property:
bindableToolStripButton1.DataBindings.Add("Enabled", myClass1, "MyBooleanProperty");
Use dependency properties (your property in your ToolStripButton should be) and create a property for your variable in your other class and create a binding and set it to the property of your ToolstripButton.
I guess that's about the easiest way to do it.
EDIT: That's only for WPF...
Else implement INotifyPropertyChanged and when your variable changes, it should automatically change in your ToolStripButton.
For similar behaviour like Controls being bound to object properties, for any Type you can implement the same interfaces.
Based on that thought, you can subclass ToolStripButton (or desired Type to have bindings) and implement IBindableComponent for it. This works for all kinds of source and target Types as long as they're not sealed. For example, your tool strip button:
public class BindableToolStripButton : ToolStripButton, IBindableComponent {
//...
This will cause the BindableToolStripButton to have its own .DataBindings property whereas the base ToolStripButton class doesn't have such a propery.
You would need to follow through on filling out implementation details using examples seen here from Microsoft for ISite, IBindableComponent, IComponent and any inherited interfaces.
Then you would add Binding instances to any instance of BindableToolStripButton.
(Note: I only have fragements so will make my first community wiki post - and we'll see how that goes... )
I written some basic databinding stuff through reflection. It works on any object and doesn't need to implement something special (no INotifyPropertyChanged, it just works) it is part of my editor at http://github.com/filipkunc/opengl-editor-cocoa look at HotChocolate/Bindings (like re-implementation of Cocoa KVC, KVO into .NET) folder. You can see it in action in HotChocolateTest project.
There is another quick and simple solution which consists in creating properties in the Form, and bind them:
public MyForm : Form
{
...
public bool CanDelete
{
get { return deleteToolStripButton.Enabled; }
set { deleteToolStripButton.Enabled = value; }
}
public MyForm()
{
...
this.DataBindings.Add("CanDelete", this.MyModel, "DeleteAllowed",
false, DataSourceUpdateMode.Never);
...
}
}
Assuming that MyModel contains a DeleteAllowed property which notifies its changes.