PDA

View Full Version : event در dynamic object



sareham238
دوشنبه 17 فروردین 1394, 12:39 عصر
سلام
خسته نباشید دوستان

من به یه مشکل برخوردم و هرچی تلاش کردم نتونستم اونو حل کنم... اگر میتونید به من کمک کنید


مشکل:
من یک button به صورت dynamic تولید میکنم ولی نمی تونم از اون button در event ها استفاده کنم... باید چیکار کنم

{
int x = 100, y = 100, n = 1;
public void CreateDynamicButton(Form form)
{
// Create a Button object
Button dynamicButton = new Button();

// Set Button properties
dynamicButton.Name = "btn";
dynamicButton.Name = "btn" + n;
dynamicButton.Location = new Point(x, y);
dynamicButton.Text = dynamicButton.Name;

// Add a Button Click Event handler
dynamicButton.Click += new EventHandler(dynamicButton_Click);

// Add Button to the Form. Placement of the Button
// will be based on the Location and Size of button
form.Controls.Add(dynamicButton);
n += 1;
x += 50;
y += 50;
}

private void dynamicButton_Click(object sender, EventArgs e)
{

"در اینجا button رو ندارم";
}
}




ممنون میشم کمکم کنید ....

am_al_59
دوشنبه 17 فروردین 1394, 12:54 عصر
شما Button رو در متد تعریف کردی برای همین وقتی از متد خارج میشی هیج آبجکتی بهش اشاره نداره
باید آبجکتی که اونو تو خودش داره بصورت فیلد خارج از متد تعریف کنی
این خطو قبل از تعریف متد CreateDynamicButton بنویس

Button dynamicButton=null;

این خط در متد رو هم


Button dynamicButton = newButton();

تغییر بده به


dynamicButton = newButton();

یونس ابراهیمی
دوشنبه 17 فروردین 1394, 12:56 عصر
اصلاح شده کدتون



using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication12
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

int x = 100, y = 100, n = 1;
// Create a Button object
Button dynamicButton = new Button();

// Set Button properties
dynamicButton.Name = "btn";
dynamicButton.Location = new Point(x, y);
dynamicButton.Text = dynamicButton.Name;

// Add a Button Click Event handler
dynamicButton.Click += new EventHandler(dynamicButton_Click);

// Add Button to the Form. Placement of the Button
// will be based on the Location and Size of button
this.Controls.Add(dynamicButton);
n += 1;
x += 50;
y += 50;
}

private void dynamicButton_Click(object sender, EventArgs e)
{
MessageBox.Show("OK");
}
}
}

sareham238
دوشنبه 17 فروردین 1394, 13:00 عصر
یعنی من باید یک آبجکتی رو تعریف کنم که Dynamic button من رو بسازه..؟؟؟

am_al_59
دوشنبه 17 فروردین 1394, 13:02 عصر
نه شما باید آبجکتی که Button رو توش داری بطور قابل دسترسی در کلاست تعریف کنی که در همه متدها بهش دسترسی داشته باشی اسمت اینطور تعریف کردن میشه فیلد

راه اول:

using System;
using System.Drawing;
using System.Windows.Forms;


namespace WindowsFormsApplication22
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();


int x = 100, y = 100, n = 1;
// Create a Button object
Button dynamicButton = new Button();


// Set Button properties
dynamicButton.Name = "btn";
dynamicButton.Location = new Point(x, y);
dynamicButton.Text = dynamicButton.Name;


// Add a Button Click Event handler
dynamicButton.Click += new EventHandler(dynamicButton_Click);


// Add Button to the Form. Placement of the Button
// will be based on the Location and Size of button
this.Controls.Add(dynamicButton);
n += 1;
x += 50;
y += 50;
}


private void dynamicButton_Click(object sender, EventArgs e)
{
MessageBox.Show(((Button)sender).Text);
}
}
}



راه دوم:


using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication12
{
public partial class Form1 : Form
{
Button dynamicButton=null;
public Form1()
{
InitializeComponent();

int x = 100, y = 100, n = 1;
// Create a Button object
dynamicButton = new Button();

// Set Button properties
dynamicButton.Name = "btn";
dynamicButton.Location = new Point(x, y);
dynamicButton.Text = dynamicButton.Name;

// Add a Button Click Event handler
dynamicButton.Click += new EventHandler(dynamicButton_Click);

// Add Button to the Form. Placement of the Button
// will be based on the Location and Size of button
this.Controls.Add(dynamicButton);
n += 1;
x += 50;
y += 50;
}

private void dynamicButton_Click(object sender, EventArgs e)
{
MessageBox.Show(dynamicButto.Text);
}
}
}

sareham238
دوشنبه 17 فروردین 1394, 13:03 عصر
آقا یونس من با MessageBox میتونم کار کنم ولی از خود button چجوری استفاده کنم

مثل:
button1.Left=20;

sareham238
دوشنبه 17 فروردین 1394, 13:05 عصر
نه شما باید آبجکتی که Button رو توش داری بطور قابل دسترسی در کلاست تعریف کنی که در همه متدها بهش دسترسی داشته باشی اسمت اینطور تعریف کردن میشه فیلد

میشه یه مثال بزنید ...

ممنون میشم

یونس ابراهیمی
دوشنبه 17 فروردین 1394, 13:06 عصر
منظورتونو متوجه نمیشم، شما اگه برنامه رو اجرا کنید یه دکمه روی فرم دارین، حالا میخواین چکارش کنید؟

am_al_59
دوشنبه 17 فروردین 1394, 13:12 عصر
این بنده خدا منظورش اینه که به کلید در سایر متدها دسترسی داشته باشه
چون کلید رو در متد تعریف کرده فقط هم در همون متد بهش دسترسی داره
البته من جسارت کردم از کد شما که کامل و دقیق بود استفاده کردم منظور خودمو بگم
امیدوارم منظور ایشونو درست فهمیده باشم
کدهای ویرایش شده رو در پست برای دوروش متفاوت گذاشتم

sareham238
دوشنبه 17 فروردین 1394, 13:12 عصر
نه شما باید آبجکتی که Button رو توش داری بطور قابل دسترسی در کلاست تعریف کنی که در همه متدها بهش دسترسی داشته باشی اسمت اینطور تعریف کردن میشه فیلد

راه اول:

using System;
using System.Drawing;
using System.Windows.Forms;


namespace WindowsFormsApplication22
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();


int x = 100, y = 100, n = 1;
// Create a Button object
Button dynamicButton = new Button();


// Set Button properties
dynamicButton.Name = "btn";
dynamicButton.Location = new Point(x, y);
dynamicButton.Text = dynamicButton.Name;


// Add a Button Click Event handler
dynamicButton.Click += new EventHandler(dynamicButton_Click);


// Add Button to the Form. Placement of the Button
// will be based on the Location and Size of button
this.Controls.Add(dynamicButton);
n += 1;
x += 50;
y += 50;
}


private void dynamicButton_Click(object sender, EventArgs e)
{
MessageBox.Show(((Button)sender).Text);
}
}
}



راه دوم:


using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication12
{
public partial class Form1 : Form
{
Button dynamicButton=null;
public Form1()
{
InitializeComponent();

int x = 100, y = 100, n = 1;
// Create a Button object
dynamicButton = new Button();

// Set Button properties
dynamicButton.Name = "btn";
dynamicButton.Location = new Point(x, y);
dynamicButton.Text = dynamicButton.Name;

// Add a Button Click Event handler
dynamicButton.Click += new EventHandler(dynamicButton_Click);

// Add Button to the Form. Placement of the Button
// will be based on the Location and Size of button
this.Controls.Add(dynamicButton);
n += 1;
x += 50;
y += 50;
}

private void dynamicButton_Click(object sender, EventArgs e)
{
MessageBox.Show(dynamicButto.Text);
}
}
}


ممنون از کمکتون

یونس ابراهیمی
دوشنبه 17 فروردین 1394, 13:16 عصر
اینطور هم میشه


using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication12
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

Button dynamicButton = new Button();

dynamicButton.Location = new System.Drawing.Point(100, 213);
dynamicButton.Name = "button2";
dynamicButton.Size = new System.Drawing.Size(75, 23);
dynamicButton.TabIndex = 0;
dynamicButton.Text = "button2";
dynamicButton.UseVisualStyleBackColor = true;
dynamicButton.Click += new System.EventHandler(this.button2_Click);

this.Controls.Add(dynamicButton);
}

private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("OK");
}

}
}

sareham238
دوشنبه 17 فروردین 1394, 14:14 عصر
به dynamicButton_MuoseMove نگاه کنید

باز اون قسمت ارور میده




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

int btnX = 100, btnY = 100, n = 1;
// Create a Button object
Button dynamicButton = new Button();

// Set Button properties
dynamicButton.Name = "btn";
dynamicButton.Name = "btn" + n;
dynamicButton.Location = new Point(btnX, btnY);
dynamicButton.Text = dynamicButton.Name;

// Add a Button Click Event handler
dynamicButton.MouseDown += new System.Windows.Forms.MouseEventHandler(dynamicButt on_MuoseDown);
dynamicButton.MouseMove += new System.Windows.Forms.MouseEventHandler(dynamicButt on_MuoseMove);
dynamicButton.MouseUp += new System.Windows.Forms.MouseEventHandler(dynamicButt on_MuoseUp);

// Add Button to the Form. Placement of the Button
// will be based on the Location and Size of button
this.Controls.Add(dynamicButton);
n += 1;
btnX += 50;
btnY += 50;
}

bool xy;
int x = 100, y = 100, n = 1;
private void dynamicButton_MuoseDown(object sender, MouseEventArgs e)
{
this.xy = true;
this.x = e.X;
this.y = e.Y;
}

private void dynamicButton_MuoseMove(object sender, MouseEventArgs e)
{
if (this.xy == true)
{
dynamicButton.Left = dynamicButton.Left + e.X - this.x;
dynamicButton.Top = dynamicButton.Top + e.Y - this.y;
}
}

private void dynamicButton_MuoseUp(object sender, MouseEventArgs e)
{
this.xy = false;
}

}
}

am_al_59
دوشنبه 17 فروردین 1394, 14:18 عصر
این خطو شما گذاشتی توی متد Form()

Button dynamicButton = new Button();


تبدیل کند به

dynamicButton = new Button();

قبل از متد Form() هم این خطو بزار

Button dynamicButton=null;

am_al_59
دوشنبه 17 فروردین 1394, 14:22 عصر
عین این کدو پیست کن

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
Button dynamicButton = null;
public Form1()
{
InitializeComponent();

int btnX = 100, btnY = 100, n = 1;
// Create a Button object
Button dynamicButton = new Button();

// Set Button properties
dynamicButton.Name = "btn";
dynamicButton.Name = "btn" + n;
dynamicButton.Location = new Point(btnX, btnY);
dynamicButton.Text = dynamicButton.Name;

// Add a Button Click Event handler
dynamicButton.MouseDown += new System.Windows.Forms.MouseEventHandler(dynamicButt on_MuoseDown);
dynamicButton.MouseMove += new System.Windows.Forms.MouseEventHandler(dynamicButt on_MuoseMove);
dynamicButton.MouseUp += new System.Windows.Forms.MouseEventHandler(dynamicButt on_MuoseUp);

// Add Button to the Form. Placement of the Button
// will be based on the Location and Size of button
this.Controls.Add(dynamicButton);
n += 1;
btnX += 50;
btnY += 50;
}


void Form1_Load(object sender, EventArgs e)
{
}


bool xy;
int x = 100, y = 100, n = 1;
private void dynamicButton_MuoseDown(object sender, MouseEventArgs e)
{
this.xy = true;
this.x = e.X;
this.y = e.Y;
}


private void dynamicButton_MuoseMove(object sender, MouseEventArgs e)
{
if (this.xy == true)
{
dynamicButton.Left = dynamicButton.Left + e.X - this.x;
dynamicButton.Top = dynamicButton.Top + e.Y - this.y;
}
}


private void dynamicButton_MuoseUp(object sender, MouseEventArgs e)
{
this.xy = false;
}


}
}

sareham238
دوشنبه 17 فروردین 1394, 14:54 عصر
دوست عزیز این راه حل هم جواب نداد ...وقتی تو RunTime میخوام از MouseMove استفاده کنم برنامه ارور میده

am_al_59
دوشنبه 17 فروردین 1394, 14:58 عصر
درست کار میکنه این نمونش همون کاری که میخواهید رو داره انجام میده

sareham238
دوشنبه 17 فروردین 1394, 15:01 عصر
درست کار میکنه این نمونش همون کاری که میخواهید رو داره انجام میده

به درست کار میکنه

ممنون از راهنمایی تون