[英]C# splitting string into comboboxes
I'm making a decimal to HEX converter.我正在将十进制转换为十六进制转换器。 The decimal number is typed into a textbox by the user and there are 8 comboboxes, one for each possible digit.
十进制数字由用户输入到一个文本框中,有 8 个组合框,每个可能的数字一个。 The method i have written takes the input string from the textbox and converts the decimal number to hexadecimal and returns it as a string.
我编写的方法从文本框中获取输入字符串并将十进制数转换为十六进制并将其作为字符串返回。
How can I put the returned string (hex number) into the correct comboboxes from "cboHex0" to "cboHex7", going from least significant bit to MSB.如何将返回的字符串(十六进制数)放入从“cboHex0”到“cboHex7”的正确组合框中,从最低有效位到 MSB。
Tried this but it does not work.试过这个,但它不起作用。 Appreciate all help as I am new to stackoverflow and C#.
感谢所有帮助,因为我是 stackoverflow 和 C# 的新手。
void decimalToHex(long dec)
{
if ((dec < 1) || (dec > 4294967295))
//if ((dec < 1) || (dec > 4294967295))
{
MessageBox.Show("ERROR\r\n" +
"Decimal number is not within the range for conversion");
return "0";
}
long hex = dec;
string hexStr = string.Empty;
while (dec > 0)
{
hex = dec % 16;
if (hex < 10)
hexStr = hexStr.Insert(0, Convert.ToChar(hex + 48).ToString());
else
hexStr = hexStr.Insert(0, Convert.ToChar(hex + 55).ToString());
dec /= 16;
}
//return hexStr;
//splitte streng til array.
string[] stringElements = hexStr.Split('');
stringElements.Reverse();
if (stringElements.Length > 0)
{
cboHex0.Text = stringElements[0];
}
if (stringElements.Length > 1)
{
cboHex1.Text = stringElements[1];
}
if (stringElements.Length > 2)
{
cboHex2.Text = stringElements[2];
}
if (stringElements.Length > 3)
{
cboHex3.Text = stringElements[3];
}
if (stringElements.Length > 4)
{
cboHex4.Text = stringElements[4];
}
if (stringElements.Length > 5)
{
cboHex5.Text = stringElements[5];
}
if (stringElements.Length > 6)
{
cboHex6.Text = stringElements[6];
}
if (stringElements.Length > 7)
{
cboHex7.Text = stringElements[7];
}
}
Edit: Okay so seems that I can target a character from a string by stringName[index].编辑:好的,看来我可以通过stringName [index]从字符串中定位一个字符。
So if the returned string is "1AF", how can I code so that the 8 comboboxes will show 0, 0, 0, 0, 0, 1, A, F?因此,如果返回的字符串是“1AF”,我该如何编码以使 8 个组合框显示 0、0、0、0、0、1、A、F?
First, to make the work easier, make your result a consistent length:首先,为了使工作更容易,使您的结果长度一致:
hexStr = hexStr.PadLeft(8, '0');
Then, you can simply assign the boxes as follows:然后,您可以简单地分配框,如下所示:
cboHex0.Text = hexStr.Substring(0,1);
cboHex1.Text = hexStr.Substring(1,1)
cboHex2.Text = hexStr.Substring(2,1);
etc.等等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.