I have my own TextBox2 class that derives from TextBox. I want to add a state called TextBlock and I want the VisualStateManager to go to that state when the IsTextBlock property/dependency property is true. When this is true, I want to change the style of the text box to be readonly and look just like a TextBlock but be able to select the text to be copyable. Is this possible? Is there a better way?
Something like that:
[TemplateVisualState(Name = "TextBlock", GroupName = "ControlType")]
[TemplateVisualState(Name = "TextBox", GroupName = "ControlType")]
public class TextBox2 : TextBox
{
public TextBox2()
{
DefaultStyleKey = typeof (TextBox2);
Loaded += (s, e) => UpdateVisualState(false);
}
private bool isTextBlock;
public bool IsTextBlock
{
get { return isTextBlock; }
set
{
isTextBlock = value;
UpdateVisualState(true);
}
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
UpdateVisualState(false);
}
internal void UpdateVisualState(bool useTransitions)
{
if (IsTextBlock)
{
VisualStateManager.GoToState(this, "TextBlock" , useTransitions);
}
else
{
VisualStateManager.GoToState(this, "TextBox" , useTransitions);
}
}
}
HTH
Related
I have a bunch of labels that I set their value in the designer and later during runtime update them, but after using them, I want to set them back to their default value. My intent with this is to reduce the amount of large code to help make it easier to read.
random example like, setting in the designer of lbl_fruit Text = no fruits available currently then
*code*
lbl_fruits.Text = "banana";
*code*
lbl_fruits.ResetText(); // I want something like this
lbl_fruits.Text = "no fruits available currently"; // Instead of this
The .ResetText(); doesn't work for this as the label text gets cleaned instead of returning to "no fruits available currently"
My current solution is making a custom label control.
public class ExLabel : Label
{
private string defaultValue = "";
public string DefaultValue
{
get { return defaultValue; }
set { defaultValue = value; this.Invalidate(); }
}
protected override void OnControlAdded(ControlEventArgs e)
{
defaultValue = this.Text;
MessageBox.Show("This code is being run");
base.OnControlAdded(e);
}
public void ResetValue()
{
this.Text = defaultValue;
}
}
This code currently solves my problem if I use the custom propriety I made, but for me the ideal solution would be to have the design-time text value as the default value and not an extra propriety I made. OnControlAdded() does not get executed, OnPaint() runs again when lbl_fruits.Text = "banana"; happens.
So the question is: Which event I can override so the code gets executed as soon as the label is loaded but doesn't run twice. And also, is there a simpler way of approaching this?
In the end the solution I used was this:
public class ExLabel : Label
{
private string defaultValue = "";
public string DefaultValue
{
get { return defaultValue; }
set { defaultValue = value; this.Invalidate(); }
}
protected override void OnPaint(PaintEventArgs e)
{
if(defaultValue == "" && !this.Text.Contains("exLabel"))
{
defaultValue = this.Text;
}
base.OnPaint(e);
}
public void ResetValue()
{
this.Text = defaultValue;
}
}
public class ExLabel : Label
{
private string defaultValue = "";
public string DefaultValue
{
get { return defaultValue; }
set { defaultValue = value; this.Invalidate(); }
}
protected override void OnControlAdded(ControlEventArgs e)
{
defaultValue = this.Text;
MessageBox.Show("This code is being run");
base.OnControlAdded(e);
}
public void ResetValue()
{
this.Text = defaultValue;
}
}
I have created a custom web server control that will have a bool property 'RenderAsLabel' so that I can convert textboxes to labels, for Read-only forms. I was wondering if there is any reason why this code should not be safe, or work as intended. Upon initial testing it seems fine, but I just want to make sure I'm not doing something that will end up causing issues.
namespace OrmControlLibrary
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:OrmTextBox ID='' runat=server ></{0}:OrmTextBox>")]
public class OrmTextBox : TextBox
{
private Label lbl;
public virtual bool RenderAsLabel
{
get
{
if (ViewState["OrmTextBox"] == null)
{
return false;
}
else
{
return (bool)ViewState["OrmTextBox"];
}
}
set
{
ViewState["OrmTextBox"] = value;
}
}
protected override void Render(HtmlTextWriter w)
{
if (RenderAsLabel)
{
SetLabelProperties();
lbl.RenderControl(w);
}
else
{
base.Render(w);
}
}
private void SetLabelProperties()
{
lbl = new Label();
lbl.ID = this.ID;
lbl.CssClass = this.CssClass;
lbl.Text = this.Text;
}
}
}
I am creating custom web control(textbox) which allows only integer data type in textbox.
On button submit event if there is no data in textbox, custom control should set TextBox1.Text=0, this is what i want. This is code which i wrote.
public class MasIntTextBox : TextBox
{
private RequiredFieldValidator req;
private RegularExpressionValidator regex;
public string ValGrp { get; set; }
public string IsRequired { get; set; }//give true/yes or false/no values only
protected override void OnInit(EventArgs e)
{
//this.CssClass = "inp-form";
if (IsRequired == "true" || IsRequired == "yes")
{
req = new RequiredFieldValidator();
req.ControlToValidate = this.ID;
req.ErrorMessage = "Enter Numeric Value";
req.Display = ValidatorDisplay.Dynamic;
if (!string.IsNullOrEmpty(ValGrp))
req.ValidationGroup = ValGrp;
Controls.Add(req);
}
regex = new RegularExpressionValidator();
regex.ControlToValidate = this.ID;
regex.ErrorMessage = "Numeric Value Only";
regex.Display = ValidatorDisplay.Dynamic;
regex.ValidationExpression = "^\\d+(\\.\\d+)?$";
if (!string.IsNullOrEmpty(ValGrp))
regex.ValidationGroup = ValGrp;
Controls.Add(regex);
//base.OnInit(e);
}
protected override void Render(HtmlTextWriter w)
{
base.Render(w);
if (IsRequired == "true" || IsRequired == "yes")
req.RenderControl(w);
regex.RenderControl(w);
}
}
I checked few events or methods for this but they got fired after btnA_click event.
I am kind of newbie in this.
Any event or method i am missing? Any suggestion accepted.
Thanks in Advance...
You should try to set the default value on TextBox.LostFocus event. This event fires immediately after focus is lost from the control (TextBox).
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 have a list of classes, but different children have different properties that need to be displayed.
What I want to achieve is to have a listbox-type control in the gui which enables each child to display it's properties the way it wants to - so not using the same pre-defined columns for every class.
I envisage something like the transmission interface (below), where each class can paint it's own entry, showing some text, progress bar if relevant, etc.
How can this be achieved in C#?
Thanks for any help.
Let your list items implement an interface that provides everything needed for the display:
public interface IDisplayItem
{
event System.ComponentModel.ProgressChangedEventHandler ProgressChanged;
string Subject { get; }
string Description { get; }
// Provide everything you need for the display here
}
The transmission objects should not display themselves. You should not mix domain logic (business logic) and display logic.
Customized ListBox:
In order to do display listbox items your own way, you will have to derive your own listbox control from System.Windows.Forms.ListBox. Set the DrawMode property of your listbox to DrawMode.OwnerDrawFixed or DrawMode.OwnerDrawVariable (if the items are not of the same size) in the constructor. If you use OwnerDrawVariable then you will have to override OnMeasureItem as well, in order to tell the listbox the size of each item.
public class TransmissionListBox : ListBox
{
public TransmissionListBox()
{
this.DrawMode = DrawMode.OwnerDrawFixed;
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
e.DrawBackground();
if (e.Index >= 0 && e.Index < Items.Count) {
var displayItem = Items[e.Index] as IDisplayItem;
TextRenderer.DrawText(e.Graphics, displayItem.Subject, e.Font, ...);
e.Graphics.DrawIcon(...);
// and so on
}
e.DrawFocusRectangle();
}
}
You can let your original transmission class implement IDisplayItem or create a special class for this purpose. You can also have different types of objects in the list, as long as they implement the interface. The point is, that the display logic itself is in the control, the transmission class (or whatever class) only provides the information required.
Example:
Because of the ongoing discussion with Mark, I have decided to include a full example here. Let's define a model class:
public class Address : INotifyPropertyChanged
{
private string _Name;
public string Name
{
get { return _Name; }
set
{
if (_Name != value) {
_Name = value;
OnPropertyChanged("Name");
}
}
}
private string _City;
public string City
{
get { return _City; }
set
{
if (_City != value) {
_City = value;
OnPropertyChanged("City");
OnPropertyChanged("CityZip");
}
}
}
private int? _Zip;
public int? Zip
{
get { return _Zip; }
set
{
if (_Zip != value) {
_Zip = value;
OnPropertyChanged("Zip");
OnPropertyChanged("CityZip");
}
}
}
public string CityZip { get { return Zip.ToString() + " " + City; } }
public override string ToString()
{
return Name + "," + CityZip;
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null) {
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
Here is a custom ListBox:
public class AddressListBox : ListBox
{
public AddressListBox()
{
DrawMode = DrawMode.OwnerDrawFixed;
ItemHeight = 18;
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
const TextFormatFlags flags = TextFormatFlags.Left | TextFormatFlags.VerticalCenter;
if (e.Index >= 0) {
e.DrawBackground();
e.Graphics.DrawRectangle(Pens.Red, 2, e.Bounds.Y + 2, 14, 14); // Simulate an icon.
var textRect = e.Bounds;
textRect.X += 20;
textRect.Width -= 20;
string itemText = DesignMode ? "AddressListBox" : Items[e.Index].ToString();
TextRenderer.DrawText(e.Graphics, itemText, e.Font, textRect, e.ForeColor, flags);
e.DrawFocusRectangle();
}
}
}
On a form, we place this AddressListBox and a button. In the form, we place some initializing code and some button code, which changes our addresses. We do this in order to see, if our listbox is updated automatically:
public partial class frmAddress : Form
{
BindingList<Address> _addressBindingList;
public frmAddress()
{
InitializeComponent();
_addressBindingList = new BindingList<Address>();
_addressBindingList.Add(new Address { Name = "Müller" });
_addressBindingList.Add(new Address { Name = "Aebi" });
lstAddress.DataSource = _addressBindingList;
}
private void btnChangeCity_Click(object sender, EventArgs e)
{
_addressBindingList[0].City = "Zürich";
_addressBindingList[1].City = "Burgdorf";
}
}
When the button is clicked, the items in the AddressListBox are updated automatically. Note that only the DataSource of the listbox is defined. The DataMember and ValueMember remain empty.
yes, if you use WPF it is quite easy to do this. All you have to do is make a different DataTemplate for your different types.
MSDN for data templates
Dr. WPF for Items Control & Data Templates