Parameter Passing between windows forms, C#

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

I have two forms. From form one I have a listbox that when double
clicked I get the selected value (string) and store in a variable. I
parse the string to get the first 12 characters and store it in a
variable to query a sql db. I set break points to see the data as it
is passed from form1 and when calling the function from form2 to get
the variable, its contents are blank.

I believe what I'm looking for is some method to access an instance a
specific instance and not create a new one that seems to wipe
everything out.
 
If I understand you correctly, form2 is used to display
the results of a db query whose parameter is selected
from form1. The query logic is in form2 and you can't
pass the variable. Is this correct?

If so, why don't you just pass the variable from form1 to
form2's function before displaying form2 instead of
trying to retrieve the variable from form1?

Form2 f2 = new Form2();
f2.GetSomeData(variableInQuestion);
f2.ShowDialog();

If that doesn't suit your needs, you could add a Caller
property to form2 and set it as 'this' before showing the
form.

Form2 f2 = new Form2();
f2.Caller = this;
f2.ShowDialog();

Then, in form2...

this.GetSomeData(Caller.variableInQuestion);


Most likely, though, I have misinterpreted your question
and was of no help whatsoever.

Charlie
 
Charlie Williams said:
If I understand you correctly, form2 is used to display
the results of a db query whose parameter is selected
from form1. The query logic is in form2 and you can't
pass the variable. Is this correct?

If so, why don't you just pass the variable from form1 to
form2's function before displaying form2 instead of
trying to retrieve the variable from form1?

Form2 f2 = new Form2();
f2.GetSomeData(variableInQuestion);
f2.ShowDialog();

If that doesn't suit your needs, you could add a Caller
property to form2 and set it as 'this' before showing the
form.

Form2 f2 = new Form2();
f2.Caller = this;
f2.ShowDialog();

Then, in form2...

this.GetSomeData(Caller.variableInQuestion);


Most likely, though, I have misinterpreted your question
and was of no help whatsoever.

Charlie

Charlie,

The first solution worked. I didn't know that I could accomplish it
that way. I was investigating the use of delegates to resolve this.

Thank you again

Chris
 
Back
Top