简体   繁体   English

从同一文本框中读取不同的int

[英]Reading different int from the same textbox

I´m little new to C# and i got some questions here: 我对C#并不陌生,我在这里遇到了一些问题:

I want to send my age in hex codes by serial port to a device that my pc is attached with. 我想通过串行端口将年龄以十六进制代码发送到我的PC所连接的设备。 I have those codes but i have to read from textBox the age inputed. 我有那些代码,但是我必须从textBox中读取输入的年龄。

For example: at textBox I enter my age (24) and each number has a hex code. 例如:在textBox上输入年龄(24),每个数字都有一个十六进制代码。 So how do i read from textBox each number? 那么我如何从textBox中读取每个数字? I think that is what i have to do, I read number 2, send hex code, then read the second number and send hex code. 我认为这就是我要做的,我读了2号,发送了十六进制代码,然后读了第二个数字,然后发送了十六进制代码。 Have I been clear? 我清楚了吗?

EDIT: Just showing u guys my code after i got awnsered. 编辑:刚给我遮篷后,只向大家展示我的代码。 Thanks all :) 谢谢大家:)

 private void btnConfirmaIdade_Click(object sender, EventArgs e)
    {
        string allValue = mtxbIdade.Text;

        foreach (char c in allValue)
        {
            MandaIndadeSerial(c);
        }

    }

    public void MandaIndadeSerial(char c)
    {
        switch (c)
        {
            case '1':
                EnviarComando("0232363b3bde03");// send hexa code to device by serial
                    break;
foreach(char c in TextBox.Text)
{
  // TODO: send current number. Cast to string if needed: (string)c 
}

This code iterates through all numbers/characters in TextBox, left to right, and allows you to process/send them separately. 此代码从左到右遍历TextBox中的所有数字/字符,并允许您分别处理/发送它们。

string numbers = new string[2];
numbers[0] = textbox.Text.Substring(0,1);  //first character in textbox
numbers[1] = textbox.Text.Substring(1,1);  //second character

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM