I have a TextBox UserControl. I create a Dynamic Property for the Textbox for MaximumLength.
public int MaximumLength { get; set; }
private void txtLocl_KeyPress(object sender, KeyPressEventArgs e)
{
txtLocl.MaxLength = MaximumLength;//txtLocl is a Usercontrol Textbox..,
//txtLocl maxLength should be given by the user in WindowsForm
//that should be come to here...,
}
I show you the Image of the UserControl Properties in Windows Form
Now i want to verify when user change the value in that property...,
Implement a custom setter that checks if the value is valid.
public int MaximumLength
{
get
{
return this.maximumLength;
}
set
{
if(value <= 4)
{
MessageBox.Show("Value is too small.");
}
else this.maximumLength = value;
}
}
Edit: So implement a getter.
Related
I have two user control's . In the first user control(Class) i have one textbox.
Now in my second user control (Test), i want to get the value of that textbox.
in my page, when user enter a value in texbox of the first usercontrol, how can i get this in the hidden field of the second usercontrol
How can I do this??
I have these properties in my usercontrols
Class User Control
public string Class_ClientId
{
get { return txtClass.ClientID; }
}
public string Class_Text
{
get { return Class; }
set
{
if (value != Class)
{
Class = value;
txtClass.Text = Class;
}
}
}
Test user control
public string KMAT_Text
{
get { return KMATName; }
set
{
if (value != KMATName)
{
KMATName = value;
txtKmat.Text = KMATName;
}
}
}
public string Class
{
get { return _hdnClass; }
set
{
if (value!= _hdnClass)
{
_hdnClass = value;
hdnClass.Value = _hdnClass;
}
}
}
There are a variety of ways to do this. The easiest to implement would be to define a change event on the first user control:
public event EventHandler SomethingChanged;
protected void OnSomethingChanged(EventArgs e)
{
if (SomethingChanged != null)
SomethingChanged(this, e);
}
public string Class_Text
{
get { return Class; }
set
{
if (value != Class)
{
Class = value;
txtClass.Text = Class;
this.OnSomethingChanged(EventArgs.Empty);
}
}
}
Have the page listen for it, and have the page pass the value to the second user control.
I have the a class in my application. It has been bound to winform textbox controls. But the textbox which is bound to BookingNo property, always shows zero (0). But i want the textbox keep empty. Is there any way to do it? Here is my code snippet.
public class Booking
{
private int pBookingNo;
private string pCustomerName;
private string pAddress;
public int BookingNo
{
get { return pBookingNo; }
set
{
if (!value.Equals(pBookingNo))
{
pBookingNo = value;
}
}
}
public string CustomerName
{
get { return pCustomerName; }
set
{
if (!value.Equals(pCustomerName))
{
pCustomerName = value;
}
}
}
public Booking() { }
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
AddDataBindings();
}
private void AddDataBindings()
{
bsBooking.DataSource = typeof(Booking);
txtBookingNo.DataBindings.Add("Text", bsBooking, "BookingNo", true, DataSourceUpdateMode.OnPropertyChanged, null, "G", GlobalVariables.CurrentCultureInfo);
txtCustomerName.DataBindings.Add("Text", bsBooking, "CustomerName");
}
}
The default value of an Integer is 0, so you have to wrap it into some other object, which supports values other than 0, like
public int? BookingNo { get; set; }
You can use Nullable Type
public int? pBookingNo
{
get;
set;
}
Link : http://msdn.microsoft.com/fr-fr/library/1t3y8s4s(v=vs.80).aspx
You could use custom formatting for the binding by adding a handler to the Format event (http://msdn.microsoft.com/en-us/library/system.windows.forms.binding.format.aspx) and return an empty string when the value is zero. But you wouldn't be able to tell whether the value is actually zero or it just hasn't been set already, in which case using the int? approach suggested by #Grumbler85 is better.
what´s about:
textBox1.BindingContextChanged += new EventHandler(BindingContext_Changed);
private void BindingContext_Changed(object sender, EventArgs e)
{
TextBox txtBox = (TextBox)sender;
if (txtBox.Text == "0"){
txtBox.Text = "";
}
}
don´t know if it works, just an idea.
I have created a custom control button by extending the System.Windows.Forms.Button class.
I have set the default .Text .Width and .Height in the constructor of the new class.
When I drop this control onto a form, the IDE is smart enough to pay attention to the Width and Height specified in the constructor and assign these properties to the new button being created, but it ignores the Text property, and assignes the .Text of the button to be "ucButtonConsumables1"
Is there a way to set the .Text to a default value of my choosing?
public partial class ucButtonConsumables : System.Windows.Forms.Button {
public ucButtonConsumables() {
this.Text = "Consumables";
this.Width = 184;
this.Height = 23;
this.Click += new EventHandler(ucButtonConsumables_Click);
}
void ucButtonConsumables_Click(object sender, EventArgs e) {
MessageBox.Show("Button Clicked")
}
}
Hide Text property from designer serialization:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public override string Text
{
get { return base.Text; }
set { base.Text = value; }
}
Or create designer with default values:
public class ConsumablesButtonDesigner : System.Windows.Forms.Design.ControlDesigner
{
public override void OnSetComponentDefaults()
{
base.OnSetComponentDefaults();
Control.Text = "Consumables";
}
}
And provide that designer to your button:
[Designer(typeof(ConsumablesButtonDesigner))]
public class ucButtonConsumables : Button
{
//...
}
Yes, it is not possible for doing it in the constructor. If you are sure that the value will not be changed again do it this way. Overriding the Text Property and returning the constant.
public override string Text
{
get
{
return "Consumables";
}
set
{
}
}
You have to override the Text property in derived class to change.
public override string Text { get; set; }
I've set up a simple form. A ListBox takes values from a list in the 'business object' displaying the Name property and providing the Value property.
In additon the ListBox's SelectedItem property is bound to a property in the same business object.
Using the UI to select a value from the list correctly changes the objects property (checked when the button is clicked) and the correct value is available. So far so good.
However, if the ListBox's SelectedIndex property is changed in the code, then the UI correctly changes as expected but the business property does not change - it would appear to have missed the change event. This is true for both setting in the constructor and in the button event handler (see the code).
What have I missed or what am I doing incorrectly.
(I've only included the code I've written - not VS wizard generated stuff)
class Frequency
{
public String Name { get; set; }
public Int16 Value { get; set; }
public Frequency(String name, Int16 value)
{
Name = name;
Value = value;
}
}
class FrequencyList : System.ComponentModel.BindingList<Frequency>
{
}
class Model
{
public static FrequencyList FrequencyValues = new FrequencyList()
{
new Frequency("Slowest", 100),
new Frequency("Slow", 150),
new Frequency("Medium", 1000),
new Frequency("Fast", 5500),
new Frequency("Fastest", 10000)
};
public Frequency StartFrequency { get; set; }
public void DoStuff()
{
if (StartFrequency == null)
return;
Int16 freq = StartFrequency.Value;
}
}
public partial class Form1 : Form
{
private Model myModel = new Model();
public Form1()
{
InitializeComponent();
// Bind the list to a copy of the static model data
this.listBox1.DataSource = Model.FrequencyValues;
// Bind the control to the model value
this.listBox1.DataBindings.Add("SelectedItem", myModel, "StartFrequency");
// Select the start value
this.listBox1.SelectedIndex = 3;
}
private void button1_Click(object sender, EventArgs e)
{
Int16 f = (Int16)listBox1.SelectedValue;
this.myModel.DoStuff();
int new_index = listBox1.SelectedIndex + 1;
if (new_index >= listBox1.Items.Count)
new_index = 0;
listBox1.SelectedIndex = new_index;
}
}
You don't want the Click event, you want the SelectedIndexChanged event. This will trigger regardless of whether the user or the program instigates the change.
Lets say we have 0 displayed in value field of the control and I want that if the value is 0 - display string.Empty (I know that the type of value is decimal and there can be no string inserted instead of decimals in it, but still... Maybe there is some formatting possible there?).
Note: This is dependent on the current implementation of NumericUpDown.
What you need to do is create a new control that inherits from NumericUpDown such that:
public partial class SpecialNumericUpDown : NumericUpDown
{
public SpecialNumericUpDown()
{
InitializeComponent();
}
protected override void UpdateEditText()
{
if (this.Value != 0)
{
base.UpdateEditText();
}
else
{
base.Controls[1].Text = "";
}
}
}
public partial class MyNumericUpDown : NumericUpDown
{
public override string Text
{
get
{
if (base.Text.Length == 0)
{
return "0";
}
else
{
return base.Text;
}
}
set
{
if (value.Equals("0"))
{
base.Text = "";
}
else
{
base.Text = value;
}
}
}
}
It seems that there is only very limited support for changing the formatting.
I have not tried this myself. But you could create a subclass and override the UpdateEditText method to support your custom format. Something like this:
protected override void UpdateEditText()
{
this.Text = Value.ToString(); // Insert your formatting here
}
An easier solution is calling the ResetText() method. You can restore the text changing the Value property.
Example code to hide text when NumericUpDown control is disabled, and restore it on enabled
private void NumericUpDown_EnabledChanged(object sender, EventArgs e)
{
if (numericUpDown.Enabled)
{
if (numericUpDown.Tag != null)
{
// Restore last value
numericUpDown.Value = (decimal)numericUpDown.Tag;
}
}
else
{
// Save last value
numericUpDown.Tag = numericUpDown.Value;
// Just to force value change
numericUpDown.Value = (numericUpDown.Value > numericUpDown.Minimum ? numericUpDown.Minimum : numericUpDown.Maximum);
// Clear text
numericUpDown.ResetText();
}
}
If you only want to hide the value from the user, you can make ForeColor the same as BackColor so the value inside NumericUpDown will be invisible to the user.