c# WinForms can you get the NumericUpDown text area - c#

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/

Related

WinForms : Dynamically add / remove AnchorStyles to / of control

I am currently working on a Editor, which lets the user design his own WinForm overlay, at least to a certain point.
Therefore I want the user to decide, which AnchorStyles the current selected Control should have. I would like it to be handled by checkboxes. Here's how I had it in mind:
As you can see, the user has currently selected a dynamically added Panel, called Grid. Handled by the CheckBoxes to the right, he should now be able to set the selected Controls AnchorStyles.
Here's my problem: I can't seem to find a usable solution, to dynamically add a specific AnchorStyle to the already existing ones, or the opposite, remove the AnchorStyle, but keep the other ones as they are.
I was trying to get it to work with...
SelectedControl.Anchor += AnchorStyles.Top;
which doesen't work at all. So i thought of this...
SelectedControl.Anchor = SelectedControl.Anchor | AnchorStyles.Top
which I imagine could work, but I haven't even tested it, since I wouldn't know how to remove ones unchecked AnchorStyle.
Building a gigantic if(){} else if(){}... doesen't seem to be a good Idea :)
I'm open for any ideas / solutions.
Thanks in advance!
Assuming you have four check box controls named top, bottom, left and right, you can handle CheckedChange event of them using a single method and set the anchor of the desired control based on the checked value of the controls. For example:
private void checkBox_CheckedChanged(object sender, EventArgs e)
{
var values = new[] { top.Checked, bottom.Checked, left.Checked, right.Checked };
byte[] data = new byte[1];
new BitArray(values).CopyTo(data, 0);
selectedControl.Anchor = (AnchorStyles)data[0];
}
Note: AnchorStyles is a flag enum having top=1, bottom=2, left=4 and right=8. Using above code I've mixed those flags to create the AnchorStyles and have assigned to the Anchor property of control.

control to show steps to do something in winforms

i want to show steps on how to cook something in winform c# .net as steps. Something like a set of text area would be nice but:
-> list box considers the whole string of one step as one item so user needs to scroll horizontally to view the whole step.
-> datagridview is also not suitable as i want the text to word wrapped.
i also want the user to be able to edit the step.
any suggestions of custom control would be nice.
Maybe a wizard like app would be suitable for you. AFAIK there's no native wizard control in C# but you could implement one using tabs or using one of many in the web.
A multi line text box will do the job great. just take a simple text box and do the following to it, and it will turn to a text area:
TextBox listBoxNewInput = new TextBox();
//Initialize label's property
listBoxNewInput.Multiline = true;
// Add vertical scroll bars to the TextBox control.
listBoxNewInput.ScrollBars = ScrollBars.Vertical;
// Allow the RETURN key in the TextBox control.
listBoxNewInput.AcceptsReturn = true;
// Allow the TAB key to be entered in the TextBox control.
listBoxNewInput.AcceptsTab = true;
// Set WordWrap to true to allow text to wrap to the next line.
listBoxNewInput.WordWrap = true;
listBoxNewInput.Width = 315;
listBoxNewInput.Height = 150;
listBoxNewInput.DoubleClick += new EventHandler(listBoxNewInput_DoubleClick);
flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
flowLayoutPanel1.Controls.Add(labelInput);
flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
flowLayoutPanel1.Controls.Add(list
BoxNewInput);

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.

user control with a textbox

I have a user control with a textbox in a win forms application.
I would like to change the property of that textbox using the properties window of visual studio .
I am using that control in various forms of same project ,is it possible?
I have set the modifier property of text box as public and set following property in the user control:
public TextBox mytextBox
{
get { return textBox1; }
set { textBox1 = value; }
}
Thanks in Advance.
What is the intent of doing this? Are you trying to have "one TextBox control shared by multiple forms" (that is not really practical). However you can set up your forms in such a way as to have all forms update in response to a single change.
[TypeConverter(typeof(ExpandableObjectConverter))]
public TextBox mytextBox
{
get { return textBox1; }
set { textBox1 = value; }
}
Notes:
From the perspective of the PropertyGrid, the setter has no benefit in this case; the properties of the already-assigned TextBox are being modified in-place.
Remember to create an initial value, and to add the TextBox to the UserControl's control-collection. If you used the VS designer to create the TextBox, this should have been done already. If you find that the VS designer method InitializeComponents() is undoing your changes, create and add the control yourself.
You may have to rebuild the project and/or reopen the Forms designer for the change to be visible.
Off-topic: Use Pascal-case for properties, and the auto-implemented get;set; pattern for readability, if at all possible.

Arrange controls in panel in ASP.NET

I want to arrange the controls in a Panel control. For example, I have four labels and four textboxs, I want to put them in a panel like a table without using the VS designer, only use the code. Does anyone do it before?
Best Regards,
C#, and use styles to control layout.
Panel pnl = new Panel();
Label lbl1 = new Label();
lbl1.Text = "1";
pnl.Controls.Add(lbl1);
TextBox tb1 = new TextBox();
pnl.Controls.Add(tb1);
Page.Controls.Add(pnl);
label
{
display: inline;
}
You could essential do the same thing Visual Studio does on the back end. Create a new control and set the properties, such as: size, name, text, and location.
I think that you can simply create a custom control that contains a label and a text box. Let's call this control LabeledTextBox. And then on your code simply add 4 instances of LabeledTextBox one after the other. This should provide the look and feel you want.

Categories