//dt是一张读取好了的表 //palResultFilters是一个Panel if (dt.Count > 0) { palResultFilters.Controls.Clear(); for (int n = 0; n < dt.Count; n++) { MenuButton btn = new MenuButton(); btn.Size = new Size(235, 45); btn.Location = new Point(13, n * 50 + 8); //以下两行是我读多语言用的,你直接替换数据库的值 string filterName = Lang.GetOptionStr(Lang.OptionType.Data, "ResultFilter", n, false); if (string.IsNullOrWhiteSpace(filterName)) filterName = dt[n].FilterName; btn.Text = filterName; btn.Tag = n; btn.Click += new EventHandler(btn_Click); palResultFilters.Controls.Add(btn); } }
dt的话 你是直接读取数据库 还是三层 貌似我这有点问题 能把你那关于dt贴出来么 借鉴借鉴
把Button放到Button[]数组里面。然后通过索引可以很方便找到的,
不知下面这种效果是否楼主想要的。下面几个参数相信lz懂的!namespace Test { class MiniButton { private int m_Rows; private int m_Cols; private int m_xOffset, m_yOffset; private Button[][] m_buttons; /// <summary> /// Create buttons and add them to given groupbox. /// </summary> /// <param name="cols"> /// Number of buttons (vertical). /// </param> /// <param name="rows"> /// Number of buttons (horizontal). /// </param> /// <param name="parent"> /// GroupBox, in which buttons will be added. /// </param> public MiniButton(int rows, int cols, Control parent, int xOffset, int yOffset) { if (cols < 1) { throw new ArgumentException(cols.ToString() + " is invalid argument."); } if (rows < 1) { throw new ArgumentException(rows.ToString() + " is invalid argument."); } m_Cols = cols; m_Rows = rows; m_xOffset = xOffset; m_yOffset = yOffset; m_buttons = new Button[m_Cols + 1][]; for (int j = 0; j < m_Cols; j++) { m_buttons[j] = new Button[m_Rows + 1]; for (int i = 0; i <= m_Rows; i++) { m_buttons[j][i] = new Button(); m_buttons[j][i].Size = new Size(60, 18); m_buttons[j][i].UseMnemonic = false; parent.Controls.Add(m_buttons[j][i]); } } ReLocate(); } /// <summary> /// Calculate locations of buttons and set their coordinates. /// Also, change font and alignment for the header row. /// </summary> private void ReLocate() { for (int i = 0; i <= m_Rows; i++) { m_buttons[0][i].Location = new Point(m_xOffset, m_yOffset + m_buttons[0][i].Size.Height * i); for (int j = 1; j < m_Cols; j++) { if (i == 0) { m_buttons[j][i].Location = new Point(m_buttons[j - 1][i].Location.X + m_buttons[j - 1][i].Size.Width, m_yOffset); } else { m_buttons[j][i].Location = new Point(m_buttons[j - 1][i].Location.X + m_buttons[j - 1][i].Size.Width, m_buttons[j][i - 1].Location.Y + m_buttons[j][i - 1].Size.Height); } } } } public int Rows { get { return m_Rows; } } /// <summary> /// Get/set the item for the specifed row and column. /// </summary> /// <param name="row"> /// The specifed row. /// </param> /// <param name="col"> /// The specifed column. /// </param> public Button this[int row, int col] { get { if (col < 0 || col >= m_Cols) { throw (new IndexOutOfRangeException(col.ToString() + " is invalid column index")); } if (row < 0 || row >= m_Rows) { throw (new IndexOutOfRangeException(row.ToString() + " is invalid row index")); } return m_buttons[col][row + 1]; } set { if (col < 0 || col >= m_Cols) { throw (new IndexOutOfRangeException(col.ToString() + " is invalid column index")); } if (row < 0 || row >= m_Rows) { throw (new IndexOutOfRangeException(row.ToString() + " is invalid row index")); } m_buttons[col][row + 1] = value; } } } }
泛型列表,foreach输入,然后用foreach每个都赋值吧
List<KEYBOARD3> kb3 = new List<KEYBOARD3>(); KEYBOARD3Manager kbm3 = new KEYBOARD3Manager(); kb3 = kbm3.GetModelList(""); int i = 0; foreach (Control ctrl in this.groupBox1.Controls) {
foreach (KEYBOARD3 kbb3 in kb3) //循环所有的button { int y = 0;//临时变量 if (ctrl is Button && y == i) //每一个button循环只会成立一次 以此来修复值相同的bug { Button bn = (Button)ctrl; MessageBox.Show(bn.Name); bn.Text = kbb3.POS.ToString(); bn.Tag = kbb3.KEYV; break;//跳出本次循环 } y++; } i++; } if (ctrl is Button && y == i) //每一个button循环只会成立一次 以此来修复值相同的bug 这句第二次为真的时候 不执行if中的语句 是什么原因呢 i的值是可以改变的
//palResultFilters是一个Panel
if (dt.Count > 0)
{
palResultFilters.Controls.Clear();
for (int n = 0; n < dt.Count; n++)
{
MenuButton btn = new MenuButton();
btn.Size = new Size(235, 45);
btn.Location = new Point(13, n * 50 + 8);
//以下两行是我读多语言用的,你直接替换数据库的值
string filterName = Lang.GetOptionStr(Lang.OptionType.Data, "ResultFilter", n, false);
if (string.IsNullOrWhiteSpace(filterName)) filterName = dt[n].FilterName;
btn.Text = filterName;
btn.Tag = n;
btn.Click += new EventHandler(btn_Click);
palResultFilters.Controls.Add(btn);
}
}
dt的话 你是直接读取数据库 还是三层
貌似我这有点问题 能把你那关于dt贴出来么 借鉴借鉴
{
class MiniButton
{
private int m_Rows;
private int m_Cols;
private int m_xOffset, m_yOffset;
private Button[][] m_buttons; /// <summary>
/// Create buttons and add them to given groupbox.
/// </summary>
/// <param name="cols">
/// Number of buttons (vertical).
/// </param>
/// <param name="rows">
/// Number of buttons (horizontal).
/// </param>
/// <param name="parent">
/// GroupBox, in which buttons will be added.
/// </param>
public MiniButton(int rows, int cols, Control parent, int xOffset, int yOffset)
{
if (cols < 1)
{
throw new ArgumentException(cols.ToString() + " is invalid argument.");
}
if (rows < 1)
{
throw new ArgumentException(rows.ToString() + " is invalid argument.");
} m_Cols = cols;
m_Rows = rows;
m_xOffset = xOffset;
m_yOffset = yOffset; m_buttons = new Button[m_Cols + 1][];
for (int j = 0; j < m_Cols; j++)
{
m_buttons[j] = new Button[m_Rows + 1];
for (int i = 0; i <= m_Rows; i++)
{
m_buttons[j][i] = new Button();
m_buttons[j][i].Size = new Size(60, 18);
m_buttons[j][i].UseMnemonic = false;
parent.Controls.Add(m_buttons[j][i]);
}
}
ReLocate();
} /// <summary>
/// Calculate locations of buttons and set their coordinates.
/// Also, change font and alignment for the header row.
/// </summary>
private void ReLocate()
{
for (int i = 0; i <= m_Rows; i++)
{
m_buttons[0][i].Location = new Point(m_xOffset, m_yOffset + m_buttons[0][i].Size.Height * i);
for (int j = 1; j < m_Cols; j++)
{
if (i == 0)
{
m_buttons[j][i].Location = new Point(m_buttons[j - 1][i].Location.X +
m_buttons[j - 1][i].Size.Width, m_yOffset);
}
else
{
m_buttons[j][i].Location = new Point(m_buttons[j - 1][i].Location.X +
m_buttons[j - 1][i].Size.Width,
m_buttons[j][i - 1].Location.Y +
m_buttons[j][i - 1].Size.Height);
}
}
}
}
public int Rows
{
get
{
return m_Rows;
}
} /// <summary>
/// Get/set the item for the specifed row and column.
/// </summary>
/// <param name="row">
/// The specifed row.
/// </param>
/// <param name="col">
/// The specifed column.
/// </param>
public Button this[int row, int col]
{
get
{
if (col < 0 || col >= m_Cols)
{
throw (new IndexOutOfRangeException(col.ToString() + " is invalid column index"));
}
if (row < 0 || row >= m_Rows)
{
throw (new IndexOutOfRangeException(row.ToString() + " is invalid row index"));
}
return m_buttons[col][row + 1];
}
set
{
if (col < 0 || col >= m_Cols)
{
throw (new IndexOutOfRangeException(col.ToString() + " is invalid column index"));
}
if (row < 0 || row >= m_Rows)
{
throw (new IndexOutOfRangeException(row.ToString() + " is invalid row index"));
}
m_buttons[col][row + 1] = value;
}
}
}
}
List<KEYBOARD3> kb3 = new List<KEYBOARD3>();
KEYBOARD3Manager kbm3 = new KEYBOARD3Manager();
kb3 = kbm3.GetModelList("");
int i = 0;
foreach (Control ctrl in this.groupBox1.Controls)
{
foreach (KEYBOARD3 kbb3 in kb3) //循环所有的button {
int y = 0;//临时变量
if (ctrl is Button && y == i) //每一个button循环只会成立一次 以此来修复值相同的bug
{
Button bn = (Button)ctrl;
MessageBox.Show(bn.Name);
bn.Text = kbb3.POS.ToString();
bn.Tag = kbb3.KEYV;
break;//跳出本次循环
}
y++;
}
i++;
}
if (ctrl is Button && y == i) //每一个button循环只会成立一次 以此来修复值相同的bug
这句第二次为真的时候 不执行if中的语句 是什么原因呢 i的值是可以改变的
dt 应该是 DataTable