Click here to Skip to main content
16,012,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Form1 and form2 a running when program start. On form1 i have pictureBox1 and i want to change location of form2 (on center of the screen) when user click on pictureBox1. How to reference two form correctly?

What I have tried:

form2 is already running in location outside of screen (deliberately) and i need it to return on centar of screen when user click pictureBox1(on form1)
Posted
Updated 30-Apr-16 7:32am

Depends on how you create the two forms.
Since you have called them "form1" and "form2" the chances are that you created the form2 instance in form1 code, and used form2.Show to display it.
In which case it's pretty simple: use the instance of form2 that you created, by stroring it in a class level variable in form1:
C#
private Form2 form2 = null;
...
   form2 = new Form2();
   form2.Show();
...
Then, in your PictureBox.Click event handler:
C#
void myPictureBox_Click(object sender, EventArgs e)
    {
    Rectangle screen = Screen.PrimaryScreen.WorkingArea;
    form2.Location = new Point((screen.Width - form2.Width) / 2, (screen.Height - form2.Height) / 2);
    }


[edit]Spelling mistake[/edit]
 
Share this answer
 
v2
Comments
BillWoodruff 1-May-16 3:50am    
You should edit your original post, and add this information. How can we help you if you don't give us a full description of what you are doing ? We are not psychics who can detect the presence of "form4" by remote sensing :)
Please see my article written specially to answer all similar questions: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows[^].

—SA
 
Share this answer
 

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