私は、画像ボックス、2つのボタン、テキストボックスを各画像に対して動的に作成しながら、画像ファイルをURLからダウンロードして画像ボックスに設定できるWindowsフォームアプリケーションを構築しています。以下は 画像ファイルをダウンロードしてコントロールを動的に作成するC#
は私がアプリケーションを実行するとnoofimages = no of images
imagesnames = array containing images names
uploadnumber= folder name created with images
for (int i = 0; i < Globals.noofimages; i++)
{
client = new WebClient();
string url = "http://www.upload2printer.co.il/public/uploads/" + Globals.uploadnumber + "/" + Globals.imagesnames[i] + "";
Uri uri = new Uri(url);
byte[] bytes;
bytes = client.DownloadData(uri);
MemoryStream ms = new MemoryStream(bytes);
Globals.images[i] = System.Drawing.Image.FromStream(ms);
create_controls(i);
}
Now create_controls(i) is a method responsible for creating the controls
private void create_controls(int index)
{
PictureBox pb = new PictureBox();
pb.Image = Globals.images[index];
pb.Size = new Size(200, 120);
Button b1 = new Button();
Button b2 = new Button();
TextBox noofprints = new TextBox();
b1.Size = new Size(20, 20);
b1.Name = index.ToString();
b1.Text = "+";
b1.Location = new Point(x, y + 125);
b1.Click += new EventHandler(button_Click);
b2.Size = new Size(20, 20);
b2.Text = "-";
b2.Name = index.ToString();
b2.Location = new Point(x + 180, y + 125);
b2.Click += new EventHandler(button_Click);
noofprints.Name = index.ToString();
noofprints.Size = new Size(160, 18);
if (status == 0)
{
noofprints.Text = "1";
}
else if (status == 1)
{
noofprints.Text = Globals.noofcopy[index].ToString();
}
noofprints.TextAlign = HorizontalAlignment.Center;
noofprints.Location = new Point(x + 20, y + 125);
pb.SizeMode = PictureBoxSizeMode.StretchImage;
pb.Location = new Point(x, y);
x += pb.Width + 10;
maxheight = Math.Max(pb.Height, maxheight);
if (x > this.panel1.Width - 100)
{
x = 20;
y += maxheight + 30;
}
this.panel1.Controls.Add(b1);
this.panel1.Controls.Add(b2);
this.panel1.Controls.Add(noofprints);
this.panel1.Controls.Add(pb);
}
今のForm_Loadメソッドでループを使用してイメージファイルをダウンロードするための私のコードであり、この形式は、すべての写真が読み込まれ、すべてのコントロールされているときのみ表示され、フォームを開いています作成した。
私がしたいことは、各写真をダウンロードし、フォームが表示されている間、それぞれのコントロールを作成することです。
私もマルチスレッドと非同期と待機を調べましたが、周りに何かを描くことはできませんでした。
私は初心者で独学のプログラマーですので、私のコードは完璧ではないことが分かりますので、この問題を解決するにはあなたの助けが必要です。
ありがとうございます。
これは、Form_Loadイベントで行うことができます。このイベントの終了後、フォームが表示されます。 Form_Loadイベントで新しいタスクを作成し、ダウンロードした各写真の後にフォームに追加します。 別のヒント: コントロールの場所を変更する必要があります –
https://stackoverflow.com/questions/19163966/add-controls-to-gui-in-background-workerをご覧ください。 UIを反応的に保ち、一度に1つずつコントロールを追加し、追加するとすぐにレンダリングしたいとします。私はこれが行く方法だと思う! – Digvijay