将参数从一种形式传递到button_Click事件 [英] passing parameters from one form to button_Click event

查看:70
本文介绍了将参数从一种形式传递到button_Click事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以....我有一个方法,该方法具有通过另一种形式传递的几个参数.当我在initializeComponent()下调用该方法时,该方法将起作用.像这样:

So....I have a method with several parameters passed on by another form. The method is working when I call on it under initializeComponent(); like this:

public CompareYearsByMonth(string Year, string Month, string Ship, Color color, string ConString)
{
    InitializeComponent();

    exportToExcel(mY, mM, mD, mS, mK, mC);
}



但是,当我尝试通过button_Click事件或后台工作程序调用它时,如下所示:



But when I try to call on it from a button_Click event or background worker like this:

private void exportButton_Click(object sender, EventArgs e)
{
    exportToExcel(mY, mM, mD, mS, mK, mC);
}

我得到一个错误:名称"在当前上下文中不存在.

参数是从其他形式传递过来的,如下所示:

I get the error: The name '' '' does not exist in the current context.

The parameters are passed on from the other form like this:

private void chart1_MouseDoubleClick(object sender, MouseEventArgs e)
{
    try
    {
        HitTestResult result = chart1.HitTest(e.X, e.Y);
        int x = int.Parse(chart1.Series[result.Series.Name.ToString()].Points[result.PointIndex].XValue.ToString());
        string Y = result.Series.Name.ToString();
        string M = chart1.Series[result.Series.Name.ToString()].Points[result.PointIndex].XValue.ToString();
        string S = listBox1.SelectedItem.ToString().Trim();

        CompareYearsByMonth frm2 = new CompareYearsByMonth(Y, M, S,       chart1.Series[result.Series.Name.ToString()].Color, connectionString);
        frm2.Text = "AVG Year: " + Y + " Month: " + M + " Ship: " + S.Substring(S.IndexOf(" "), S.Length - S.IndexOf(" "));
        frm2.Show();
    }
    catch
    {
    }
}



我该怎么做呢?我需要该按钮才能正常工作,我甚至想在backgroundworker



How am I supposed to do this right? I need that button to work and I would even want to use the same parameters in backgroundworker

推荐答案

中使用相同的参数.被束缚住),
但是我猜mY,mM等是CompareYearsByMonth类中的字段或属性.是这样,那么我怀疑您应该保存在构造函数中传递给它们的值,以便以后在尝试在类按钮单击处理程序中使用它们时,它们不会为空.
It''s difficult to be sure from that code (nothing appears to be tied to anything else),
but I am guessing that mY, mM and so forth are either fields or properties within the CompareYearsByMonth class. Is so, then I suspect that you should be saving the values you are passed in the constructor to them so they aren''t blank when you later try to use them in the classes button click handler.


hiii
当我从事项目工作时,我遇到了同样的问题,我通过使用一个globaldata类解决了该问题.
我创建了一个GlobalData类,并以一种或多种形式声明了所需的变量.当您需要从任何类调用该变量的set属性时,需要调用该类的get属性.

hiii
When i was working on project ,i had same problem ,i solved that problem by using one globaldata class .
I created one GlobalData Class and declare variables which i need on one or more forms .When you need call set property of that variable from any class ,when you need call get property of that class.

In GlobalData Class

class Globaldata
    {
        public static string ConnectionString ="Dsn=Activity;server=172.16.2.35;";
        public static string SQLQuery { get; set; }       
    }


From another class or form

GlobalData.SQLQuery="select * from table"

FROM any other form you can access SQLQuery like

SqlCommand cmd=new SqlCommand (GlobalData.SQLQuery,con);



希望对您有帮助..答复



I hope this will helpful for you..Reply


这篇关于将参数从一种形式传递到button_Click事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆