Below code which I am using right now does not give what I require.
textBox1.Text = textBox1.Text + enteredvalue;
I would like to achieve entering decimal value in text box without having to enter ".".
If 3 is entered, the output in text box should be 00.03, and then if 5 is entered the output should be 00.35 and so on.
How do I achieve it?
EDIT: I achieved it by creating a method and calling it everytime i press the input number.
public void dropcashvalue(string inputdigit)
{
if (txtDropCash.Text == "00.00")
{
txtDropCash.Text = "";
this.dropstring = "";
}
this.dropstring = this.dropstring + inputdigit;
txtDropCash.Text = (Convert.ToDouble(this.dropstring) / 100).ToString("F2");
}
My textbox and inputnumber design looks like this.
You need to maintain an additional variable for your compund value.
double enteredValue = 0.0;
Whenever a new digit comes in you add it to your value:
enteredValue = enterValue*10 + inputDigit;
Inthe Textbox you show a formatted version of your value:
textBox.Text = (enteredValue/100).ToString("F2");
Related
A price is being displayed in a RichTextBox. It takes a double value representing the price and displays it as a string.
double priceDisplayed = 0.00;
richTextBox_itemPrice.Text = priceDisplayed.ToString("C", new CultureInfo("en-AU"));
The above code results in the price being displayed with the currency on the RHS:
0.00$
Why is this? Checking similar examples, it seems the code above should show the currency symbol on the LHS.
This is really strange, as mentioned in the comment, there must be some culture-clashes on your machine.
In the meantime, you could try something like this on the event TextChanged:
private void richTextBox_itemPrice_TextChanged(object sender, EventArgs e)
{
string text = richTextBox_itemPrice.Text;
if (richTextBox_itemPrice.Text.Contains("$"))
{
text = text.Replace("$","");
}
richTextBox_itemPrice.Text = "$" + text;
}
And initialize the field to have richTextBox_itemPrice.Text = "$";
I have a TextBoxnamed PercentageText. I used TextChanged event to Append "%" to text typed inside the TextBox. The code inside the TextChanged event is given below
if (skipTextChange)
skipTextChange = false;
else
{
skipTextChange = true;
if (PercentageText.Text =="")
{
PercentageText.Text = " ";
}
if (PercentageText.TextLength == 1)
{
if (PercentageText.Text != "%")
{
PercentageText.Text =""+ PercentageText.Text.Trim() + "%";
}
}
}
and initiallized SkipTextChange=false; out side the TextChanged Event Block. My Problem is When I Type Anything the first character goes all the way to the end of the text, for an example, if I type 152 it Shows 521 and When I cleared the TextBox using keyboard(Back Space key), and Type again it works Perfactly.
Instead of going for all this troubles I suggest you to simply add a label to the right of the textbox and put a % as the label's text.
However, if you really want to go for the TextChanged path then you need to test if your input ends with the % char and add it only if not. Also you need to set the position where the next char should be typed.
if (skipTextChange)
skipTextChange = false;
else
{
skipTextChange = true;
if (PercentageText.Text == "")
{
PercentageText.Text = " ";
}
if (!PercentageText.Text.EndsWith("%"))
{
PercentageText.Text = "" + PercentageText.Text.Trim() + "%";
PercentageText.SelectionStart = PercentageText.TextLength - 1;
}
}
Consider to test extensively with this approach. Copy/Paste, Delete and BackSpace behavior should be verified and insertion of multiple spaces or with the case of a % char typed directly by the user. Of course, if this textbox is supposed to contain only numbers a more complex verification code is required. If this is the context then I suggest to use NUmericUpDown control and the label trick to its right.
I have a multiline text box called txtOutput that receives messages from the serial port. Each new message is a new line and represents a number from 1 to a maximum of 4 digits.
The method used to add the data in the multiline textbox is an append.
I have no problems with the above feature, it is working fine.
I would like to take the last message from the txtOutput and show it in textBox2 if the number is less than 1000, and in textbox3 if it is not. Then both text boxes would be updated.
I would appreciate if someone can give in an example especially in how to get the last message from the multiline textbox to a variable and how to update the textboxes if there is a new value.
You should save the last message (from the serial port) in a variable such as lastSerialMesssage. You can then convert this value to an integer and use a conditional statement to check if the value is smaller than 1000, if it is, set TextBox3 to the last serial message value, else set the value to TextBox2.
string lastSerialMessage = SerialPortMessage;
int lastMessageValue;
Int32.TryParse(lastSerialMessage, out lastMessageValue);
if (lastMessageValue < 1000)
{
TextBox3.Text = lastSerialMessage;
} else {
TextBox2.Text = lastSerialmessage;
}
http://msdn.microsoft.com/en-us/library/f02979c7.aspx
Thanks to all for the suggestions but as I mentioned in my comments, the suggested methods did not work because the content of the string was not accurate and I ended up receiving in the textBox 2 and 3 only part of the data and not always. I have solved the problem (thanks to other advices) using RegEx in this way:
if (txtOutput.Text.Length > 0)
{
MatchCollection mc = Regex.Matches(txtOutput.Text, #"(\+|-)?\d+");
if (mc.Count > 0)
{
long value = long.Parse(mc[mc.Count - 1].Value);
if (value < 1000)
{
textBox2.Text = value.ToString();
}
else
{
value = value - 1000;
textBox3.Text = value.ToString();
}
}
}
this is working fine and no piece of information are lost.Thanks again for your advices.
Hi I was wanting to display the number of characters entered into a textbox and I want it to update as I type how can I go about this?
Here is what I have:
int kk = textBox1.MaxLength;
int jj = //This is what I need to know.
string lol = jj.ToString() + "/" + kk.ToString();
label2.Text = lol;
How about
int jj = textBox1.Text.Length;
Or am I missing something?
The text of the text box will be a string, so it has a Length property, i.e.:
textBox1.Text.Length
TextBoxobject.Text.Length will give you the length of textbox value.
you can use the OnTextChanged event of the Textbox on the client side in Javascript and compute the increment from.
Input string was not in a correct format. At this line:
int total = 0;
total = int.Parse(TextBox2.Text) + int.Parse(TextBox4.Text) + int.Parse(TextBox6.Text) +
int.Parse(TextBox8.Text) + int.Parse(TextBox10.Text) + int.Parse(TextBox12.Text) +
int.Parse(TextBox14.Text) + int.Parse(TextBox16.Text);
Label1.Text = total.ToString();
I would like to pass the value to another page.
what does it means? T_T
Thanks in advance :)
protected void Button1_Click(object sender, EventArgs e)
{
Session["Month"] = DropDownList2.SelectedValue;
Session["expen1"] = TextBox1.Text;
Session["expen2"] = TextBox3.Text;
Session["expen3"] = TextBox5.Text;
Session["expen4"] = TextBox7.Text;
Session["expen5"] = TextBox9.Text;
Session["expen6"] = TextBox11.Text;
Session["expen7"] = TextBox13.Text;
Session["expen8"] = TextBox15.Text;
int totalvalue = 0;
totalvalue = int.Parse(TextBox2.Text) + int.Parse(TextBox4.Text) + int.Parse(TextBox6.Text) + int.Parse(TextBox8.Text) + int.Parse(TextBox10.Text) + int.Parse(TextBox12.Text) + int.Parse(TextBox14.Text) + int.Parse(TextBox16.Text);
Label1.Text = totalvalue.ToString();
Session["price1"] = TextBox2.Text;
Session["price2"] = TextBox4.Text;
Session["price3"] = TextBox6.Text;
Session["price4"] = TextBox8.Text;
Session["price5"] = TextBox10.Text;
Session["price6"] = TextBox12.Text;
Session["price7"] = TextBox14.Text;
Session["price8"] = TextBox16.Text;
Session["total"] = Label1.Text;
Server.Transfer("sum.aspx");
}
I want to store the result in sum.aspx.
If any of your TextBox values are null or are not a number, this will break. In order for this to work, all of the TextBox values will need to have a default value of 0 and you will have to restrict the input of the TextBox to numbers.
Instead of using textboxes and parsing text you trust to be numeric, use some sort of input mask or validation BEFORE parsing. Alternatively, use a different control like a Numeric up/down or numeric spinner.
You need to learn about how to handle exceptions, when to use try parse and when to use parse...
If any of the textboxes is empty, you will get an exception, since empty text cannot be parsed.
Use int.TryParse instead.
what does it means?
One of your TextBoxes contains a text which can't be parsed as an Integer.
Check Each textbox data should be numbers. if try enter string and validating with int.parse you will get this error.
It means that one of the textboxes values (TextBox#.Text) contains a value that cannot be "converted" to an integer.
What values are inside the textboxes? For example, if the textbox contains a non-numeric character it wont be able to convert, since the letter 'a' has no numeric value.
It means that one of the calls to int.Parse threw an exception because the text value was not a value that could be parsed into an Integer (e.g. the text was a non numeric value).
A better way to do this would be:
var textBoxesToParse = new [] { TextBox2, TextBox4, TextBox6, TextBox8, TextBox10, TextBox12, TextBox14, TextBox16 };
int total = 0;
foreach (var textBox in textBoxesToParse)
{
int textBoxValue;
if(int.TryParse(textBox.Text, out textBoxValue))
{
total += textBoxValue;
}
else
{
// The textbox had an invalid value, up to you what you need to do here.
}
}
As has been mentioned, the error is that one of your textboxes has either a blank or a non-numeric value.
You can use a RegularExpressionValidator so that the user is permitted to submit the form only when the values are numeric.
<asp:RegularExpressionValidator ID="RegularExpressionValidator7" runat="server"
ControlToValidate="txtLastName"Display="Dynamic" ErrorMessage="Numeric characters only"
ForeColor="Red" ValidationExpression="^[0-9]*$"
Additionally, you should also look to use tryParse or Int32.Parse(); the latter returns 0 if it is passed a null string.