Link to home
Start Free TrialLog in
Avatar of canuckconsulting
canuckconsultingFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How to launch and focus a form from another's when launched

I have a .Net 4 Windows Forms project with Visual Studio 2010 using c#.

I have a FormProduct and FormSearch.  When I launch FormProduct I want it to show FormSearch and for the focus to be on FormSearch.  I have tried this in the constructor of FormProduct but while FormSearch is displayed the focus goes to FormProduct.  I suspect I am opening the form too early but I'm not sure which event to use instead.

Once opened how can I test to be sure the form is still open?  I keep a reference to the form butI don't know how to check to see if the user has closed it.
Avatar of momonana
momonana
Flag of Egypt image

put in the constructor this line to open anther window and bring it to the front

Mohammed Naguib
Thanks
Sorry but the code didn't attached well  

put in the constructor this line to open anther window and bring it to the front

//--------------------------------------------------------
Application.OpenForms["FormSearch "].BringToFront();
//--------------------------------------------------------
Mohammed Naguib
Thanks    
Avatar of canuckconsulting

ASKER

This did not work for me.  Here is the code:

public FormProduct()
        {
            _formSearch = new FormSearch(this);
            _formSearch.Show();
            Application.OpenForms[_formSearch.Name].BringToFront();
            InitializeComponent();
        }

Open in new window

There is the full code of the two forms
the key here is to define every form to the another one

try it and it will work
I'm Sure  
//this is the code for FormProduct 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class FormProduct : Form
    {
        private FormSearch _formSearch;
        

        private void FormProduct_Load(object sender, EventArgs e)
        {
            

        }

        public FormProduct()
        {
            _formSearch = new FormSearch(this);
            _formSearch.Show();
            Application.OpenForms[_formSearch.Name].BringToFront();
            InitializeComponent();
        }
          
    }
}

Open in new window

//this is the code for FormSearch

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class FormSearch : Form
    {
        private FormProduct formProduct;

        public FormSearch()
        {
            InitializeComponent();
        }

        public FormSearch(FormProduct formProduct)
        {
            // TODO: Complete member initialization
            this.formProduct = formProduct;
        }
    }
}

Open in new window

I created a new project and this does not work.  I have uploaded the project here:

http://www.sendspace.com/file/oj192v

ASKER CERTIFIED SOLUTION
Avatar of momonana
momonana
Flag of Egypt image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
do not forget to evaluate my answers i need points so much

thanks
Hi

To show the form search in front of formProduct please try the below code in the constructor of the FormProduct

public partial class FormProduct : Form
    {
        public FormProduct()
        {
            InitializeComponent();
            FormSearch forms = new FormSearch();
            forms.Show();
            forms.TopMost = true;        
        }
    }
If your design permits then why not show the FormSearch as modal dialog instead?

In the Shown event of FormProduct put,

FormSearch frm = new FormSearch();
frm.ShowDialog();

With model dialog you can even utitlize its DialogResult to know exactly how the search form closed like OK or Cancel.
as like i said to you canuckconsulting make everything we did lately in the project and
in Shown event of formproduct put this line

this.SendToBack();

it will send the formproduct behind the FormSearch

Thanks
Mohamed Naguib
In the end I simply did as follows in the Shown event.  Thanks for your help.

        private void FormProduct_Shown(object sender, EventArgs e)
        {
            _formSearch.Show();
        }