Ok - I just need some ideas here. The scenario is - There are a lot of fields in my windows form which are mandatory to be entered by the user. One of them might be "Name". So for this, I have a label lblName with text Name, another label lblMandatory with text * colored in red which signifies it is mandatory. So that means I have two labels for a field Name, and similarly I have more than 20 fields in my form. I was just thinking of creating a custom label - something called MandatoryLabelControl which will have a * by default after it's text. This would help me in decreasing the number of labels in my form. The custom label is actually a combination of two things - First a text for the label, secondly the * in red color. I searched for this a lot, but can't find anything to start with. Please help with some suggestions.
You have two options - a custom label control or a user control that contains the label and the * based on a Mandatory property. If you have an explicit need to make the "*" red (seems to be the case per your q), you will need to use the user control. This is slightly heavier, so I would recommend rethinking that requirement. Here's how the custom label control would look:
public class CustomLabel : Label
{
public CustomLabel()
{
}
public bool Mandatory { get; set; }
public override String Text
{
get
{
return base.Text + " *";
}
}
}
You would now use the CustomLabel instead of the Label.
Related
I am sorry if this has been answered before. I searched around and couldn't find a suitable answer.
I have a numericupdowncontrol that takes user input. Right now I have set decimalPlaces property set to 2.
If user enters 1.23, it stays correct. However if user enters 1.2, then it displays 1.20. That is not what I want. It should display 1.2 and not 1.20. Is there a way to do this?
If user enters 1, then it should be 1 and not 1.00. How do do this?
Thanks much!
If you don't mind customizing your NumericUpDown, you can do this which is very easy, short and reliable:
//You can use this class instead of the standard NumericUpDown
public class CustomNumericUpDown : NumericUpDown
{
//Override this to format the displayed text
protected override void UpdateEditText()
{
Text = Value.ToString("0." + new string('#', DecimalPlaces));
}
}
I have custom control - using Win Forms, that contains four TextBoxes, all have property to turn them off or on - I just setting visible parameter on them.
I would like to change size and position of the custom control - for example, when I turn off first textbox, I would like to change position of all 3 componets below him, to get them higher.
Of course, I would like to work it with every TextBox - every TextBoxes, below TextBox I am changing position, should change position.
I cant achieve it with changing of Position of TextBox in its own property - I can ask TextBox on top of me, if its property is set to on or of, but it dont works, because I dont know order of setting property in the application.
I can change position of TextBox below me - in the property of Top textbox, but I can do that with only one TextBox below, I dont know and cant find out, if two TextBoxes below are not off and fourth TextBox should be on position of second.
I cant change it by using some variable - when I change it, other TextBoxes dont care about it and they have set their position before.
So do you have any idea how could I achieve it?
The FlowLayoutPanel is designed for exactly this kind of behavior. Place your textboxes inside a FlowLayoutPanel, and then when you set the visible property of one or more of them to false, the other textboxes will automatically move up (or over if that's how you have it set up).
If you want for some reason do it manually, just make a chain of controls.
public class CustomTextBox
{
public CustomTextBox(CustomTextBox previousSibling)
{
PreviousSibling = previousSibling;
}
public CustomTextBox PreviousSibling { get; private set; }
public CustomTextBox PreviousVisibleSibling
{
get
{
if (PreviousSibling == null)
{
return null;
}
return PreviousSibling.Visible ? PreviousSibling : PreviousSibling.PreviousVisibleSibling
}
}
}
I am making a form that contains a lot of User Controls, each User Control is part of the form (contains TextBox, ComboBox etc).
User will use the form to update their information.
At end of submission, I need to display the original data and the data that the user have entered.
I wonder if is possible that I can replace the input control (TextBox etc) to Label?
So I can just simply use the same user control, then convert each of the input control to label to display the data... (I just don't really want to use readonly or disable)
Note: I used different dataset to map each of the User Control data....
What I was thinking to do is like to get the input control from Page.Controls:
aInputControl = new Label();
or...
Page.Controls.Remove(aInputControl);
Then somehow add new Label in same position in the page...
But I have no idea how...can't think of anything except add another div to surround each of the control...
I just wonder if it this is possible...
Thanks in advance.
================
Edit: Seems like making new user control is not a good way for me....I will just try to somehow map each original data and new data into a new User control, and write them into page...but anyway, thanks for the idea guys.
You can make a custom user control that contains a TextBox and a Label, and displays one or the other depending on whether or not it has a value.
I would create a user control or a web control to encapsulate that functionality. Add a Property to change the display mode and some logic in the control to determine which control to show.
Here is a sample of code to give you an idea, I can expand on this if you would like.
public class ReadOnlyControl<T> : WebControl where T : Control, ITextControl {
protected T inputControl;
protected Label lblLabel;
public bool IsReadOnly { get; set; }
public string Text { get; set; }
protected override void Render(HtmlTextWriter writer) {
Control control = IsReadOnly ? lblLabel : (Control)inputControl;
((ITextControl)control).Text = Text;
control.RenderControl(writer);
}
}
You may want to rethink your design. I recommend you have another view/page that displays the data summary after submit. Additionally, you will have more control of the formatting this way. I don't want to sound condescending, but it just sounds like you are being a little lazy.
I have an initial value property like this:
[Category("Main")]
[Description("Intial Value")]
[DefaultValue(10)]
public int InitialValue
{
get { return m_initialValue; }
set {
m_initialValue = value;
this.TrackBar.Value = this.m_initialValue;
}
}
So in my constructor I do this for example:
this.InitialValue = 10;
To my surprise when dragging the custom control on a form the setter is not called so that my trackbar value is not synchronized.
Why ?
Only when I change the property in dialog box the setter is called.
I decided to take your advice as suggested in one of the comments:
You can try by yourself will take 2 minutes.
So I did (it took about 3 minutes), and I was unable to reproduce the behavior that you described.
Here are the exact steps that I followed:
Created a new Windows Forms Application.
Added a new User Control to my project.
Opened the new User Control in design view and added a TrackBar control (leaving the TrackBar control's properties all set to their defaults).
Added the following code to the User Control class (exactly the same as you posted above, with the addition of a private field m_initialValue that you omitted from the original example):
public class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
this.InitialValue = 10;
}
[Category("Main")]
[Description("Intial Value")]
[DefaultValue(10)]
public int InitialValue
{
get { return m_initialValue; }
set
{
m_initialValue = value;
this.trackBar1.Value = this.m_initialValue;
}
}
int m_initialValue;
}
Built the project.
Opened the default Form (Form1) that was created with the new project in design view.
Dragged the User Control that I had just created (UserControl1) out of the toolbox where it was automatically placed and onto the surface of the form.
The indicator on the slider bar appeared all the way to the right side (the correct and expected position given the default Maximum value of 10). Now, you tell me: What are we doing differently?
Try adding [Browsable(true)] .
The key portion of your question is here:
when dragging the custom control on a form
You're still in the designer, and the designer cheats a bit to render things. Does this still happen when you actually run the application?
Is it possible to get the text area of a NumericUpDown control? I'm looking to get it's size so that I can mask it with a panel. I don't want the user to be able to edit AND select the text. Is this possible? Or is there another way of covering up the text in the text box?
Thanks.
You can get this by using a Label control instead of the baked-in TextBox control. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form.
using System;
using System.Windows.Forms;
class UpDownLabel : NumericUpDown {
private Label mLabel;
private TextBox mBox;
public UpDownLabel() {
mBox = this.Controls[1] as TextBox;
mBox.Enabled = false;
mLabel = new Label();
mLabel.Location = mBox.Location;
mLabel.Size = mBox.Size;
this.Controls.Add(mLabel);
mLabel.BringToFront();
}
protected override void UpdateEditText() {
base.UpdateEditText();
if (mLabel != null) mLabel.Text = mBox.Text;
}
}
If you want do disallow manual editing, you can just set ReadOnly property to true.
updown.ReadOnly = true;
If you want to disallow selecting too (I wonder why you need this), you can use reflection. I don't think there's better way, because the field upDownEdit is internal field of UpDownBase.
FieldInfo editProp = updown.GetType().GetField("upDownEdit", BindingFlags.Instance | BindingFlags.NonPublic);
TextBox edit = (TextBox)editProp.GetValue(updown);
edit.Enabled = false;
Set the ReadOnly property to true, that's all.
The 'proper' way to do this is to create an Up-Down control and a Label (the label can't be selected or edited). However, the authors of Windows Forms, in their infinite wisdom, have decided that we don't need the Up-Down control and so they didn't provide a .NET wrapper for one. They decided that the only reason we could ever want an Up-Down control is when paired with a TextBox control.
The Up-Down control is simple enough to create a light wrapper if you want to go this route: http://msdn.microsoft.com/en-us/library/bb759880.aspx
Edit 1
[snip]
Edit 2
I blogged about it here: http://tergiver.wordpress.com/2010/11/05/using-the-up-down-control-in-windows-forms/