我创建了大约12个picbox并且动态标签能够检索picbox中的数据并从SQL中标记。二进制格式的picbox图像和表中的imagestore也包含数据库中的图像标题的行。
问题是我想在picbox上添加一个click_event。尽快我点击picbox一个textbox1。我创建的文本必须显示存储在SQL中的图像的标题。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace library
{
public partial class showingmore : Form
{
private string passvalue;
public string passval
{
get { return passvalue; }
set { passvalue = value; }
}
public showingmore()
{
InitializeComponent();
}
MemoryStream ms;
byte[] photo_aray;
private void showingmore_Load_1(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection("Provider=SQLOLEDB;User ID=sa;Password =12345678; Initial Catalog=library; server=raj; TRUSTED_CONNECTION=true;");
OleDbDataAdapter Adap = new OleDbDataAdapter("select * from movie ", con);
textBox1.Text = passvalue;
DataSet ds = new DataSet();
Adap.Fill(ds);
//textBox1.Text = ds.Tables[0].Rows[15][0].ToString(); // the working way to use ds to fill data
//textBox1.Text = ds.Tables[0].Rows[1];
//int icount = ds.Tables[0].Rows.Count;
//textBox1.Text = icount.ToString();
/**** Creating Label's and Picture Box ****/
int n = 12; // total time for running loop.
int j = 14; // y-axis co-ordinate label
int k = 479; // x-axis co-ordinate label
int l = 18; // y-axis co-ordinate label
// Creating label through loop's.
for (int i = 0; i < n; i++)
{
//Create label
Label labels = new Label();
PictureBox picbox = new PictureBox();
labels.Text = ds.Tables[0].Rows[i][0].ToString();
if (i <= 5)
{
//Position label on screen
labels.Location = new Point(j, k);
j = j + 228;
// Label text color
labels.ForeColor = Color.Gainsboro;
}
else
{
k = 782; // x-axis co-ordinate
labels.Location = new Point(l, k);
l = l + 228;
labels.ForeColor = Color.Gainsboro;
}
this.Controls.Add(labels);
}
n = 12; // total time for running loop.
j = 18; // y-axis co-ordinate
k = 246; // x-axis co-ordinate
l = 18; // y-axis co-ordinate
// Creating PictureBox through loop's.
for (int i = 0; i < n; i++)
{
//Create PictureBox
PictureBox picbox = new PictureBox();
picbox.Image = null;
if (ds.Tables[0].Rows[i][14] != System.DBNull.Value)
{
photo_aray = (byte[])ds.Tables[0].Rows[i][14];
MemoryStream ms = new MemoryStream(photo_aray);
picbox.Image = Image.FromStream(ms);
}
if (i <= 5)
{
//Position PictureBox on screen
picbox.Location = new Point(j, k);
picbox.Size = new Size(161, 220);
picbox.BackColor = Color.Gainsboro;
j = j + 228;
}
else
{
k = 543; // y-axis co-ordinate
picbox.Location = new Point(l, k);
picbox.Size = new Size(161, 220);
picbox.BackColor = Color.Gainsboro;
l = l + 228;
}
this.Controls.Add(picbox);
}
}
}
}
答案 0 :(得分:1)
要获取动态创建的PictureBox的click事件,您可以在创建时简单地订阅click事件;
private void createPicBoxes()
{
for (int i = 0; i <= 12; i++)
{
PictureBox picBox = new PictureBox();
picBox.Click += picBox_Click;
}
}
static void picBox_Click(object sender, EventArgs e)
{
//do your stuff here which handles generically all of your picture boxes clicks.
}
简而言之,订阅这样的控件事件意味着无论何时执行该操作,在您的情况下都会发生单击事件,附加的方法将会启动。
答案 1 :(得分:0)
在将picbox添加到Controls
集合之前将其放在循环中的某个位置:
picbox.MouseClick += picbox_MouseClick;
然后,在showingmore
类的某处实现点击处理程序:
private void picbox_MouseClick(object sender, MouseEventArgs e)
{
// Your code here.
}
答案 2 :(得分:0)
创建PictureBox时,将事件处理程序添加到PictureBox中。 像下面的东西
PictureBox picbox = new PictureBox();
picbox.Click += new EventHandler(this.picbox_Click);
void picbox_Click(object sender, System.EventArgs e)
{
//your code...
}