我在为表格布局面板中的用户控件触发鼠标单击事件时遇到问题。我用这个类创建了一个用户控件。
public partial class ComputerControl : UserControl
{
private int computerId;
public int ComputerId
{
get { return computerId; }
set { computerId = value; }
}
private String computerName;
public String ComputerName
{
get { return computerName; }
set { computerName = value; }
}
private String computerIp;
public String ComputerIp
{
get { return computerIp; }
set { computerIp = value; }
}
private String computerMacAddress;
public String ComputerMacAddress
{
get { return computerMacAddress; }
set { computerMacAddress = value; }
}
private Image computerPicture;
public Image ComputerPicture
{
get { return computerPicture; }
set { computerPicture = value; }
}
private RightClikControl rightClick = new RightClikControl();
public RightClikControl RightClick
{
get { return rightClick; }
set { rightClick = value; }
}
public ComputerControl(int computerId, String computerName, String computerIp, String computerMacAddress, Image computerPicture)
{
InitializeComponent();
ComputerId = computerId;
ComputerName = computerName;
ComputerIp = computerIp;
ComputerMacAddress = computerMacAddress;
ComputerPicture = computerPicture;
labelComputerState.Text = ComputerName;
pictureBoxComputer.Image = ComputerPicture;
}
private void ComputerControl_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
RightClick.Show();
}
}
}
我使用这种方法在表格布局面板中添加了多个控件。
private void InsertComputers()
{
int i, j;
tableLayoutPanelComputers.Visible = false;
tableLayoutPanelComputers.Controls.Clear();
tableLayoutPanelComputers.RowStyles.Clear();
tableLayoutPanelComputers.ColumnStyles.Clear();
tableLayoutPanelComputers.ColumnCount = tableLayoutPanelComputers.Size.Width / 130;
tableLayoutPanelComputers.RowCount = tableLayoutPanelComputers.Size.Height / 100;
for (i = 0; i < tableLayoutPanelComputers.RowCount; i++)
{
tableLayoutPanelComputers.RowStyles.Add(new RowStyle(SizeType.Absolute, 100));
}
for (j = 0; j < tableLayoutPanelComputers.ColumnCount; j++)
{
tableLayoutPanelComputers.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 130));
}
int clientCount = 0;
for (i = 0; i < tableLayoutPanelComputers.RowCount && clientConf.Count > clientCount; i++)
{
for (j = 0; j < tableLayoutPanelComputers.ColumnCount && clientConf.Count > clientCount; j++)
{
ComputerControl cc = new ComputerControl(clientConf[clientCount].ClientId, clientConf[clientCount].ClientName, clientConf[clientCount].ClientIp, clientConf[clientCount].ClientMacAddress,Images.Pc_PowerOff);
tableLayoutPanelComputers.Controls.Add(cc, j, i);
clientCount++;
}
}
tableLayoutPanelComputers.Visible = true;
}
因此,当我在表格布局面板单元格中的用户控件上单击鼠标右键时,任何事情都会启动。
我在编码什么错误?