简体   繁体   中英

Set returned value from method as public string

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.

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!

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM