Solved

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

Posted on 2011-09-14
11
315 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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
 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
2 questions 10 25
Where did System.Data.Objects go? 2 23
Hey!!!! 1 20
transaction in asp.net, sql server 6 31
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

806 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