Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I want to create a TextBox in windows form application in visual studio that contains only specific numbers. for example i want one textbox to be able to contain only numbers between 0 and 1. If its any other value I want a message to appear that says wrong number. The app is in written in C#.
I have seen this example
C# Numeric Only TextBox Control
but it is for numeric and i want specific numeric.
Edit:My mistake i wasn't clear enough. i tried the NumericUpDown but it doesn't work for all examples. In the second box i want to have values from 20 to 340 but only the .5 floating points between (20,20.5,21 etc) and if i use NumericUpDown, if the user inserts 22.6 the cell accepts it and i don't want that. That's why i ask if it's possible to control the value inserted with a Textbox or a MaskedTextBox and if it's eligible then for the user to be able to enter it the box. Hope i am clear enough now.
You should use a NumericUpDown.
You can use it like this;
NumericUpDown control = new NumericUpDown();
control.Minimum = 0;
control.Maximum = 1;
control.DecimalPlaces = 2; // or something you want.
control.Increment = .01; // step is .01
create user control inheriting TextBox and add an event for KeyPress as follows
public class MyTextBox:TextBox
{
List<int> numberstobeallowed = Enumerable.Range(20, 340 - 20).ToList();
public MyTextBox()
{
this.Leave += new EventHandler(MyTextBox_Leave);
}
void MyTextBox_Leave(object sender, EventArgs e)
{
if (numberstobeallowed.Contains(Convert.ToInt32(this.Text)))
this.Text = string.Empty;
}
}
hope this helps
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I have a C# Application where I am using Serial communication with a Microcontroller to Display data on the application. I have used a text box to display the data :
public void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
try
{
DataRec = serialPort1.ReadExisting();
int.TryParse(DataRec, out myTotal);
this.BeginInvoke(new EventHandler(DisplayText));
}
catch(NullReferenceException)
{
//catching the exception
}
}
public void DisplayText(object sender, EventArgs e)
{
textBox2.Text = myTotal.ToString();
}
Instead of TextBoxes I have also tried using labels. I get the same result and I didnt see any change in performance. I am using labels because I didnt want the user to think they can edit the values in the textboxes.
I have tried searching for the advantages of one over the other. So far the ones I have seen is :
Textboxes need to be set as readonly whereas in labels you dont need to do that.
Even when I set the textboxes as read only the Cursor is still visible whereas in Label it isn't.
What are some of the pros and cons in terms of performance, while using either a Label or Textbox?
Is it ok if I use labels ?
There are a few pro's and con's to both.
Label
Pro's:
Text is not copy able
Cursor does not change
Sets size based on text (if autosize is on, I think its on by default)
Option to align text to the right (autosize off)
Con's:
Text is not selectable/copy able
Text might outgrow form/parent with autosize
TextBox
Pro's:
Text is copy able
Fixed size (also a con)
Con's:
Does not autosize
Height not adjustable (does not apply to rich textbox/multiline = true)
My opinion:
In my opinion you should use a label if the user shouldn't be able to copy the data. The exception to this is if you have just 1 un-editable value and all others are textbox's, then you should just make a readonly textbox.
You have already answered your own question. It seems clear to me that a label is more appropriate in this case. You could argue that later that you may need the extra functionality that the text box provides but you should consider the YAGNI principle.
As to pros and cons, the user will probably believe they are able to edit the value in the text box, they wont make that mistake using a label.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have an Application build in WPF. It have a Textbox that will always accept only 10 digits and is always ready for Scanning ID Barcode Contains 10 Digits or Entering Number with Keyboard.
Now some Customers are entering just 2 digits and leaving system as it is. Let us say he write 12 in TextBox and left it. When new Customer is coming he is Scanning his Id without noticing that there is something already written in the TextBox. So New Number is coming like this 1224444444 and two numbers are missing that is 34.
How can I clear Textbox before Scanning or Before Writing?
some example code of exactly how you are attempting this would be useful.
A WPF textbox can be cleared by either calling the .Clear method, or simply by setting the "Text" property of the Textbox to string.empty.
With regards to most barcode scanners i've seen and used (usually KB emulation), you can usually set them up to get a prefix and suffix on the data so that you can detect scan input over keyboard input. You can then detect a scan and clear the textbox prior to entering the new information
react at the new scanevent from the barcodescanner and clear the textbox.text with string.empty
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have created a text box which can accept only one character. When the user types anything into the box, I want it converted into upper case.
Set TextBox.CharacterCasing to CharacterCasing.Upper.
textBox1.CharacterCasing = CharacterCasing.Upper;
Right-click your textbox, and then click Properties.
In the Properties window, locate the CharacterCasing property, and then click to select Upper from the list.
(Source: http://support.microsoft.com/kb/818363)
Use KeyDown event and check the input key. Then use String.ToUpper(). For example:
private void YourTextBox_KeyDown(object sender, KeyEventArgs e)
{
YourTextBox.Text = YourTextBox.Text.ToUpper();
}
string lower = "converted from lowercase";
Console.WriteLine(lower.ToUpper());
Take a look at this: How to convert strings to lower, upper, or title (proper) case by using Visual C#
You could either change the textbox's casing to upper case like this:
TextBox.CharacterCasing = CharacterCasing.Upper;
Or you could create an event which is executed when the textbox's text has changed.
TextBox.Text = TextBox.Text.ToString().ToUpper();
And so whenever someone types something inside of the textbox it will be changed to upper case, but I do not think that this is a good idea. I recommend using my first advice.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
In my project, I need to get the listview containing the data from the back end, If I place the listview in the form and upon clicking a button I can load data, but the requirement is initially the listview should be hidden or not present there, only on clicking the button the listview should be viewed with data. Thank you.
Set the List View's property Visible = Falsein the properity window.
Within the button click event handler set it to true;
listView1.Visible = true;
You use the Visible Property. At the form load, you set it to false and then in the button_click
private void button1_Click(object sender, EventArgs e)
{
listView1.Visible = true;
}
I think You should use listView1.Hide(); initially in Form constructor
And on button click You should use listView1.show()
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Apologizes if this topic isnt in the right section, or if this has been already answered in another topic.
I am working with tabs, and have come across a business requirement where a need has arised to create tabs during runtime.
The request is to create a ADD Tab, REMOVE Tab buttons and to x amount of similar controls to be added at run time.
What would be the ideal approach to this - since there might be validations we need to add as well, plus eventually the data pushed to a DB, etc
Is there a sample project out there on codeplex/code gallery that I could take a look at ?
I am working with a windows forms application on VB 2010. C# examples work for me as well. I need a code sample that might guide me, open eneded answers welcomed - it will only help me learn.
Thanks.
AS a starting point. To Add Tabs and controls to Tab use something like.
TabPage tp = new TabPage("New Tabl Page");
TextBox t = new TextBox();
t.Left = 10;
t.Top = 10;
t.Visible = true;
t.Width = 100;
tp.Controls.Add(t);
tabControl1.TabPages.Add(tp);
You might consider creating a template Tab Control so that this could be used over and over again and then think about how you want to bind these controls.