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.
Related
In Xamarin Forms if you activate the navigation bar, that show you the title of the page and an arrow to use as a back button, when you have the screen reader activated and the back button is on focus, the talkback (for Android) says "unlabeled button".
I know how to set the Accessibility Name of general elements, but I don't know how to get the back button element, since it is not an istance.
If you're using Shell Navigation, create a CustomShellRenderer
[assembly: ExportRenderer(typeof(AppShell), typeof(CustomShellRenderer))]
namespace Droid.Renderers.CustomAppShell
{
public class CustomShellRenderer : ShellRenderer
{
protected override IShellToolbarAppearanceTracker CustomToolbarAppearanceTracker()
{
return new CustomShellToolbarAppearanceTracker(this);
}
}
}
namespace Droid.CustomShell
{
public class CustomShellToolbarAppearanceTracker : ShellToolbarAppearanceTracker
{
public override void SetAppearance(AndroidX.AppCompat.Widget.Toolbar toolbar, IShellToolbarTracker toolbarTracker, ShellAppearance appearance)
{
base.SetAppearance(toolbar, toolbarTracker, appearance);
toolbar.NavigationContentDescription = "Custom Label Description”; // Set label here
}
}
}
I have question. I created a custom user control. My CustomUserControl is inherited from UserControl. I add some custom methods and properties in my CustomUserControl. I wanted to add my CustomUserControl to "Add New Item" in Visual Studio for projects.
For this I used "Item Template" and created a template. After restarting Visual Studio every thing was fine and I could add my CustomUserControl by using "Add New Item" in my project.
Just i have a problem when I add CustomUserControl to my project, the methods and properties that I am added into template file appeare and i can change them. How can I Hide methods and property in template? I don't want to see methods and properties after add CustomUserControl to project.
Note : When i add my CustomUserControl project, "CustomUserControl1" is created and it inherit from the UserControl not my CustomUserControl.
My Template is :
public partial class CustomUserControl : UserControl
{
private string _Version;
[Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
public string Version
{
get { return _Version; }
private set { _Version = value; }
}
private void InitRequirements()
{
try
{
// ... My Code
}
catch (Exception exp)
{
throw exp;
}
}
}
After adding to project :
public partial class CustomUserControl1 : UserControl
{
private string _Version;
[Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
public string Version
{
get { return _Version; }
private set { _Version = value; }
}
private void InitRequirements()
{
try
{
// ... My Code
}
catch (Exception exp)
{
throw exp;
}
}
}
it should be like this :
public partial class CustomUserControl1 : CustomUserControl
{
// Without showing methods and properties
}
Thank you
Best regards,
You can try to hide the properties you don't want to display using the new keyword.
For example, if you want to hide the property Text, you can add the following:
[Bindable(false)]
[Browsable(false)]
public new string Text { get; set; }
Edit
If you want to reuse your control in other projects, instead of an "Item Template" you may want to create a library or assembly and reference it in your projects. Like this, you will be able to use it and inherit from it without seeing the code. When you use the Item Template it will just create a new UserControl based on the code saved on the template, but you are not reusing the UserControl itself. You could also create a NuGet package if you want to handle versioning, etc...
I have here some custom user control with DataGridView in it, now when i Implement my user control to some form I want to be able to access DataGridView properties within designer, is this possible?
This is my user control
public partial class MyUserControlTest01 : UserControl
{
// my way to accsses DataGridView
//
public DataGridView Dtv_userControl
{
get { return myUserControl_datagridView; }
set { myUserControl_datagridView = value; }
}
public MyUserControlTest01()
{
InitializeComponent();
}
So when i implement this user control to some Form, I can access DataGridView properties from code, but i want to do it from Designer.
Hope my question is clear,
any suggestion is helpful.
Thank you for your time.
Just add these annotations over your DataGridView-Property
[Browsable(true)]
[Category("*Any category you want*")]
public DataGridView Dtv_userControl
{
get { return myUserControl_datagridView; }
set { myUserControl_datagridView = value; }
}
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?
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; }
}
}