将方法的返回值设置为公共字符串

发布于 2024-10-08 13:57:06 字数 973 浏览 5 评论 0原文

我试图在表单之间传递一个值 - 在本例中,是 DataGridView 中突出显示的单元格的结果。

在我的主窗体中,我使用公共字符串方法获取值:

    public string GetCaseID()
    {
        int i;
        i = dgCases.SelectedCells[0].RowIndex;
        string caseid = dgCases.Rows[i].Cells[1].Value.ToString();
        string version = dgCases.Rows[i].Cells[2].Value.ToString();
        return version + "_c" + caseid;
    }

    //Form2 is launched
    private void btnEvLvlUserSelect_Click(object sender, EventArgs e)
    {
        Form2 form2= new Form2();
        form2.ShowDialog();
    }

由于 GetCaseID() 被声明为公共字符串,所以我应该能够从 Form2 中调用它,对吧?

在 Form2 中,我只有这个:

    private void button1_Click(object sender, EventArgs e)
    {
        //Take selected case information
        fmHome fmhome = new fmHome();
        textBox1.Text = fmhome.GetCaseID();
    }

我知道跨表单通信是有效的:如果我用普通的旧字符串替换 GetCaseID(),它会按预期在 Form2 中显示。

这是否与必须将 dgCases 声明为公开有关?

谢谢。

I'm trying to pass a value between forms - in this case, the result of a highlighted cell in a DataGridView.

In my main form, I get the value using a public string method:

    public string GetCaseID()
    {
        int i;
        i = dgCases.SelectedCells[0].RowIndex;
        string caseid = dgCases.Rows[i].Cells[1].Value.ToString();
        string version = dgCases.Rows[i].Cells[2].Value.ToString();
        return version + "_c" + caseid;
    }

    //Form2 is launched
    private void btnEvLvlUserSelect_Click(object sender, EventArgs e)
    {
        Form2 form2= new Form2();
        form2.ShowDialog();
    }

Since GetCaseID() is declared as a public string, I should be able to call it from my Form2, right?

In Form2, I just have this:

    private void button1_Click(object sender, EventArgs e)
    {
        //Take selected case information
        fmHome fmhome = new fmHome();
        textBox1.Text = fmhome.GetCaseID();
    }

I know the cross-form communication works: If I replace GetCaseID() with a plain old string, it displays as expected in Form2.

Could it be something to do with having to declare the dgCases as public as well?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏尔 2024-10-15 13:57:06

button1_Click 中,您将创建 fmHome 类的新实例。这是一个与创建它的 fmHome 实例不同的实例,因此它在 dgCases 中没有选定的行。在此实例上调用 GetCaseID() 将不会返回您期望的结果。

您的 button1_Click 处理程序需要有一种方法可以在打开它的表单上调用 GetCaseID()。一个非常基本的方法是在 Form2 上添加这样的属性:

public fmHome fmHomeParent { get; set; }

然后,当您打开 Form2 实例时,执行以下操作:

private void btnEvLvlUserSelect_Click(object sender, EventArgs e)
{
    Form2 form2= new Form2();
    form2.fmHomeParent = this;
    form2.ShowDialog();
}

所以在您的 button1_Click< /code> 处理程序,您可以访问该实例而不是创建一个新实例:

private void button1_Click(object sender, EventArgs e)
{
    //Take selected case information
    textBox1.Text = fmHomeParent.GetCaseID();
}

希望这会有所帮助!

In button1_Click, you're creating a new instance of the fmHome class. This is a different instance from the instance of fmHome that created it, so it doesn't have a selected row in dgCases. Calling GetCaseID() on this instance will not return what you're expecting.

Your button1_Click handler needs to have a way to call GetCaseID() on the form that opened it. A very basic way would be to add a property like this on Form2:

public fmHome fmHomeParent { get; set; }

Then, when you open your instance of Form2, do this:

private void btnEvLvlUserSelect_Click(object sender, EventArgs e)
{
    Form2 form2= new Form2();
    form2.fmHomeParent = this;
    form2.ShowDialog();
}

So in your button1_Click handler, you can access that instance instead of creating a new one:

private void button1_Click(object sender, EventArgs e)
{
    //Take selected case information
    textBox1.Text = fmHomeParent.GetCaseID();
}

Hope this helps!

半暖夏伤 2024-10-15 13:57:06

您在button1_Click 中创建fmHome 的新实例,因此它不包含原始数据网格。

您可以将数据网格的实例作为参数传递给 Form2 构造函数。

注意:混合视图和数据的方式将导致代码无法维护,这不是一个好的做法......但我假设您正在处理遗留代码?

You create a new instance of fmHome in button1_Click, so it does not contain your original datagrid.

You could pass the instance of the datagrid as a parameter to the Form2 constructor.

Note: the way you mix view and data will lead to unmaintainable code and is not a good practise... but I assume you're dealing with legacy code?

拿命拼未来 2024-10-15 13:57:06

像您一样创建新实例不会获得任何有意义的价值。

相反,请确保 Form2 可以获取对原始 fmHome 实例的引用。最常见的模式是添加一个 fmHome 类型的(私有)Form2 成员变量,并将其设置为 Form2 构造函数的一部分,要求创建者将其传入(在您的情况下,在 btnEvLvlUserSelect_Click 中)。然后您可以在 Form2.button1_Click() 方法中使用该成员,而不是创建一个新的 (看不见的)空形式。

Creating a new instance as you are doing won't get any meaningful value.

Instead, make sure Form2 can get a reference to the original fmHome instance. The commonest pattern is to add a (private) Form2 member variable of type fmHome, and set it as part of the Form2 constructor(s), requiring the creator to pass it in (in your case, in btnEvLvlUserSelect_Click. Then you can use that member in your Form2.button1_Click() method instead of creating a new (invisible) empty form.

秋凉 2024-10-15 13:57:06

您还可以执行以下操作:

//Form2 is launched
private void btnEvLvlUserSelect_Click(object sender, EventArgs e)
{
    Form2 form2= new Form2(this.GetCaseID());
    form2.ShowDialog();
}

然后在 Form2 中:

public partial class PlayerInfo : Form
{
    string caseID;
    public Form2(string fmHomeCaseID)
    {
        caseID = fmHOmeCaseID;
    }


    // Button Click in your second form
    private void button1_Click(object sender, EventArgs e)
    {
        textBox1.Text = caseID;
    }
}

您还可以创建一个类,其中包含您的申请所需的所有信息,然后在需要的地方填写它,并在另一个表单上从中获取数据无需连接它们两个。但我不知道这是否是最好的解决方案。

What you can also do is the following:

//Form2 is launched
private void btnEvLvlUserSelect_Click(object sender, EventArgs e)
{
    Form2 form2= new Form2(this.GetCaseID());
    form2.ShowDialog();
}

Then in Form2:

public partial class PlayerInfo : Form
{
    string caseID;
    public Form2(string fmHomeCaseID)
    {
        caseID = fmHOmeCaseID;
    }


    // Button Click in your second form
    private void button1_Click(object sender, EventArgs e)
    {
        textBox1.Text = caseID;
    }
}

What you can also do is make a class which holds all the information that is required for your application and then fill it up where needed and get the data from it on the other form without having to connect both of them. But I don't know if this is the best solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文