如何使位数组(true或false)改变颜色C# [英] How to make bit array(true or false) change back color C#
本文介绍了如何使位数组(true或false)改变颜色C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<pre> public static BitArray ConvertHexToBitArray(string hexData)
{
if (hexData == null)
return null;
BitArray ba = new BitArray(4* hexData.Length);
for (int i = 0; i < hexData.Length; i++)
{
byte b = byte.Parse(hexData[i].ToString(), NumberStyles.HexNumber);
for (int j = 0; j < 4; j++)
{
ba.Set(i * 4 + j, (b & (1 << (3 - j))) != 0);
}
}
return ba;
<pre>private void button5_Click(object sender, EventArgs e)
{ //label1.Text = HexTobyteArray.BitArrayToString(array);
var array = HexTobyteArray.ConvertHexToBitArray(textBox1.Text);
// string a = HexTobyteArray.BitArrayToString(array);
foreach (bool b in array)
{
MessageBox.Show(b.ToString());
Button[] btns = new Button[4];
btns[0] = this.button1;
btns[1] = this.button2;
btns[2] = this.button3;
btns[3] = this.button4;
// label1.Text = a;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (b == true)
{
btns[i].BackColor = Color.Green;
}
else
{
btns[i].BackColor = Color.Red;
}
}
}
}
}
我尝试了什么:
我的问题是数组更改颜色按钮,
请求帮助我
What I have tried:
my problem is arrays to change back Color buttons,
pleass help me
推荐答案
注意:此处显示的技术不适用于前缀为'0x的十六进制字符串,或'0X:如果你想允许,测试和删除这些前缀。或者,将转换技术切换为:[ ^ ] ...如果你这样做,你将没有'TryParse的固有验证的优势。
0.在表格级别列出你的按钮...使用:
Note: the technique shown here will not work with hex string prefixed with '0x, or '0X: if you want to allow that, test for, and strip out, those prefixes. Or, switch the conversion technique to:[^] ... if you do that, you will not have the advantage of 'TryParse's inherent validation.
0. make a list of your Buttons at the Form level ... for re-use:
private List<Button> buttons;
// in the Form 'Load Event
buttons = new List<Button> { button1, button2, button3, button4 };
1。验证你有一个'合法'十六进制格式的字符串,然后使用位移来测试:
1. validate you have a string in 'legal' hex format, then use bit-shifting to test:
int hextoint;
if (Int32.TryParse(textBox1.Text, NumberStyles.HexNumber, CultureInfo.CurrentCulture, out hextoint))
{
for (int i = 0; i < buttons.Count; i++)
{
buttons[i].BackColor = ((hextoint & 1) == 1)
? Color.Green
: Color.Red;
hextoint = hextoint >> 1;
}
}
else
{
throw new ArgumentException("bad hex input");
}
这篇关于如何使位数组(true或false)改变颜色C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文