我如何传递变量到一个按钮事件的方法? [英] How do i pass variables to a buttons event method?
问题描述
我需要能够沿着两个对象的方法,当我点击一个按钮被解雇通过。我如何做到这一点。
到目前为止,我一直在寻找创造改变EventArgs的:
公共类CompArgs:System.EventArgs
{
私有对象metode;
私人类型力typen;
公共CompArgs(物体M,T型)
{
this.metode =米;
this.typen = T;
}
公共对象Metode()
{
返回metode;
}
公共型力typen()
{
返回力typen;
}
}
不过,我将如何使用它?是否有可能以某种方式覆盖按钮的点击事件中使用自定义事件处理程序,这需要CompArgs作为参数?
私人无效的button1_Click(对象发件人,EventArgs五)
{
装配装配= Assembly.LoadFile(@C:\components.dll);
INT计数器= 0;
的foreach(在assembly.GetTypes类型类型())
{
如果(type.IsClass ==真)
{
按钮BTN =新按钮();
btn.Location =新点(174 +(计数器* 100),10);
btn.Size =新尺寸(95,23);
btn.Name = type.Name;
btn.Text = type.Name;
btn.UseVisualStyleBackColor = TRUE;
this.Controls.Add(BTN);
obj对象= Activator.CreateInstance(类型);
//我需要传递的OBJ和类型的btn_Click
btn.Click + =新的EventHandler(btn_Click);
计数器++;
}
}
}
和事件,方法,其中我需要它:
私人无效btn_Click(对象发件人,CompArgs CA)
{
MessageBox.Show ((字符串)ca.Typen()InvokeMember(getMyName,
BindingFlags.Default |。BindingFlags.InvokeMethod,
空,
ca.Metode(),
空) );
}
斜面你?刚才设置承载按钮,然后从按钮单击事件访问这些表单上的财产或者成员变量
编辑:反馈意见后自定义按钮的建议(不同样的建议如上)
MyButton类:按钮
{
私人类型m_TYpe;
私有对象m_Object;
公共对象的对象
{
{返回m_Object; }
集合{m_Object =价值; }
}
公共类型类型
{
{返回m_TYpe; }
集合{m_TYpe =价值; }
}
}
Button1Click(对象发件人,EventArgs参数)
{
用作MyButton MB =(发件人为用作MyButton);
//那么你就可以访问Mb.Type
//和Mb.object
}
I need to be able to pass along two objects to the method being fired when I click a button. How do i do this?
So far i've been looking at creating a changed eventArgs:
public class CompArgs : System.EventArgs
{
private object metode;
private Type typen;
public CompArgs(object m, Type t)
{
this.metode = m;
this.typen = t;
}
public object Metode()
{
return metode;
}
public Type Typen()
{
return typen;
}
}
But how would I use it? Is it possible to somehow override the click-event of the button to use a custom eventhandler, which takes CompArgs as a parameter?
private void button1_Click(object sender, EventArgs e)
{
Assembly assembly = Assembly.LoadFile(@"c:\components.dll");
int counter = 0;
foreach (Type type in assembly.GetTypes())
{
if (type.IsClass == true)
{
Button btn = new Button();
btn.Location = new Point(174 + (counter * 100),10);
btn.Size = new Size(95, 23);
btn.Name = type.Name;
btn.Text = type.Name;
btn.UseVisualStyleBackColor = true;
this.Controls.Add(btn);
object obj = Activator.CreateInstance(type);
//I need to pass on obj and type to the btn_Click
btn.Click += new eventHandler(btn_Click);
counter++;
}
}
}
And the event-method where i need it:
private void btn_Click(object sender, CompArgs ca)
{
MessageBox.Show((string)ca.Typen().InvokeMember("getMyName",
BindingFlags.Default | BindingFlags.InvokeMethod,
null,
ca.Metode(),
null));
}
Cant you just set a property or member variable on the form that hosts the button and access these from the button click event?
EDIT: custom button class suggestion after feedback comment (not the same suggestion as above)
class MyButton : Button
{
private Type m_TYpe;
private object m_Object;
public object Object
{
get { return m_Object; }
set { m_Object = value; }
}
public Type TYpe
{
get { return m_TYpe; }
set { m_TYpe = value; }
}
}
Button1Click(object sender, EventArgs args)
{
MyButton mb = (sender as MyButton);
//then you can access Mb.Type
//and Mb.object
}
这篇关于我如何传递变量到一个按钮事件的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!