WinForm focus

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 4050 Views - Last Post: 03 January 2012 - 04:46 PM Rate Topic: -----

#1 webwired  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

WinForm focus

Posted 02 January 2012 - 02:29 PM

Hey everyone... So it looks like I've completed all of the operational code of my duplicate file remover application that I was building for practice... There's just one thing though that I can't seem to iron out...

There is the MainForm (Form1), then it opens up a second form with a DataGridView on it ==> after you select the items you want to delete on the DataGridView ==> You click the Delete button ==> After you click the Delete button and it does its job, a MessageBox pops up to let you know that it has completed its tasks...

The problem is that after I click OK on the popup MessageBox, and after it Closes the form with the DataGridView on it, I can't get it to bring the MainForm back to focus, it gets hidden away on the taskbar...

I've tried these, but they didn't work either...
MainForm myMainForm = new MainForm();
myMainForm.Activate();
myMainForm.BringToFront();



Is This A Good Question/Topic? 0
  • +

Replies To: WinForm focus

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,843
  • Joined: 02-June 10

Re: WinForm focus

Posted 02 January 2012 - 02:52 PM

Have form 1 subscribe to form2's .FormClosed event.

When that is raised, then have form1 bring itself into focus. This puts the responsibility where it belongs, on to Form1
Was This Post Helpful? 1
  • +
  • -

#3 webwired  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

Re: WinForm focus

Posted 02 January 2012 - 03:05 PM

That's some new stuff to me, so it'll take me a while to figure it out, but I'll post back the code once I have it in the bag... (I hope)
Was This Post Helpful? 0
  • +
  • -

#4 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 181
  • View blog
  • Posts: 777
  • Joined: 27-October 11

Re: WinForm focus

Posted 02 January 2012 - 03:26 PM

A hint to get you pointed in the right way:

Quick and easy custom events

Oh and use the .Show() method to show the form and when you need to hide it use .Hide()... :D
Was This Post Helpful? 1
  • +
  • -

#5 webwired  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

Re: WinForm focus

Posted 02 January 2012 - 03:40 PM

Yeah, that looks like some good stuff... When it comes to Event Handlers, especially Custom ones, especially in C#, I can use all the material I can find.
Was This Post Helpful? 0
  • +
  • -

#6 webwired  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

Re: WinForm focus

Posted 02 January 2012 - 04:25 PM

So get this, I'm working on this... (Mind you, my almost complete application) ... when all of a sudden, I hit the Debug button to try out my latest code adjustment and some crazy Exception regarding the Database Entity makes everything come to a screeching halt...

I tried undoing everything that I had done, (with the whole Event Closing thing) even though I didn't do anything with LINQ or the Database Entity... but it keeps throwing this very unhelpful error...

On this line:
public Database1Entities() : base("name=Database1Entities", "Database1Entities")

I get this error now when I attempt to Debug:
An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll

That make any sense to you guys?
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,843
  • Joined: 02-June 10

Re: WinForm focus

Posted 02 January 2012 - 04:40 PM

There is no special event creation. You just use the events that already exist. Just like subscribing to a .Click event of a Button.

Most of what you need to do Visual Studio will even fill in for you:

  • Attached Image
  • Attached Image
  • Attached Image

Was This Post Helpful? 1
  • +
  • -

#8 webwired  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

Re: WinForm focus

Posted 02 January 2012 - 05:30 PM

Well I was able to find out what I had done to create the problem, apparently in tempting fate with messing with Custom Events, I called a function recursively over and over again
Was This Post Helpful? 0
  • +
  • -

#9 webwired  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

Re: WinForm focus

Posted 03 January 2012 - 10:55 AM

I really am trying to get a handle on this Subscribing to Events...

So on my MainForm I put the following:
        public MainForm()
        {
            InitializeComponent();
            SelectionForm newSelectionForm = new SelectionForm();
            newSelectionForm.FormClosed += new FormClosedEventHandler(newSelectionForm.SelectionForm_FormClosed);
        }
        void SelectionForm_FormClosed(object sender, EventArgs e)
        {
            this.Focus();
        }



And on the secondary form I put the following:
        public void SelectionForm_FormClosed(object sender, FormClosedEventArgs e)
        {

        }



Obviously I'm not getting it am I? Please, a pointer back in the right direction?
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,843
  • Joined: 02-June 10

Re: WinForm focus

Posted 03 January 2012 - 11:01 AM

The first part is fine.

The second part is unneeded. You don't have to do anything in the selection form when it closes. Just closing it will raise the "FormClosed" event. That's built-in to the form object.

Again, this is just like subscribing to a button click. When a button is clicked you don't have to go into the buttons code to do anything. You just react to it. Same here. When the form is closed you don't have to go into that form's code. You just react to it, in the Form1, by having form 1 tell itself to take focus.
Was This Post Helpful? 1
  • +
  • -

#11 webwired  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

Re: WinForm focus

Posted 03 January 2012 - 11:08 AM

View PosttlhIn`toq, on 03 January 2012 - 12:01 PM, said:

The first part is fine.

The second part is unneeded. You don't have to do anything in the selection form when it closes. Just closing it will raise the "FormClosed" event. That's built-in to the form object.

Again, this is just like subscribing to a button click. When a button is clicked you don't have to go into the buttons code to do anything. You just react to it. Same here. When the form is closed you don't have to go into that form's code. You just react to it, in the Form1, by having form 1 tell itself to take focus.


Yeah, I didn't see much sense in having an empty Event, but the problem is, if I delete the Event on the secondary form, then on my MainForm, it doesn't see the Event on the secondary form, obviously, therefore won't allow me to put the Event Handler on the MainForm... So I must be doing something wrong, because even when I did have the empty Event Handler on the secondary form, it didn't bring the MainForm back up into focus upon closing...
Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,843
  • Joined: 02-June 10

Re: WinForm focus

Posted 03 January 2012 - 11:12 AM

Give me a few minutes to assemble a small demo project to attach for you
Was This Post Helpful? 1
  • +
  • -

#13 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,843
  • Joined: 02-June 10

Re: WinForm focus

Posted 03 January 2012 - 11:32 AM

Attached Image

Button names should be obvious looking at the code below.

Code for MainForm
Spoiler


code for SecondaryForm
Spoiler

Was This Post Helpful? 2
  • +
  • -

#14 webwired  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

Re: WinForm focus

Posted 03 January 2012 - 11:46 AM

View PosttlhIn`toq, on 03 January 2012 - 12:12 PM, said:

Give me a few minutes to assemble a small demo project to attach for you


Thank you very much for your extended help, I most certainly appreciate your time and patience. Your code example couldn't have been easier to follow and worked perfectly. I definitely learned something and this'll go in my code snippet bank, literally and figuratively.
Was This Post Helpful? 0
  • +
  • -

#15 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 181
  • View blog
  • Posts: 777
  • Joined: 27-October 11

Re: WinForm focus

Posted 03 January 2012 - 12:55 PM

Can you subscribe to the first form's close event in the second ones designer code? :D
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2