[C#][Control]BitsControl概念與簡易實做
int bitWith = width / 8;
bit8.Width = bitWith;
bit7.Width = bitWith;
bit6.Width = bitWith;
bit5.Width = bitWith;
bit4.Width = bitWith;
bit3.Width = bitWith;
bit2.Width = bitWith;
bit1.Width = bitWith;
int bitHeight = flowLayoutPanel1.ClientSize.Height - bit8.Margin.Top * 2;
bit8.Height = bitHeight;
bit7.Height = bitHeight;
bit6.Height = bitHeight;
bit5.Height = bitHeight;
bit4.Height = bitHeight;
bit3.Height = bitHeight;
bit2.Height = bitHeight;
bit1.Height = bitHeight;
}</pre>
public void LoadBitsState(string hexValue)
{
LoadBitsState(Convert.ToInt32(hexValue, 16));
}</pre>
namespace WindowsFormsApplication6 { public partial class BitsControl : UserControl { public BitsControl() { InitializeComponent(); }
public void LoadBitsState(int weightValue)
{
SwitchButton[] switchButtons = { bit1, bit2, bit3, bit4, bit5, bit6, bit7, bit8 };
for (int i = 8; i >= 1; --i)
{
int currentWeightValue = int.Parse(Math.Pow(2 , i - 1).ToString());
SwitchButton currentSwitchButton = switchButtons[i - 1];
if (weightValue >= currentWeightValue)
{
currentSwitchButton.State = SwitchButton.SwitchState.On;
weightValue -= currentWeightValue;
}
else
{
currentSwitchButton.State = SwitchButton.SwitchState.Off;
}
}
}
public void LoadBitsState(string hexValue)
{
LoadBitsState(Convert.ToInt32(hexValue, 16));
}
private void BitsControl_SizeChanged(object sender, EventArgs e)
{
int width = flowLayoutPanel1.ClientSize.Width - panel1.Width - bit8.Margin.Left * 16 - panel1.Margin.Left * 2;
if(width < 0)
return;
int bitWith = width / 8;
bit8.Width = bitWith;
bit7.Width = bitWith;
bit6.Width = bitWith;
bit5.Width = bitWith;
bit4.Width = bitWith;
bit3.Width = bitWith;
bit2.Width = bitWith;
bit1.Width = bitWith;
int bitHeight = flowLayoutPanel1.ClientSize.Height - bit8.Margin.Top * 2;
bit8.Height = bitHeight;
bit7.Height = bitHeight;
bit6.Height = bitHeight;
bit5.Height = bitHeight;
bit4.Height = bitHeight;
bit3.Height = bitHeight;
bit2.Height = bitHeight;
bit1.Height = bitHeight;
}
}
}