Setting default properties for toolbox items? - c#

I don't want the button I add to have dotted borders when clicked, so I found out that I can disable that by turning off the focus cues. I don't want to have to change settings like this for each individual button I add. Is there any way to set property defaults in Visual Studio?

What you need to do is create a new Control based on Button and use it throughout your application.
public class MyButton : System.Windows.Forms.Button
{
protected override bool ShowFocusCues
{
get { return false; }
}
}

Related

How to persist custom user control property in application settings?

I created simple user control consisting of 3 elements:
2 radio buttons and table layout panel aka Yes or No control.
I created custom property boolean "Value" which changes depending of checked radio button.
UPDATE 1: I added that control to form and bind property "Value" to settings and in control code I added logic to determine which radio but should be checked but after saving settings and reloading form neither of radio buttons are checked.
How can I achieve that effect with the least effort.
Below the code:
public partial class YesOrNoControl : UserControl
{
public YesOrNoControl()
{
InitializeComponent();
LoadValue();
}
[Description("Sets the value of Control"), Category("Behavior"), DefaultValue(false), Browsable(true)]
public bool Value { get; set; }
void LoadValue()
{
if (Value)
{
YesButton.Checked = true;
}
else
{
NoButton.Checked = true;
}
}
private void YesButton_Click(object sender, EventArgs e)
{
Value = true;
}
private void NoButton_Click(object sender, EventArgs e)
{
Value = false;
}
}
You can define application settings within the IDE (under project settings).
You can then manipulate the settings by use of the Properties.Settings namespace.
Settings are automatically loaded at runtime, you can save the settings by calling the Save() method.
More links: Using Application Settings and User Settings
Applications Settings for WinForms
I managed to solve my problem.
I modified property "Value" to get value from application setting (specially created for this purpose) and set value to same application setting.
At the end of setter I added saving of application settings.
It solves the main problem but it's kind of workaround, not the true answer to problem.
Below the modified code of property:
public bool Value
{
get
{
return Properties.Settings.Default.YesOrNoControlValue;
}
set
{
Properties.Settings.Default["YesOrNoControlValue"] = value;
Properties.Settings.Default.Save();
}
}

Hide WinForm UserControl custom property from VS designer

Visual Studio is incorrectly calling my UserControl's custom properties at design time.
I have read many of the posting about using the [Browsable( false )] and [DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden )] attributes, but this has not worked for me.
To reproduce this problem, using Visual Studio, create a new Windows Forms Application, then add a User Control to your project, and drag that User Control onto your Form. Add a public custom property to your User Control, as shown below.
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
[Browsable( false )]
[DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden )]
public bool AreYouThere
{
get
{
MessageBox.Show( "Yes I Am Here!" );
return true;
}
}
}
When the Form is open in the Visual Studio designer, if I force the solution to clean and then rebuild, I will see a MessageBox with the text "Yes I Am Here!", indicating that Visual Studio has called the AreYouThere property on my User Control.
This should not happen, since I have decorated the AreYouThere property with the [Browsable( false )] and [DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden )] attributes.
Any idea why this is happening?
(This problem occurs on Visual Studio 2010 and 2013).
In order to hide a property from every place possible you have to mark it with those attributes
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[EditorBrowsable(EditorBrowsableState.Never)]
[Bindable(false)]
[Browsable(false)]
public class CustomDesigner : ControlDesigner
{
private static string[] RemovedProperties = new[]
{
"AccessibilityObject","AccessibleDefaultActionDescription","AccessibleDescription",
"AccessibleName","AccessibleRole","AllowDrop","Anchor","AutoEllipsis","AutoScrollOffset",
"AutoSize","AutoSizeMode","FlatAppearance", "FlatStyle",
"TextAlign","TextImageRelation","UseCompatibleTextRendering",
"UseMnemonic","UseWaitCursor"
};
public CustomDesigner() { }
protected override void PreFilterProperties(IDictionary properties)
{
foreach (string prop in RemovedProperties)
{
properties.Remove(prop);
}
base.PreFilterProperties(properties);
}
}
[ToolboxItem(true)]
[DesignerCategory("code")]
[Designer(typeof(CustomDesigner))]
public partial class NewButton : Button
{
public Color OnHoverBackColor
{
get { return _onHoverBackColor; }
set
{
_onHoverBackColor = value;
Invalidate();
}
}
}
Do not set the default value for the property as you want it. In your example, set the property AreYouThere to false/true and in the parent or whereever you are using it you instanceOfUserControl1.AreYouThere = true/false in say Load event.

Show form in designer with code executed during load

I have a windows form that I am making in C# using Visual Studio 2013. The form contains a ListView that has 2 columns. The ListView itself is created inside of the InitializeComponent() which is auto-generated by VS. I actually add the column headers outside of InitializeComponent(). The problem is that I want the [Design] view in VS to show the column headers that I declare but I don't want to clutter up the InitializeComponent() method in addition to it also saying that I should not modify the contents of the code within the method.
Basically what I did was create a method that adds the two column headers to the ListView and formats them:
private void initRecipListView()
{
this.recipList.Columns.Add("Recipient", -2, System.Windows.Forms.HorizontalAlignment.Left);
this.recipList.Columns.Add("Number of Reports", -2, System.Windows.Forms.HorizontalAlignment.Left);
}
I want to call this method so that those two columns are added and visible during runtime AS WELL AS when I view the form inside of the VS designer window. I have tried putting that method inside of the constructor for the form itself which works during runtime but doesn't in the designer window.
Any idea where I need to put this method for it to be called and used when I am viewing the form in the designer window?
You can derive your own class from ListView and implement a scheme like this. Basic ingredients you need is presetting the number of columns in the constructor and exposing properties to allow you to set the column header text. Override OnClientSizeChanged() to keep the columns centered and deal with the vertical scroll bar appearing.
Add a new class and paste the code shown below. Compile. Drop the new control from the top of the toolbar. You can set the Column1Name and Column2Name properties in the designer or in your code. You'll get the WYSIWYG view in the designer.
using System;
using System.ComponentModel;
using System.Windows.Forms;
class MyListView : ListView {
public MyListView() {
this.Columns.Add("Unnamed1");
this.Columns.Add("Unnamed2");
this.View = View.Details;
}
public string Column1Name {
get { return this.Columns[0].Text; }
set { this.Columns[0].Text = value; }
}
public string Column2Name {
get { return this.Columns[1].Text; }
set { this.Columns[1].Text = value; }
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Browsable(false)]
public new ListView.ColumnHeaderCollection Columns {
get { return base.Columns; }
}
protected override void OnClientSizeChanged(EventArgs e) {
base.Columns[0].Width = this.ClientSize.Width / 2;
base.Columns[1].Width = this.ClientSize.Width - base.Columns[0].Width;
base.OnClientSizeChanged(e);
}
}

Initialize control in control

This is my custom control class:
// Extended ComboBox where ill change some property.
public class ExtComboBox : ComboBox
{
...
}
// ExtButton is a control that i am going to drop on Form from ToolBox.
public partial class ExtButton : Button
{
public ExtComboBox ComboBoxInsideButton { get; set; }
public ExtButton()
{
InitializeComponent();
ComboBoxInsideButton = new ExtComboBox();
ComboBoxInsideButton.Text = "hi!";
Controls.Add(ComboBoxInsideButton);
}
}
Basically when i add this control to form there will be ComboBox on top off Button.
Don't ask my why i need this :D
Now if i need to change ComboBox text i simply use:
extButton1.ComboBoxInsideButton.Text = "aaa";
All work fine.. but :) when i am trying to change some ComboBox properties in Design mode (Window Properties -> Expand ComboBoxInsideButton -> Change Text to "bbb")
after rebuilding or running project ComboBox properties will be reseted (ExtButton.Designer.cs)
Question 1: How to initialize subcontrol with some default properties value, so when ill drop control on Form all setting will be added?
and
Question 2: How to change properties of subcontrol on design time.
EDIT:
Answer here: Designer does not generate code for a property of a subcontrol. Why?
Adding [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] solves the problem.
I wrote a mini-tut on how to create custom UserControls and accessing their members here. Pretty much, it looks like you are going to want add properties to your ExtComboBox that expose the ComboBox properties you'd like to change. Then, in ExtButton, you will be able to use the . to change these values at runtime.
Also, instead of doing:
public ExtComboBox ComboBoxInsideButton { get; set; }
...
ComboBoxInsideButton = new ExtComboBox();
do
public ExtComboBox comboBoxInsideButton = null;
...
comboBoxInsideButton = new ExtComboBox();
Be sure to understand the difference between private and public also. I'm not sure if you want your ExtComboBox to be public if you're placing it on another control.
Hope this helps.

Set default usercontrol property value in asp.net

I have worked out how to set the default property value for a usercontrol in a windows form as below
private bool _MyTestProperty = true;
[DefaultValue(true)]
public bool MyTestProperty
{
get
{
return this._MyTestProperty;
}
set
{
this._MyTestProperty = value;
}
}
This code successfully causes a default value to appear in the properties pane when the usercontrol is being used inside a windows form.
However applying the same technique to usercontrols (ascx files) in ASP.NET does not work. Could anyone guide me in the right direction?

Categories

Resources