Thread og pictureBox,, opfølgning
Fortsættelse af http://www.eksperten.dk/spm/814733Jeg har nu lavet et light udgave er det projekt som jeg tumler med.
Jeg vil gerne load et billede ind i en for og mens jeg ser på dette begynde at loade flere billeder ind i hukommelsen og tilknytte hver af dem til en picturebox som ikke er synlig før jeg justerer pictuerboxens størrelse og placering.
TIl dette formål
Jeg har en form, en controller og en pictureboxklasse
FORMEN:
namespace PictureBrowser
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ControllerSmallPic.Instance.init(this);
}
private void button1_Click(object sender, EventArgs e)
{
//ControllerSmallPic.Instance.LoadPictureBoxesBackground();
Thread t = new Thread(ControllerSmallPic.Instance.LoadPictureBoxesBackground);
t.Start();
}
}
}
CONTROLLEREN
public class ControllerSmallPic
{
private static ControllerSmallPic controllerThumbnails;
private Form1 f;
private List<PictureBox_suber> pictureBox_List;
private ControllerSmallPic() { }
public static ControllerSmallPic Instance
{
get
{
lock (typeof(ControllerSmallPic))
{
if (controllerThumbnails == null)
controllerThumbnails = new ControllerSmallPic();
return controllerThumbnails;
}
}
}
public void init(Form1 f)
{
this.f = f;
f.Show();
this.pictureBox_List = new List<PictureBox_suber>();
AddPictureboxToConPic();
AddPictureboxToForm(0);
}
public void LoadPictureBoxesBackground( )
{
for (int id = 1; id < 10; id++)
{
AddPictureboxToConPic();
AddPictureboxToForm(id);
}
MessageBox.Show("Done");
}
public void AddPictureboxToConPic()
{
int id = pictureBox_List.Count;
pictureBox_List.Add(new PictureBox_suber(id, f, 100, 100, 100, 100));
}
public void AddPictureboxToForm(int id)
{
pictureBox_List[id].showPictureBox();
pictureBox_List[id].Picturebox.Location = new System.Drawing.Point(10, 10);
}
}
}
PICTUREBOXKLASSEN:
using System;
using System.Threading;
using System.IO;
using System.Windows.Forms;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
namespace PictureBrowser
{
public class PictureBox_suber {
private PictureBox p;
private Form f;
private int id;
delegate void AddItDelegate(PictureBox p);
void AddIt(PictureBox p)
{
f.Controls.Add(p);
f.Invalidate();
}
public PictureBox_suber(int id, Form1 f, int x, int y, int width, int height)
{
this.id = id;
this.f = f;
this.p = new PictureBox();
p.Name = id.ToString();
p.Location = new System.Drawing.Point(x, y);
p.Size= new System.Drawing.Size(width, height);
p.SizeMode = PictureBoxSizeMode.Zoom;
p.BackColor = Color.BlueViolet;
}
public PictureBox Picturebox
{
get{return p;}
}
public void showPictureBox()
{
Invoke(new AddItDelegate(AddIt), new object[] {p });
f.Controls.Add(p);
}
}
}
Jeg har i eksemplet ikke tilføjet billeder:
Først loader jeg en picterebox :
public void init(Form1 f)
{
this.f = f;
f.Show();
this.pictureBox_List = new List<PictureBox_suber>();
AddPictureboxToConPic();
AddPictureboxToForm(0);
}
Derefter vil jeg så starte med at loade 9 ekstra pictureboxe ind i hukommelsen:
public void LoadPictureBoxesBackground( )
{
for (int id = 1; id < 10; id++)
{
AddPictureboxToConPic();
AddPictureboxToForm(id);
}
MessageBox.Show("Done");
}
Men dette vil jeg gerne have gjort med en tråd. Jeg har sat en knap på tråden.
Men det dutter ikke.
I PICTUREBOXKLASSEN kendes ordet invoke ikke.
Hvad er galt ?
Michael
