PDA

View Full Version : سوال: مشگل در ساخت Instance جدید



pouria_bayat
یک شنبه 13 مهر 1393, 21:02 عصر
در کلاس A:

private void Control_Load(object sender, EventArgs e)
{
Thread RunAdding = new Thread(new ThreadStart(ComponentAdd));
RunAdding.Start();
}
void ComponentAdd()
{
int item = 2;
Action act = new Action(() =>
{
dynamicTableLayoutPanel = new TableLayoutPanel();
dynamicTableLayoutPanel.Controls.Clear();
dynamicTableLayoutPanel.Refresh();
dynamicTableLayoutPanel.AutoScroll = true;
dynamicTableLayoutPanel.Location = new System.Drawing.Point(0, 0);
dynamicTableLayoutPanel.Name = "TableLayoutPanelFirst";
dynamicTableLayoutPanel.Size = new System.Drawing.Size(MainPanel.Width, MainPanel.Height);

dynamicTableLayoutPanel.VerticalScroll.Visible = false;
dynamicTableLayoutPanel.VerticalScroll.Enabled = false;
dynamicTableLayoutPanel.BackColor = Color.Transparent;
dynamicTableLayoutPanel.ColumnCount = 3;

//
MainLayout = new FlowLayoutPanel[item];
picbox = new PictureBox[item];
lblName = new Label[item];
for (int i = 0; i < item; i++)
{
MainLayout[i] = new FlowLayoutPanel();
MainLayout[i].Size = new System.Drawing.Size(333, 263);
MainLayout[i].Location = new System.Drawing.Point(0, 0);
MainLayout[i].FlowDirection = FlowDirection.TopDown;
MainLayout[i].WrapContents = false;
MainLayout[i].BackColor = Color.WhiteSmoke;
//
picbox[i] = new PictureBox();
picbox[i].Size = new Size(327, 207);
picbox[i].MouseClick += SystemPic_Click;
picbox[i].Name = i.ToString();
picbox[i].Anchor = AnchorStyles.None;
picbox[i].Cursor = Cursors.Hand;
picbox[i].SizeMode = PictureBoxSizeMode.StretchImage;
picbox[i].BackColor = System.Drawing.Color.Transparent;
picbox[i].Image = Narm.Properties.Resources.RD2;
MainLayout[i].Controls.Add(picbox[i]);

Handle CH = new Handle(i, picbox[i]);
if (Dic.ContainsKey(i))
Dic[i] = CH;
else
Dic.Add(i, CH);
Console.Out.WriteLine("End OF Create");

}
MainPanel.Controls.Add(dynamicTableLayoutPanel);
MainPanel.Refresh();
MainPanel.ResumeLayout();

});
if (this.InvokeRequired)
this.BeginInvoke(act);
else
act();
}


همانطور که می بینید در کلاس A از کلاس B(یا Handle) یک شی جدید یا Instance جدید ساخته شده و دو پارامتر i , picbox[i] رو به ان کلاس میفرستد. اما کلاس B :


public Handle(int p, PictureBox box)
{
if (_picbox != null)
if (_picbox.Name == "0" && box.Name == "1")
{
MessageBox.Show("yes");
}
_picbox = box;
StartRunning();
}

و اما مشگلی که است مثل اینکه اصلا Instance جدیدی ساخته نمیشه و پیغام Yes به نمایش در می اید یعنی اینکه مقادیر Instance دوم جایگزین شی اولی یا INstance اولی می شود.
چه کار می شود کرد که دوتا شی از یک کلاس به صورت جداگانه ایجاد کنم؟

parvizwpf
دوشنبه 14 مهر 1393, 09:35 صبح
میشه کمی بیشتر توضیح بدید.