Solved

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

Posted on 2011-09-14
11
313 Views
Last Modified: 2013-12-17
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.
0
Comment
Question by:canuckconsulting
11 Comments
 

Expert Comment

by:momonana
ID: 36535025
put in the constructor this line to open anther window and bring it to the front

Mohammed Naguib
Thanks
0
 

Expert Comment

by:momonana
ID: 36535034
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    
0
 

Author Comment

by:canuckconsulting
ID: 36535061
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

0
 

Expert Comment

by:momonana
ID: 36535305
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

0
 

Author Comment

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

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

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Accepted Solution

by:
momonana earned 500 total points
ID: 36535441
the final solution

is in the event (Shown)  of the FormProduct put this






private void FormProduct_Shown(object sender, EventArgs e)
        {
 this.SendToBack();
        }

Open in new window

0
 

Expert Comment

by:momonana
ID: 36535705
do not forget to evaluate my answers i need points so much

thanks
0
 
LVL 4

Expert Comment

by:AnuTiji
ID: 36535748
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;        
        }
    }
0
 
LVL 8

Expert Comment

by:jagrut_patel
ID: 36535764
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.
0
 

Expert Comment

by:momonana
ID: 36535910
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
0
 

Author Closing Comment

by:canuckconsulting
ID: 36536988
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();
        }
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now