根据下面的代码,我可以从我的SQL服务器表中收集存储的图像,并在每个图像下面放置一个CheckBox。我的目标是使用这些CheckBoxes选择它们上方的图像并使用另一个用户控件执行操作(即用于从DB中删除它们或将它们复制到另一个目录的按钮)。我试图创建一个事件处理程序,但无法用我的c#知识水平实现。我恳请一个解决方案和指导,以便更好地理解。
private void button4_Click(object sender, EventArgs e)
{
PictureBox[] pba = new PictureBox[100];
SqlConnection con4 = new SqlConnection(DBHandler.GetConnectionString());
SqlCommand cmd4 = new SqlCommand("ReadAllImage", con4);
cmd4.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da2 = new SqlDataAdapter(cmd4);
DataSet dt2 = new DataSet("ImageData");
da2.SelectCommand = cmd4;
da2.Fill(dt2);
int a = comboBox1.Items.Count;
byte[] xdata = new byte[0];
CheckBox[] chckbx = new CheckBox[400];
for (var i = 0; i < a; i++)
{
DataRow myRow2 = null;
if (myRow2 == null)
{
myRow2 = dt2.Tables[0].Rows[i];
xdata = (byte[])myRow2["ImageData"];
MemoryStream stream2 = new MemoryStream(xdata);
PictureBox npb = new PictureBox();
npb.Size = new Size(60, 60);
int b = i / 5;
npb.Location = new Point(420 + (i % 5) * 70, 20 + (90 * b));
npb.Image = Image.FromStream(stream2);
npb.SizeMode = PictureBoxSizeMode.StretchImage;
pba[i] = npb;
this.Controls.Add(pba[i]);
CheckBox chckbxx = new CheckBox();
chckbxx.Location = new Point(420 + (i % 5) * 70, 80 + (90 * b));
chckbxx.AutoSize = true;
chckbx[i] = chckbxx;
this.Controls.Add(chckbx[i]);
}
答案 0 :(得分:0)
制作一个自定义控件来保存图像和复选框。 然后两者总是连在一起: 逻辑上和编程上。
自定义控件的示例: https://msdn.microsoft.com/en-us/library/ff723977%28v=expression.40%29.aspx
http://www.codeproject.com/Articles/2016/Writing-your-Custom-Control-step-by-step