Results 1 to 13 of 13

Thread: Simple C# Question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Posts
    153

    Question Simple C# Question

    Hi Guys,

    I am calling only one instance of Form2 when I click on the datagrid on the Form1. The Form2 appears but when the focus is lost for the Form2 I cannot see Form2 anymore. No idea where it has gone!!

    As I am calling only one instance of the Form2 clicking any number of times on the datagrid never brings up the Form2.

    Any ideas?
    there r no alternatives 4 hardwork.

  2. #2
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: Simple C# Question

    Can't say anything other than, Form2 is hiding behind Form1, without seeing some code.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Posts
    153

    Re: Simple C# Question

    Hi DeadEyes,

    Thanks for an early reply.

    Sorry, I am unable to provide the code at the moment as I am not at that Site but Form1 Calls Form2 as only 1 instance. This is an MDI application and Form2 is not called from MDI menus but called only from Form1 which has MDI form as the MDIParent.

    Any sample code please help.
    there r no alternatives 4 hardwork.

  4. #4
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    Re: Simple C# Question

    is form2 designed to be shown in the taskbar? how are you instantiating and showing form2?

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Posts
    153

    Post Re: Simple C# Question

    HI Andy,

    Form2 is not designed to be shown in Taskbar.

    I have taken an example from C# forums in other site to create a bool variable
    as

    Code:
    public static bool IsOpen = false;
    and

    when the Form2 is being opened from Form1
    the code is as below. When the user clicks on the product id on the datagrid it would bring up the Form2

    Code:
    private void dgProducts_MouseUp(object sender, MouseEventArgs e)
    		{
    			System.Drawing.Point pt = new Point(e.X, e.Y); 
    			DataGrid.HitTestInfo hti = dgProducts.HitTest(pt); 
    			if(hti.Type == DataGrid.HitTestType.Cell) 
    			{ 
    				dgProducts.CurrentCell = new DataGridCell(hti.Row, hti.Column); 
    				dgProducts.Select(hti.Row); 
    			 
    			sProductId = dgProducts[hti.Row,0].ToString();
    
    			if (sProdId != "")
    			{
    				if (!Form2.IsOpen)
    				{
    					Form2 frm2= new Form2();
    					frm2.sProdId = this.sProductId;
    					frm2.Show();
    				}
    			}
    			}
    
    		}
    The instance of Form2 shows up but when the focus is lost I cannot find the Form2 anywhere!
    there r no alternatives 4 hardwork.

  6. #6
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    Re: Simple C# Question

    Could you also post the code for form2? Specifically the section InitializeComponents, and Form2_Load.

    that's where I'd start looking. It seems to be a problem with form2.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Simple C# Question

    How about
    Code:
    if (Form2.IsOpen)
    {
        myForm2Instance.Activate();
    }
    else
    {
        // Create a new instance and display.
    }
    Last edited by jmcilhinney; Jun 21st, 2005 at 07:40 PM. Reason: Remove VB syntax

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Posts
    153

    Arrow Re: Simple C# Question

    jmcilhinney,

    In your code
    Code:
    if (Form2.IsOpen)
    {
        myForm2Instance.Activate();
    }
    else
    {
        // Create a new instance and display.
    }
    Where will the object myForm2Instance first be created / declared?


    Edit: Added [code][/code] tags for clairty. - Hack
    Last edited by Hack; Jun 22nd, 2005 at 10:04 AM.
    there r no alternatives 4 hardwork.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Simple C# Question

    Form2 doesn't need any code. This should work "as is" as long as the instance of Form2 has not had Hide called on it, its Visible property set to False or had Close called on it. In the previous code I posted, I'd suggest putting a breakpoint on the line that activates the existing instance of Form2 and use the Locals or Watch window to examine all the properties of that object. I'd be looking specifically at Visible, Location, WindowState, IsDisposed and anything else that might cause it to not be seen.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Posts
    153

    Re: Simple C# Question

    jmcilhinney,

    I dont know where I have lost the code i have used before.

    Can you please send me sample code both activating and opening only one instance of the Form2 from Form1 if you dont mind.

    In your code

    if (Form2.IsOpen)
    {
    myForm2Instance.Activate();
    }
    else
    {
    // Create a new instance and display.
    }

    Where will the object myForm2Instance first be created / declared?
    Last edited by Venkrishna; Jun 22nd, 2005 at 09:22 AM.
    there r no alternatives 4 hardwork.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Simple C# Question

    Here's what I would do. I'd forget that IsOpen variable altogether. Declare a variable of type Form2 as a member of Form1 and use it like this:
    Code:
    private Form2 myForm2;
    
    private void ShowForm2()
    {
        if (this.myForm2 == null || this.myForm2.IsDisposed)
        {
            // Create a new instance of Form2.
            this.myForm2 = new Form2();
            this.myForm2.Show();
        }
        else
        {
            // If myForm2.Visible has been set to False then change that here.
            // Activate the existing instance.
            this.myForm2.Activate();
        } 
    }
    I hope my syntax is all correct. I do most of my work in VB and sometimes some VB syntax can sneek in when I don't copy and paste. Note that this code is all in Form1.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Posts
    153

    Resolved Re: Simple C# Question [Resolved]

    Hello people,

    Atlast I have found some info from

    http://msdn.microsoft.com/smartclien...q/default.aspx

    In this page there is a topic called
    Creating Forms and Controls

    and there is a topic

    How do I ensure that no more than one instance of modeless dialog is created or open at a time?

    This solved my problem and I am quite happy with it.
    there r no alternatives 4 hardwork.

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Simple C# Question

    This solution will certainly work but using owned forms does have other implications. I'd suggest reading the help topic for the "Form.OwnedForms Property" just to check whether you find these implications good, bad or indifferent.

    Edit:
    Note that the article you cite talks about modeless dialogues rather than just windows in general. The VS.NET Find and Replace dialogue is an example of a modeless dialogue. It displays the behaviour of an owned form.
    Last edited by jmcilhinney; Jun 23rd, 2005 at 05:58 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width