C# Property Grid - c#

I am writing an application which is going to allows users to change the properties of a text box or label and these controls are user controls. Would it be easiest to create a separate class for each user control which implements the properties I want them to be able to change and then bind those back to the user control? Or is there another method I am overlooking?

Create a custom Attribute, and tag the properties you want the user to edit with this attribute. Then set the BrowsableAttribute property on the property grid to a collection containing only your custom attribute:
public class MyForm : Form
{
private PropertyGrid _grid = new PropertyGrid();
public MyForm()
{
this._grid.BrowsableAttributes = new AttributeCollection(new UserEditableAttribute());
this._grid.SelectedObject = new MyControl();
}
}
public class UserEditableAttribute : Attribute
{
}
public class MyControl : UserControl
{
private Label _label = new Label();
private TextBox _textBox = new TextBox();
[UserEditable]
public string Label
{
get
{
return this._label.Text;
}
set
{
this._label.Text = value;
}
}
[UserEditable]
public string Value
{
get
{
return this._textBox.Text;
}
set
{
this._textBox.Text = value;
}
}
}

Related

In which form event I can hide label of user control

In a windows application project I have a form which used a user control. I want to hide a label and textbox on user control. In which event of form I can do this ?
This method in user control which named DoctorPermissionApprove:
public void LoadDoctorPermission(int fromWhere)
{
if (fromWhere == 0) // Başhekimden geldiyse?
{
labelDoctor.Visible = true;
editDoctorWithoutHead.Visible = true;
}
else if (fromWhere == 1) // Normal Hekimden geldiyse
{
labelDoctor.Visible = false;
editDoctorWithoutHead.Visible = false;
}
}
And in form:
private void ExistRequestAndNewEntryForm_Shown(object sender, EventArgs e)
{
var obj = new DoctorPermissionApprove();
obj.LoadDoctorPermission(0);
}
For example I tried in shown event. But it still visible
I want to hide or show this components when the anybody open the form
Thank you a lot
In the UserControl class add a public property to set the internal label visibility true or false. This can be accessed from your parent form where your usercontrol is added.
Example:
public class YourUserControl
{
//This code will be in designer class
private Label lblYourLabelToHide = new Label();
//Create this public property to hide the label
public bool IsLabelVisible
{
set { lblYourLabelToHide.Visible = value; }
}
}
public class YourParentForm
{
//This will be in designer
private YourUserControl userControl = new YourUserControl();
public void Form_Load()
{
//based on some criteria
userControl.IsLabelVisible = false;
}
}

How do I pass values to user control? [duplicate]

This question already has answers here:
How to pass values to a user control in winforms c#
(2 answers)
Closed 8 years ago.
I have a windows form and I want to pass a value to a user control. I programmatically create the user control in the winform and set a value, but it doesn't get set. Here is the code where I create the user control:
namespace AddPanel
{
public partial class Form1 : Form
{
public Form1()
{
int a = db.CamTable1s.Count();
InitializeComponent();
DisplayImage(a);
}
private void DisplayImage(int rowNum)
{
test nt = new test();
nt.Location = new System.Drawing.Point(33, h);
nt.Name = "test1";
nt.usrID = "username";
nt.Size = new System.Drawing.Size(408, 266);
this.Controls.Add(nt);
}
}
}
I set a variable I made in test user control called nt.Name, then I just want to display it in a text box on the user control. Here is the code for the user control:
namespace AddPanel
{
public partial class test : UserControl
{
public string usrID { get; set; }
public test()
{
InitializeComponent();
//textBox1.Text = usrID;
}
public test(string Id)
{
InitializeComponent();
usrID = Id;
UCtextBox.Text = usrID;
}
}
}
Obviously, I don't know why this isn't working. Could someone help me out?
Even in WPF, where you have bindings, the UI will not automatically pick up a change to a property (without raising PropertyChanged). It definitely won't in the hard-coded land of WinForms.
So you have two problems:
You invoked the default constructor in your call, so no code ever sets the Text property of the textbox
Even if you had set the text, the subsequent change to the property would not propagate to the UI.
The simplest solution would be to just run the UI update in the setter:
private string usrID;
public string UserID //Correct style!
{
get { return usrID; }
set
{
usrID = value;
RCtextBox.Text = usrID;
}
}
You could also call a method from the setter, listen on INotifyPropertyChanged and a number of other things.
Another way would be to expose a method instead of a property:
public string UpdateUserID(string newId)
{
usrID = newId;
RCtextBox.Text = usrID;
}
You should put value passed into usrId property to the textbox.
public partial class test : UserControl
{
public string usrID
{
get{return _usrId;}
set
{
_usrId = value;
UCtextBox.Text = value;
}
}

how to make custom C# ComboBoxItem that inherits System.Windows.Forms.Control? if possible?

I need to create custom comboboxitem wich i can process among other controls that inherit System.Windows.Forms.Control class. So my comboboxitem must inherit System.Windows.Forms.Control because i use cast to that type and refer to Text property withn a loop.
There is some posts on how to create custom item but that one inherits Object class which is not working for me? I tried but it didnt work?Here is my try:
public class ComboItem : System.Windows.Forms.Control
{
public object Value { get; set; }
public override string Text { get; set; }
// public object Value { get; set; }
public ComboItem(string text) { this.Text = text; }
public ComboItem(string text, string value) { this.Text = text; this.Value = value; }
public override string ToString()
{
return Text;
}
}
nothing is displayed in combo box after following code
private void Form1_Load(object sender, EventArgs e)
{
ComboItem asd = new ComboItem("qweqwwqeq");
ComboItem asd2 = new ComboItem("2222222");
comboBox1.Items.Add(asd);
comboBox1.Items.Add(asd2);
comboBox1.SelectedIndex = 1;
}
this is context in which i need to use it:
System.Windows.Forms.Control ctrl = (System.Windows.Forms.Control)asd["Kontrola"];
ctrl.Text = (String)asd["Engleski"];
I assume you are not really talking about the ComboBoxItem Class, which is from WPF but simply of the Winforms Combobox which contains objects.
For some reason Controls don't get displayed in Collections unless they are actually placed in a Container Control.
So you probably have to wrap your Control in a minimal wrapper class like this:
class CtlWrapper
{
public Control theControl { get; private set; }
public CtlWrapper(string text)
{
theControl = new Control();
theControl.Text = text;
}
public CtlWrapper(Control control) { theControl = control; } // for fun
public override string ToString() { return theControl.Text; }
}
Not sure what you'll do with such a generic thing as Control in the ComboBox's Items list, but maybe you'll add some code to create different Control types..? With the wrapper the ToString text gets displayed as expected..
Of course you an add a Value string or whatever you need to the class as needed or you could use your own class as the type of 'theControl'..
Edit: For fun I have added a 2nd constructor to allow for adding existing Controls (of any type ;-)

Change custom control default Text property at design time

I created a user control. It's basically a button with some custom properties.
public partial class CustomButton : Button {
// My custom properties, constructor and events
}
Everytime I add this CustomButton on a form, its default Text value is set to "customButtonX", where X is 1, 2, 3, ...
How can I change this value? I would like it to be "buttonX" (X = 1, 2, 3...).
EDIT : I would like the trick (or whatever it is I have to do) to be active when I add a button on a form via the design view also. Meaning when I drag-drop a CustomButton from my toolbox to a form, its Text value should be "buttonX".
The default is "yourControlNameX" thats right. But you can replace the name in the constructor.
Note that this only will work at Runtime (not at design time)
public partial class CustomButton : Button {
// My custom properties, constructor and events
public CustomButton()
{
this.Text = this.Text.Replace("customButton ", "button");
}
}
When you drag a control from the Toolbox to a form there are some events that are triggered. In your case you have to subscribe to the one that is fired when the text property of your control is changed from String.Empty to the default name and change it. To do this you have to get the service that exposes these events (an implementation of IComponentChangeService) before the control is added to the form. This can be done overriding the Site property of your control. Modifying the example that you can find here, this kind of code should work:
private IComponentChangeService _changeService;
public override System.ComponentModel.ISite Site
{
get
{
return base.Site;
}
set
{
_changeService = (IComponentChangeService)GetService(typeof(IComponentChangeService));
if (_changeService != null)
_changeService.ComponentChanged -= new ComponentChangedEventHandler(OnComponentChanged);
base.Site = value;
if (!DesignMode)
return;
_changeService = (IComponentChangeService)GetService(typeof(IComponentChangeService));
if (_changeService != null)
_changeService.ComponentChanged += new ComponentChangedEventHandler(OnComponentChanged);
}
}
private void OnComponentChanged(object sender, ComponentChangedEventArgs ce)
{
CustomButton aBtn = ce.Component as CustomButton;
if (aBtn == null || !aBtn.DesignMode)
return;
if (((IComponent)ce.Component).Site == null || ce.Member == null || ce.Member.Name != "Text")
return;
if (aBtn.Text == aBtn.Name)
aBtn.Text = aBtn.Name.Replace("customButton", "button");
}
Just override the text and set whatever you want into it.
public partial class CustomButton : Button {
public override string Text
{
get
{
//return custom text
return base.Text;
}
set
{
//modify value to suit your needs
base.Text = value;
}
}
}
May be you could use a ControlDesigner and set the Text property after inizialized.
public class MultiDesigner : ControlDesigner
{
public MultiDesigner()
{
}
public override void InitializeNewComponent(IDictionary defaultValues)
{
base.InitializeNewComponent(defaultValues);
ICommonControl control = this.Component as ICommonControl;
control.Text = control.Tag.ToString();
}
}
decorate your control with..
[Designer("MultiPuntoDeVenta.Controls.Tickets.Editors.Designer.MultiDesigner, MultiPuntoDeVenta.Controls")]
public class LabelBase<T> : KryptonLabel, ICommonControl where T : ICommonControl
{
.....

change label text in usercontrol at run time

hello
i m new to c# and im working on a project,in which i made a usercontrol1 as
*label textbox datepicker*now i wnt to change the label text,i m trying this code but it is not working
using System;
using System.Windows.Forms;
namespace library_system
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
// private string DateLabel
public string DateLabel
{
**get { return DateLabel.Text; }//error when i write dateLabel.Text
set
{
DateLabel.Text= value;//error datelabel.Text
}**
}
i m using this code in usercontrol for is it right to do this way??
and in the main form i m writing code as
userControl11.DateLabel="From Date";//on for load event??Is this Right
Thanks in advance!!
You are writing a property and setting itself.
If your label name is lbl you could simply use lbl.Text="what you want";
If you need a property to have a stronger check on text, you could write:
public string DateLabel
{
get { return lbl.Text; }
set { lbl.Text = value; }
}
So in main form you could write (suppose you have a control named uc)
uc.DateLabel = "hello";
EDITED
To be clear: suppose you have
one label named lbl in your UserControl1 user control
a UserControl1 control in your main form named uc
In your user control code you can write:
public string DateLabel
{
get { return lbl.Text; }
set { lbl.Text = value; }
}
In your main form you can then write:
uc.DateLabel = "what you want";
Try changing it to this (controlname is id you have given to your label)
public string DateLabel
{
get { return controlname.Text; }
set
{
controlname.Text= value;
}
}
Your Property is Called DateLabel, and you are trying to set it.
That doesnt make sense.
Try the following. You will need to drag a asp:Label onto you usercontrol and call it lblDateLabel.
public string DateLabel
{
get { return lblDateLabel.Text; }
set { lblDateLabel.Text= value; }
}

Categories