c# - 如何将复选框、日期时间选择器和文本框值转换为整数
c# - how to convert checkbox,datetime picker and textbox value to integer
感谢那些回复的人,您的解决方案很正确!
@留言板..抱歉问题过多而不具体。
我一直在试验我得到的解决方案,为了根据提供的解决方案保持一致,我有以下代码:
var mystr = String.Format("{0},{1:yyyy,M,d,H,m,s},{2:yyyy,MM,dd,HH,mm,ss},{3},{4},{5},{6},{7},{8},{9},{10}",
this.textBox3.Text, //0
this.dateTimePicker1.Value,//1
this.dateTimePicker2.Value,//2
Convert.ToInt32(this.checkBox1.Checked),//3
Convert.ToInt32(this.checkBox2.Checked),//4
Convert.ToInt32(this.checkBox3.Checked),//5
Convert.ToInt32(this.checkBox4.Checked),//6
Convert.ToInt32(this.checkBox5.Checked),//7
Convert.ToInt32(this.checkBox6.Checked),//8
Convert.ToInt32(this.checkBox7.Checked),//9
Convert.ToInt32(this.checkBox8.Checked));//10
textBox1.Text = mystr;
MessageBox.Show("The value entered is: " + mystr , " Alarm DATA", MessageBoxButtons.OK);
单击“确定”按钮后,我得到一串值,即:
88,2015,5,17,22,6,53,2015,05,17,22,06,53,0,0,0,0,0,0,0,0
我将通过串口将这些发送到 Arduino,问题是它们目前的格式。
arduino 期望它们采用与从串行终端发送的格式(数据类型)相同的格式(数据类型)window,并且没有添加换行符。
(当我输入这个 88,2015,5,17,22,6,53,2015,05,17,22,06,53,0,0,0,0,0,0,0,0进入串行终端 window.
我的问题是如何将变量 mystr 的值 set/change 设置为 arduino 需要的值?
我目前正在 Arduino 上使用 Serial.parseint() 来接收以逗号分隔的值并将它们加载到变量中。
非常感谢任何帮助。
D
For reference ..Here's my original question:
I have created a Windows form which currently has 9 checkboxes (I
don't know if this was the right choice.) as well as a button and 2
textbox.
At the moment I type dates into the textboxes in the form of
YYYY,MM,DD,hh,mm,ss and then I select checkbox values which when
selected should return a 1 and if un-selected a 0.
I realize this may seem like a very basic question but I can't seem to
find the answers I'm looking for.
I would like the following to happen when I click the button :
A single string get's generated/created. (Eventually I will send this
out via serial. (not sure if this is the variable type I should be
using and I would appreciate and example for both if at all possible).
The string/int/message comprises of the two dates ( or as unix
timestamps or an option for either) and each checkbox value (either a
0 or a 1) << this is where I battling to find info on as well as the
datetime picker)
So the actual string would look something like this :
2015,4,30,3,0,5,2015,4,30,3,0,10,0,1,0,0 (where the format is
date1,date2,checkbox1,checkbox2,checkbox3,checkbox4)
I can currently use the checkbox.text fields but I would prefer to
just get the value from each as a zero or 1 to build my
string/int/message.
I am using C# VS express.
首先,我建议使用 DateTimePicker
输入日期和时间。您可以让它显示您喜欢的任何格式,它会为您处理验证,确保用户永远不会输入无效格式或无效值。然后你可以这样做:
var str = String.Format("{0:yyyy,M,d,H,m,s},{1:yyyy,M,d,H,m,s},{2},{3},{4}",
this.dateTimePicker1.Value,
this.dateTimePicker2.Value,
Convert.ToInt32(this.checkBox1.Checked),
Convert.ToInt32(this.checkBox2.Checked),
Convert.ToInt32(this.checkBox3.Checked));
如果你想要更通用的东西,试试这个:
var dates = this.Controls.Cast<Control>()
.OfType<DateTimePicker>()
.Select(dtp => dtp.Value.ToString("yyyy,M,d,H,m,s"));
var bools = this.Controls.Cast<Control>()
.OfType<CheckBox>()
.Select(cb => Convert.ToInt32(cb.Checked).ToString());
var str = String.Join(",", dates.Concat(bools).ToArray());
这将适用于任意数量的任意名称的控件。需要注意的是,每组控件的 z 顺序必须与您希望其输出出现在最终 String
中的顺序相匹配。您可以通过在文档大纲 window 中拖放来调整 z 顺序,它可以从视图 > 其他 Windows 菜单中打开。
string s = null;
if (checkbox.checked)
s = "1";
else
s = "0";
感谢那些回复的人,您的解决方案很正确! @留言板..抱歉问题过多而不具体。
我一直在试验我得到的解决方案,为了根据提供的解决方案保持一致,我有以下代码:
var mystr = String.Format("{0},{1:yyyy,M,d,H,m,s},{2:yyyy,MM,dd,HH,mm,ss},{3},{4},{5},{6},{7},{8},{9},{10}",
this.textBox3.Text, //0
this.dateTimePicker1.Value,//1
this.dateTimePicker2.Value,//2
Convert.ToInt32(this.checkBox1.Checked),//3
Convert.ToInt32(this.checkBox2.Checked),//4
Convert.ToInt32(this.checkBox3.Checked),//5
Convert.ToInt32(this.checkBox4.Checked),//6
Convert.ToInt32(this.checkBox5.Checked),//7
Convert.ToInt32(this.checkBox6.Checked),//8
Convert.ToInt32(this.checkBox7.Checked),//9
Convert.ToInt32(this.checkBox8.Checked));//10
textBox1.Text = mystr;
MessageBox.Show("The value entered is: " + mystr , " Alarm DATA", MessageBoxButtons.OK);
单击“确定”按钮后,我得到一串值,即: 88,2015,5,17,22,6,53,2015,05,17,22,06,53,0,0,0,0,0,0,0,0
我将通过串口将这些发送到 Arduino,问题是它们目前的格式。
arduino 期望它们采用与从串行终端发送的格式(数据类型)相同的格式(数据类型)window,并且没有添加换行符。 (当我输入这个 88,2015,5,17,22,6,53,2015,05,17,22,06,53,0,0,0,0,0,0,0,0进入串行终端 window.
我的问题是如何将变量 mystr 的值 set/change 设置为 arduino 需要的值?
我目前正在 Arduino 上使用 Serial.parseint() 来接收以逗号分隔的值并将它们加载到变量中。
非常感谢任何帮助。
D
For reference ..Here's my original question:
I have created a Windows form which currently has 9 checkboxes (I don't know if this was the right choice.) as well as a button and 2 textbox.
At the moment I type dates into the textboxes in the form of YYYY,MM,DD,hh,mm,ss and then I select checkbox values which when selected should return a 1 and if un-selected a 0.
I realize this may seem like a very basic question but I can't seem to find the answers I'm looking for.
I would like the following to happen when I click the button :
A single string get's generated/created. (Eventually I will send this out via serial. (not sure if this is the variable type I should be using and I would appreciate and example for both if at all possible).
The string/int/message comprises of the two dates ( or as unix timestamps or an option for either) and each checkbox value (either a 0 or a 1) << this is where I battling to find info on as well as the datetime picker)
So the actual string would look something like this : 2015,4,30,3,0,5,2015,4,30,3,0,10,0,1,0,0 (where the format is date1,date2,checkbox1,checkbox2,checkbox3,checkbox4)
I can currently use the checkbox.text fields but I would prefer to just get the value from each as a zero or 1 to build my string/int/message.
I am using C# VS express.
首先,我建议使用 DateTimePicker
输入日期和时间。您可以让它显示您喜欢的任何格式,它会为您处理验证,确保用户永远不会输入无效格式或无效值。然后你可以这样做:
var str = String.Format("{0:yyyy,M,d,H,m,s},{1:yyyy,M,d,H,m,s},{2},{3},{4}",
this.dateTimePicker1.Value,
this.dateTimePicker2.Value,
Convert.ToInt32(this.checkBox1.Checked),
Convert.ToInt32(this.checkBox2.Checked),
Convert.ToInt32(this.checkBox3.Checked));
如果你想要更通用的东西,试试这个:
var dates = this.Controls.Cast<Control>()
.OfType<DateTimePicker>()
.Select(dtp => dtp.Value.ToString("yyyy,M,d,H,m,s"));
var bools = this.Controls.Cast<Control>()
.OfType<CheckBox>()
.Select(cb => Convert.ToInt32(cb.Checked).ToString());
var str = String.Join(",", dates.Concat(bools).ToArray());
这将适用于任意数量的任意名称的控件。需要注意的是,每组控件的 z 顺序必须与您希望其输出出现在最终 String
中的顺序相匹配。您可以通过在文档大纲 window 中拖放来调整 z 顺序,它可以从视图 > 其他 Windows 菜单中打开。
string s = null;
if (checkbox.checked)
s = "1";
else
s = "0";