[英]Change decimal place in textbox value with numericUpDown
通過此代碼,我可以使用numericUpDown
修改小數位。 如果initialize
myDecimal
變量,則此代碼有效。 但是我需要將decimal
位修改為在textbox
鍵入的值。
換句話說, myDecimal = tbxConvertito.Text
。 但是在這種情況下,代碼不起作用。
請檢查此頁面的屏幕截圖: 使用numericUpDown更改文本框中的小數位
public partial class Form1 : Form
{
public decimal myDecimal = 3755.25012345M;
public Form1()
{
InitializeComponent();
tbxConvertito.Text = myDecimal.ToString();
numericUpDown1_ValueChanged(this, EventArgs.Empty);
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
int decimalPlace = (int)numericUpDown1.Value;
string[] numbers = myDecimal.ToString().Split(new char[] { '.', ',' });
string tmp = string.Empty;
if (decimalPlace <= numbers[1].Length)
{
tmp = "," + numbers[1].Substring(0, decimalPlace);
if (tmp.EndsWith(","))
tmp = string.Empty;
}
else
tmp = "," + numbers[1];
tbxConvertito.Text = numbers[0] + tmp;
}
}
您可以從tbxConvertito
拆分屬性Text
。
同樣,當將在TextBox中更改文本時,您必須調用numericUpDown1_ValueChanged
以限制NumericUpDown中的設置。
public partial class Form1 : Form
{
public decimal myDecimal = 3755.25012345M;
public Form1()
{
InitializeComponent();
tbxConvertito.Text = myDecimal.ToString();
numericUpDown1_ValueChanged(this, EventArgs.Empty);
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
int decimalPlace = (int)numericUpDown1.Value;
string[] numbers = tbxConvertito.Text.Split(new char[] { '.', ',' });
string tmp = string.Empty;
if (numbers.Length != 1)
{
if (decimalPlace <= numbers[1].Length)
{
tmp = "," + numbers[1].Substring(0, decimalPlace);
if (tmp.EndsWith(","))
tmp = string.Empty;
}
else
tmp = "," + numbers[1];
}
tbxConvertito.Text = numbers[0] + tmp;
tbxConvertito.Select(tbxConvertito.Text.Length, 0);
}
private void tbxConvertito_TextChanged(object sender, EventArgs e)
{
numericUpDown1_ValueChanged(this, EventArgs.Empty);
decimal.TryParse(tbxConvertito.Text.Replace(',', '.'), out myDecimal);
}
}
不丟失數據:
public partial class Form1 : Form
{
public decimal myDecimal = 0;
public Form1()
{
InitializeComponent();
// init value
tbxConvertito.Text = myDecimal.ToString();
numericUpDown1_ValueChanged(this, EventArgs.Empty);
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
int decimalPlace = (int)numericUpDown1.Value;
string[] numbers = myDecimal.ToString().Split(new char[] { '.', ',' });
string tmp = string.Empty;
if (numbers.Length != 1)
{
if (decimalPlace <= numbers[1].Length)
{
tmp = "," + numbers[1].Substring(0, decimalPlace);
if (tmp.EndsWith(","))
tmp = string.Empty;
}
else
tmp = "," + numbers[1];
}
tbxConvertito.Text = numbers[0] + tmp;
tbxConvertito.Select(tbxConvertito.Text.Length, 0);
}
private void tbxConvertito_TextChanged(object sender, EventArgs e)
{
if (keyValue == 188) return;
if (keyPressed)
{
string stringValue = tbxConvertito.Text;
if ((stringValue.Contains(',') && stringValue.Split(new char[] { ',' })[1].Length <= (int)numericUpDown1.Value) || !stringValue.Contains(','))
decimal.TryParse(tbxConvertito.Text.Replace(',', '.'), out myDecimal);
keyPressed = false;
}
numericUpDown1_ValueChanged(this, EventArgs.Empty);
Console.WriteLine("Displayed value: {0}", tbxConvertito.Text);
Console.WriteLine("Actual value: {0}", myDecimal);
}
bool keyPressed = false;
int keyValue;
private void tbxConvertito_KeyDown(object sender, KeyEventArgs e)
{
keyValue = e.KeyValue;
keyPressed = true;
}
}
場景:
現在我們不會丟失任何數據。 僅當用戶在文本框中鍵入內容時,我們才會更新原始數據。
試試看
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
//var ix = tbxConvertito.Text.IndexOf(',');
decimal myDecimal = 0;
bool IsDecimal = decimal.TryParse(tbxConvertito.Text, out myDecimal);
if (IsDecimal)
{
decimal letDivide = myDecimal / 100;
int decimalPlace = (int)numericUpDown1.Value;
tbxConvertito.Text = letDivide.ToString().Replace(".", "");
var index = tbxConvertito.Text.Length - decimalPlace;
if (index > -1)
tbxConvertito.Text = tbxConvertito.Text.Insert(index, ",");
else
tbxConvertito.Text = tbxConvertito.Text.Insert(1, ",");
}
else
{
tbxConvertito.Text = tbxConvertito.Text.ToString().Replace(",", "");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.