Click here to Skip to main content
16,012,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Form1 where i open a new Form2 with button click.
Every time i click that button in Form1, i would like to pass a
string value to Form2 code.
How can this be done?
I have tried to study this, and tried several examples, but the examples i found was not quite the same.
Please help, and thanks in advance.

Simo

What I have tried:

Tried some examples from the net. Tried to figure it out myself, but have no clue...
Posted
Updated 9-Mar-16 23:54pm
Comments
asplover1 10-Mar-16 5:34am    
can you please show your code what you are using to open the form 2
Member 12302635 10-Mar-16 5:46am    
private void button4_Click(object sender, Eventargs e)
{
Form2 showG = new Form2();
showG.Show();
}

like this, so nothing much here :)
Member 12302635 10-Mar-16 5:50am    
I just want to open another window to show "results graphics" in a picture.
I need the string with the path and filename to be sent over, which i have ready on Form1.
If there is another way to do this (i.e. usercontrol or whatever, please point it out...)

Hello ,
There are several example available in Google .
try these
1. Passing Data Between Forms
2. How to pass the Value one form to another form in windows form

Thanks
 
Share this answer
 
Comments
Member 12302635 10-Mar-16 6:09am    
Went through the Google and some other paths, but it wasn't what i looked for.
I know there's good examples and blog around the net like this one:
http://geek-goddess-bonnie.blogspot.com/2011/01/passing-data-between-forms.html
But this was just a little detail in a bigger project, that i wanted to find just simplest thisng. And yes i'm a newbie, and that's why i sometimes need help from the net :)
Animesh Datta 10-Mar-16 6:30am    
Never forget Google your friend . If you search properly then you should get right answer .not an issue but The above links explain enough for your basic requirement .
Create a constructor for the second form which accepts a string parameter:
C#
public Form2(string s)
   {
   // Do something with your string
   ...
   }
Then when you construct the form instance, pass your value:
C#
Form2 myForm2Instance = new Form2("Hello World!");
myForm2Instance.Show();
 
Share this answer
 
Comments
Member 12302635 10-Mar-16 6:06am    
Thanks for this solution. All other found from Google, or other sources were way more complicated for this simple thing.
Member 12302635 10-Mar-16 6:11am    
And yes, i'm a newbie. I tried "something" like this and quit too quickly.. :)
OriginalGriff 10-Mar-16 6:20am    
:laugh:
We're all guilty of that from time to time!
Member 12302635 10-Mar-16 6:55am    
One more newbie question, why can't i use a string like this:
Form2 myForm2Instance = new Form2(sMyfilepath);
i can write it like that, but its always null on the other side??
OriginalGriff 10-Mar-16 7:19am    
You can - and it'll work fine.
string myString = "Hello World!";
...
Form2 myForm2Instance = new Form2(myString);
myForm2Instance.Show();
It'll only be null if the string variable you are passing has not been assigned a value.
Put a breakpoint on the constructor, and see what the debugger says! (You do know how to use the debugger, yes?)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900