Set the return value from the method as a public string

I am trying to pass a value between forms - in this case, the result of the selected cell in the DataGridView.

In my main form, I get the value using the public line 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 would have to call it from my Form2, right?

In Form2, I have this:

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

I know that working with a cross form works: if I replaced GetCaseID () with a plain old line, it will appear as expected in Form2.

Could this be due to the fact that declaring dgCases publicly available?

Thanks.

+4
source share
4 answers

In button1_Click you create a new instance of the fmHome class. This is an instance of another of the fmHome instance that created it, so it does not have a highlighted line in dgCases . Calling GetCaseID() on this instance will not return what you expect.

Your button1_Click handler should have a way to call GetCaseID() in the form that opens. A very simple way is to add such a property to Form2 :

 public fmHome fmHomeParent { get; set; } 

Then, when you open your instance of Form2 , do the following:

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

So, in your button1_Click handler button1_Click you can access this 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!

+4
source

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

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

Note: the way you mix view and data will result in unreachable code and is not good practice ... but I suppose you are dealing with legacy code?

+1
source

Creating a new instance, as you do, will not get any meaningful value.

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

0
source

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 that contains all the information necessary for your application, and then fill it in where necessary, and get the data from it in a different form, without linking them both. But I do not know if this is the best solution.

0
source

Source: https://habr.com/ru/post/1332345/


All Articles