Solved

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

Posted on 2011-09-14
11
316 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

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.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

820 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