Currency symbol on wrong side - c#

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 = "$";

Related

TextBox text "Replace" function does not work well

I have already looked at similar questions here but it did not help.
I am using windows forms. I have button1 and textbox1.
I am trying to replace (or delete) the selected text in textBox1 and enter new letter (letter A) in place of it.
The code works well with random mixed numbers and letters
for example:
385F1 select 8 and then result = 3A5F1 (8 replaced by A)
H74S31B select 4S and then the result is = H7A31B
KQ5689 select Q5689 and then the result is KA
So it works well, but when I select a number or a letter from a string which consists of same numbers or letters then it does not work, for example:
666777222333 select any 7 then the result = 666AAA222333 (not
working)
9992244GG select any 9 then the result = AAA2244GG (not working)
QQQHHHUUU select any Q then the result = AAAHHHUUU (not working)
QQQHHHUUU select any QH then the result = QQAHHUUU(it works when
different letters selected)
4433366 select 333 then the result = 44A66 (it works when all same
numbers is selected)
Hope I explained it well. I don't know what causes this behavior. please help. Thank you
public partial class Form1 : Form
{
int TxTindex;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ActiveControl = textBox1;
textBox1.Focus();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Focus();
if (textBox1.SelectedText.Length > 0) // to check if any text selected
{
TxTindex = textBox1.SelectionStart; // save the caret position
textBox1.Text = textBox1.Text.Replace(textBox1.Text.Substring(textBox1.SelectionStart, textBox1.SelectionLength),"A");
textBox1.SelectionStart = TxTindex + 1; // place the caret after the inserted string
}
else
{
return;
}
}
}
Your problem is here :
textBox1.Text = textBox1.Text.Replace(textBox1.Text.Substring(textBox1.SelectionStart, textBox1.SelectionLength),"A");
The Substring function returns string. In your example (666777222333 select any 7 then the result = 666AAA222333 (not working)), it returns "7". But Text.Replace will replace all occurrences of 7. That is not what you want. What you can do is, instead of using string.Replace function, use string.Remove and string.Insert
textBox1.Text = textBox1.Text.Remove(textBox1.SelectionStart, textBox1.SelectionLength).Insert(textBox1.SelectionStart, "A");
But this might not be very efficient for large strings. A StringBuilder would be better.
The problem is caused by the usage of string.Replace function.
What you are trying to accomplish can be done simply by
if (textBox1.SelectionLength > 0)
textBox1.SelectedText = "A";
There is no need to save/set selection start.

To enter in decimal format in textbox

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

populating empty field of a string with 0(zero) in c# winform?

I have a winform where I have a textbox that will take upto 14 numbers. Now if user enters less that 14 i have to populate the rest of the fields with 0s. Eg. if a user 10 numbers the i have to include 4 more 0's to to make it 14.
Change the MaxLength property of the text box to 14. After you get the Text property, use, the PadLeft, or PadRight methods on the String class.
Example
void textBox_LostFocus(object sender, EventArgs e)
{
var text = this.textBox.Text;
text = text.PadLeft(14, '0');
this.textBox.Text = text;
}
Results
var value = "abcd";
var leftPadded = value.PadLeft(14, '0'); // <- "0000000000abcd"
var rightPadded = value.PadRight(14, '0'); // <- "abcd0000000000"
You might also want to consider using the MaskedTextBox class.
textbox.TextChanged += new EventHandler( textbox_TextChanged );
private textbox_TextChanged(Object sender, EventArgs e) {
textbox.Text = textbox.Text.PadLeft(14, '0');
}
If you use databinding, and if you just want to add the numbers in the display of the textbox, and not in the underlying datasource, you can use standard .net formatting when you create your own textbox derivative, as described here. Might come in handy.

Select Text in a multiline textbox, move it to another multiline textbox on a button click in C#

I have a problem that I haven't come across yet that I hope some of you may help me with. I am trying to select a single line, either the fist, second, or last line in a multiline textbox and move it to another multiline textbox with a button click in C#. I am unsure how to select just a single line at a time, then add it to the other multiline textbox. If anyone has some insight, that would be great! Thank you!
Brent
Well, assuming that you are defining a "line" as a complete string of characters separated by other similar strings with a newline character, and not simply as the string of characters visible in a single horizontal plane in a text field with word wrap property set to true.....
public void Button1_Click(object sender, ClickEventArgs e)
{
//get the values of both boxes
string value1 = TextBox1.Text.Trim();
string value2 = TextBox2.Text.Trim();
//split the value from the source box on its new line characters
string[] parts = value1.split(Environment.NewLine);
string last_line = parts[parts.length -1];
//add the last row from the source box to the destination box
value2 += (Environment.NewLine + last_line);
//set the last_line in the source to an empty string
parts[parts.Length -1] = String.Empty;
//put the new values back in their text boxes
TextBox1.Text = String.Join(Environment.NewLine, parts).Trim();
TextBox2.Text = value2;
}
If you are dealing with visible line and word wrapped that is a whole 'nother ball game and the answer is dependant on if you are talking ASP, or Win App. Also, this was written off the cuff, so you may need to tweak a character or two to get it to compile. No Warranties, LOL.
Something like this will work:
public void Button1_Click(object sender, ClickEventArgs e)
{
string text = TextBox1.Text;
// spliting text on the basis on newline.
string[] myArray = text.Split(new char[] { '\n' });
foreach (string s in myArray)
{
//Line by line copy
TextBox2.Text += s;
}
}
Try something like this:
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Lines.Length > 0)
{
textBox2.Text += textBox1.Lines[textBox1.GetLineFromCharIndex(textBox1.SelectionStart)];
}
}
What it is doing is using the GetLineFromCharIndex with the SelectionStart caret location as the char Index to pull the Line out of the TextBox.Lines array

Error on int.parse

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.

Categories