delegate fra trå til listbox, igennem methode?
Hej eksperter, jeg har prøve og kigge på delegate, og syntes det er ekstremt svært.. så jeg prøvede og lave noget med det.Jeg prøver og køre en tråd "t", af Klassens "dataloader" methode "LoadXML", og sende løbende data til min main, men det er som om jeg er stødt på grund, da min methode jeg sender til SKAL være statisk? så kan jeg bare ikek have listbox1 i den? kan man gøre noget ved det?
min kode
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Threading;
namespace ItemBox
{
delegate void myDelegate(string name);
public class dataloader
{
myDelegate del1 = new myDelegate(Form1.AddItem);
XmlDocument spawnfile = new XmlDocument();
XmlDocument lootkeyfile = new XmlDocument();
XmlDocument resourcefile = new XmlDocument();
public void LoadXML()
{
spawnfile.Load("spawns.xml");
lootkeyfile.Load("lootkeys.xml");
resourcefile.Load("resources.xml");
for (int i = 0; i < 100; i++)
{
del1("TEST");
System.Threading.Thread.Sleep(100);
}
}
}
public partial class Form1 : Form
{
dataloader bw = new dataloader();
Thread t;
public Form1()
{
InitializeComponent();
t = new Thread(new ThreadStart(bw.LoadXML));
}
//Load Knap
private void button1_Click(object sender, EventArgs e)
{
//Start Background Worker
if (t.IsAlive)
{
t.Abort();
}
else
{
t.Start();
}
}
public static void AddItem(string name)
{
// listBox1.Items.Add(name);
}
formkode
namespace ItemBox
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.d = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.Location = new System.Drawing.Point(12, 12);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(526, 472);
this.listBox1.TabIndex = 0;
//
// d
//
this.d.Location = new System.Drawing.Point(463, 495);
this.d.Name = "d";
this.d.Size = new System.Drawing.Size(75, 23);
this.d.TabIndex = 1;
this.d.Text = "Load";
this.d.UseVisualStyleBackColor = true;
this.d.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(550, 530);
this.Controls.Add(this.d);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button d;
}
}