Decimal places in NumericUpDown control - c#

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));
}
}

Related

How can i make to label return values in C#?

So guys i am new in C# and i want to know how to make to label return some values.
One example :
Label1 will be 75 dollars when i click in radiobutton.
When i choose the radiobutton, will change the value of 0 to 75 dollars in the painel,and will be added to the value of the buy.
I already tried some stuffs but didn't worked,i am really freaking out with this.
Please help, I need to do that for the Course of Programming.
You can use Text property for set and get value from label, such as:
int myIntVariable1 = 934;
myLabel.Text = myIntVariable1;
int myIntVariable2 = Convert.ToInt32(myLabel.Text);
Now myIntVariable1 and myIntVariable2 will have 934 value
A label cannot return a value,
if you're using a radio button \ button group, you could need to fire an CheckedChanged event, easiest way would be get the radio buttons on your form, double click for the event to get generated, then use a switch statement to have the label changed
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
label1.Text = "your new value or text here";
}
Also, just as a note of interest for you and the course you're doing, sign up to PluralSight which has mass of courses, and will teach you all you need to know about the fundamentals of C# and much more. You can get a free 10 or 14 day trial and then you can sign up from $29 per month and cancel at any time.

A mandatory label user control in C#

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.

Custom control components setting position C#

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
}
}
}

Is it possible to convert input control to label?

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.

How to inherit base Textbox to provide colored borders

I have a winform app that we use in house. It has many individual controls on each of it's 25 "pages"(usercontrols). Our user base prefers very technicolor apps...they want a TextBox to be outlined Blue if it is a required field (color should go away if data entered). They want a TextBox to change to outlined Green if data has been changed in it to remind them to save. They want a currently highlighted TextBox to be outlined RedOrange.
I have been trying to come at this from many different angles (some of you have probably seen similar posts by me lately). Non of them work... So one way I KNOW will work is to register the paint event for every control and check for a "required" tag for the required portion. The OnFocus for the current field portion and finally the Validate event for the data changed portion.
I know this is not the best way or at least I STRONGLY suspect it isn't but I am out of time, nearly, and nearing my point of frustration. That being said, will that DESTROY my app's responsiveness? Is there a better way? Can I override the base control to color on different premises so that I don't have to go to each of the 100+ controls?
Any idea would be welcome because I am between my stupid Paint_Event idea and rewriting all the controls in WPF... :)
I will be rewarding a solution that works for me and that I can implement shortly with a Bounty.
I am so sick of colors...
Here is my attempt based on suggestions.
public class MyTextBox : TextBox
{
private bool _isRequired;
public bool isRequired
{
get
{
return _isRequired;
}
set
{
_isRequired = value;
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (isRequired && base.Text.Equals(string.Empty))
{
HighlightControl(e.Graphics);
}
}
private void HighlightControl(Graphics graphics)
{
ControlPaint.DrawBorder(
graphics,
this.ClientRectangle,
Properties.Settings.Default.RequiredFieldColor,
Properties.Settings.Default.BorderWidth,
Properties.Settings.Default.BorderStyle,
Properties.Settings.Default.RequiredFieldColor,
Properties.Settings.Default.BorderWidth,
Properties.Settings.Default.BorderStyle,
Properties.Settings.Default.RequiredFieldColor,
Properties.Settings.Default.BorderWidth,
Properties.Settings.Default.BorderStyle,
Properties.Settings.Default.RequiredFieldColor,
Properties.Settings.Default.BorderWidth,
Properties.Settings.Default.BorderStyle);
}
}
I don't know the particulars of your app, but you could derive your own control from the base TextBox and let it handle much of this for you. Some thoughts:
Give it a bool Required property and some internal logic to color accordingly.
Have the textbox respond to its own
events - when text is entered, it can
do the right thing - change colors or
whatever is appropriate.
Provide your derived control with
properties to set the colors that get
used for each condition, then you can
switch them easily when the users
decide they want pink rather than
green.
You can utilize the focus events to "know" whether your TextBox (this is the one control you mentioned, so I'll assume this is the main control used here) has focus or lost it and the text change events can be used to drive all the color changes to the control.
You can certainly wire up all the
text boxes to control the
Apply/OK/whatever buttons to
determine if the buttons should be
enabled, assuming you have an Apply button or something like that which stores the data on click. There are a number of ways to communicate this. It's easy enough to iterate through the controls and ask their state.
Seems like this would work just fine.
What have you tried? You didn't really mention what you'd tried that didn't work.
You've got a problem, TextBox is, erm, special. Windows Forms leaves all painting to the native Windows EDITBOX control, the Paint event won't be raised. That can be fixed by setting the UserPaint control style to true:
using System;
using System.Drawing;
using System.Windows.Forms;
class MyTextBox : TextBox {
public MyTextBox() {
this.SetStyle(ControlStyles.UserPaint, true);
}
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
// Paint something
//...
}
}
Copy and paste this into a new class, compile and drop the new control from the top of the toolbox. Try it out, you'll be quite disappointed. What you see is the result of close to 20 years of appcompat hacks on a control that dates back to Windows version 2. One of the grave crimes that EDITBOX commits is painting itself without generating a WM_PAINT message. That was important way back when Windows had to run on a 386SUX, keeping it compatible with old programs prevented Microsoft from fixing its behavior.
No happy answers here, you'll have to give up on the idea of customizing the border. What you can do is give the control a distinct BackColor when it has the focus. For example:
class MyTextBox : TextBox {
Color mBackColor;
protected override void OnEnter(EventArgs e) {
mBackColor = base.BackColor;
base.BackColor = Color.AliceBlue;
base.OnEnter(e);
}
protected override void OnLeave(EventArgs e) {
base.BackColor = mBackColor;
base.OnLeave(e);
}
}
You can inherit from base control classes and add your own drawing logic. That won't be very expensive performance-wise, but, if your app is of significant size, you'll have to replace each occurence of standard TextBox with your own implementation.

Categories