Overriding Form.Text property - c#

I'm attempting to override Form.Text in order to modify the Title prior to appearing on the form.
As a proof of concept I created this class, which will be used in place of directly inheriting from Form:
public class FormWithVersionNumber : Form
{
[SettingsBindable(true)]
public override string Text
{
get
{
return "tester";
}
}
}
I would've expected all forms that inherit from this to have the Title "tester" but instead it is always blank. I've been through with breakpoints, and can't see any reason why this should happen. So what is the reason?

Because the actual Title is not retreived from Text but from the internal property WindowText in Control.
Here's an example of how you can do:
public partial class FormWithVersionNumber : Form
{
public override sealed string Text
{
get
{
return base.Text + " 1.0.0.0";
}
set
{
base.Text = value + " 1.0.0.0";
}
}
public FormWithVersionNumber()
{
InitializeComponent();
Text = "Some Title";
}
}

Related

C# dynamic design-time properties

I would like to have a control that allows a property to be shown if another property's value is set to a specific value. The following is a much simplified example of what I would like:
public class CustomButton : Control
{
private ButtonType _bType = ButtonType.OnOff;
private Int32 _minPress = 50; // 50 mS
public ButtonType Button_Type
{
get { return _bType; }
set { _bType = value; }
}
public Int32 Minimum_Press_Time // Only for momentary buttons
{
get { return _minPress; }
set { _minPress = value; }
}
}
public enum ButtonType
{
Momentary,
OnOff
}
On adding CustomButton to a Windows.Forms form, the Minimum_Press_Time will only show in the Properties window if Button_Type is changed to ButtonType.Momentary.
Is such a thing possible?
Yes, its possible to get close but it looks a little strange. I've done this on some controls before. Here is a full example of what you would need to do:
public partial class CustomButton : Control
{
private ButtonType _buttonType = ButtonType.OnOff;
private CustomButtonOptions _options = new OnOffButtonOptions();
[RefreshProperties(System.ComponentModel.RefreshProperties.All)]
public ButtonType ButtonType
{
get { return _buttonType; }
set
{
switch (value)
{
case DynamicPropertiesTest.ButtonType.Momentary:
_options = new MomentaryButtonOptions();
break;
default:
_options = new OnOffButtonOptions();
break;
}
_buttonType = value;
}
}
[TypeConverter(typeof(ExpandableObjectConverter))]
public CustomButtonOptions ButtonOptions
{
get { return _options; }
set { _options = value; }
}
public CustomButton()
{
InitializeComponent();
}
}
public enum ButtonType
{
Momentary,
OnOff
}
public abstract class CustomButtonOptions
{
}
public class MomentaryButtonOptions : CustomButtonOptions
{
public int Minimum_Press_Time { get; set; }
public override string ToString()
{
return Minimum_Press_Time.ToString();
}
}
public class OnOffButtonOptions : CustomButtonOptions
{
public override string ToString()
{
return "No Options";
}
}
So basically what is happening is you are using an ExpandableObjectConverter to convert an abstract type to a set of options. You then use the RefreshProperties attribute to tell the property grid that it will need to refresh the properties after this property changes.
This is the easiest way I've found to come as close to what you are asking for as possible. The property grid doesn't always refresh the right way so sometimes there will be a "+" sign next to an options set with no expandable properties. Use the "ToString" in the properties to make the display on the property grid look intelligent.

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 create custom TextBox control?

I want to perform Trim() method on each TexBox control on my page, before value is returned. I dont' want to hard-code the same code for each TexBox control, I want to do it in more elegant way.
I've found made the following class
namespace System.Web.UI.WebControls
{
public partial class TrimmedTextBuox : TextBox
{
private string text;
public override string Text
{
get { return string.IsNullOrEmpty(text) ? text : text.Trim(); }
set { text = value; }
}
}
}
but it fails, while debuggind the compiler doesn't get inside get{} and set{}.
After that, I created a UserControl item, but it must be deriverd from System.Web.UI.UserControl, not System.Web.UI.WebControls.TextBox to get it work (there's an exception which points to that)
So, how can I do that ?
First you have to register your control in your .aspx page like that:
<%# Register TagPrefix="customControls" Namespace="WebApplication.Custom.Controls" Assembly="WebApplication"%>
Then you can call it using the markup
<customControls:TrimmedTextBuox ID="txtTrim" runat="server"/>
Plus you don't have to create another "text" property in your custom TextBox. Instead, it can be done like that:
namespace WebApplication.Custom.Controls
{
public class TrimmedTextBuox : TextBox
{
public override string Text
{
get
{
return base.Text;
}
set
{
if (!String.IsNullOrEmpty(value))
base.Text = value.Trim();
}
}
}
}
This will trim recursively all text boxes before inserting.
public static void trimRecursive(Control root)
{
foreach (Control control in root.Controls)
{
if (control is TextBox)
{
var textbox = control as TextBox;
textbox.Text = textbox.Text.Trim();
}
else
{
trimRecursive(control);
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
trimRecursive(Page);
}
Simple Solution to your problem is to hide the Text property of your base class by using new keyword. sample code...
public class TrimmedTextBox : TextBox
{
public new string Text
{
get
{
var t = (string) GetValue(TextProperty);
return t != null ? t.Trim() : string.Empty;
}
}
}
For more info about how new keyword with property works refrer to this SO Question

Overriding "Text" property of a control extended from Button using C#

I'm having a problem extending the standard WebControls.Button control. I need to override the text property, but I receive the error message:
cannot override inhereted member 'System.Web.UI.WebControls.Button.Text.get' because it is not marked virtual, abstract or override
I used the following code for a LinkButton, and that worked perfectly:
public class IconLinkButton : LinkButton
{
private string _icon = "";
public string Icon
{
get
{
return _icon;
}
set
{
_icon = value;
}
}
public override string Text
{
get
{
return "<i class=\""+Icon+"\"></i> " + base.Text;
}
set
{
base.Text = value;
}
}
}
However, doing the same thing for a standard Button kicks up the error I described above.
public class IconButton : Button
{
private string _icon = "";
public string Icon
{
get
{
return _icon;
}
set
{
_icon = value;
}
}
public virtual string Text
{
get
{
return "<i class=\"" + Icon + "\"></i> " + base.Text;
}
set
{
base.Text = value;
}
}
}
How can I fix this?
This is because LinkButton has a virtual Text property.. whilst Button does not.
You can hide the base functionality entirely by using new:
public class IconButton : Button {
public new string Text {
// implementation
}
}
Using new hides the inherited member completely.

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