存储文本框值时未获得正确的输出 [英] not getting correct output when storing textbox values
问题描述
对于在textBox1中输入的任何数值,我始终在消息框中得到的输出为"0".你能解决我的密码吗?谢谢
公共 部分 class MainWindow:窗口
{
公共 双重 []平均值= 新 double [ 5 ];
}
私有 无效 button1_Click(对象发件人,RoutedEventArgs e)
{
average [ 0 ] = Convert.ToDouble(textBox1.Text);
MessageBox.Show(Convert.ToString(average [ 0 ])));
}
PS:我也尝试过this.textBox1.Text,但是没有用.您能否也请您解释一下此"关键字的确切含义?谢谢
感谢大家的答复,但是我的目的不是在文本框中显示值,而是将其存储在数组中.我计划每次单击按钮时在double数组中存储一个新值.我希望代码是这样的.
private button_click() { int i=0; code to store the current value in the textbox into a double array[i] and probably display it on a messagebox .... i++; textbox1.clear(); }
这"的含义是: ^ ]
你最好
double mycheckDouble = 0 ; 如果(双 .TryParse(textBox1.Text, out mycheckDouble) MessageBox.Show(字符串 .Format(" ,mycheckDouble)); 其他 MessageBox.Show(" Convert.ToDouble() [TextBox并使用了错误的一个,或者您没有更改TextBox
中的值
this
是对类的当前实例的引用.请参阅文档此(C#参考) [ ^ ]
公共 部分 类 MainWindow:窗口 { 公共 双重 []平均值= 新 double [ 5 ]; 公共 静态 int i = 0 ; 受保护的 无效 AddToArray() { 如果(i!= 5 &&i; >< ; 5 ) { average [i] = Convert.TODouble(txtbox.text); i ++; } } } // ,然后将其调用到您的按钮上单击,您可以编写代码以停止单击按钮(如果它是5次)
I keep getting the output in messagebox as ''0'' for any numerical value I give in the textBox1. can you please fix my code ? Thanks
public partial class MainWindow : Window
{
public double[] average = new double[5];
}
private void button1_Click(object sender, RoutedEventArgs e)
{
average[0] = Convert.ToDouble(textBox1.Text);
MessageBox.Show(Convert.ToString(average[0]));
}
PS: I have also tried this.textBox1.Text, but it did not work. Could you please also explain me what ''this'' keyword exactly does ? Thanks
Thanks everyone for the reply, but My objective here is not to display the value in the textbox, but to store it in an array. I am planning to store a new value in the double array everytime i click the button. I want the code to be something like this.
private button_click() { int i=0; code to store the current value in the textbox into a double array[i] and probably display it on a messagebox .... i++; textbox1.clear(); }
what ''this'' means: http://msdn.microsoft.com/en-us/library/dk1507sz(v=vs.80).aspx[^]
you better do
double mycheckDouble = 0; if (double.TryParse(textBox1.Text, out mycheckDouble) MessageBox.Show(string.Format("value found {0}", mycheckDouble)); else MessageBox.Show("Failed to convert value to a double");
Convert.ToDouble()[^] returns 0 when the input is null, an empty string or "0". So either you have more than oneTextBox
and using the wrong one or you do not change the value in yourTextBox
thethis
is the reference to the current instance of your class. refer to the documentation this (C# Reference)[^]
public partial class MainWindow : Window { public double[] average = new double[5]; public static int i = 0; protected void AddToArray() { if(i != 5 && i < 5) { average[i] = Convert.TODouble(txtbox.text); i++; } } } // then call that mwthod to your button click you can write your code to stop clicking the button if it is at 5 times
这篇关于存储文本框值时未获得正确的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!