Make Form always on top and can't Focus off

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 11765 Views - Last Post: 18 July 2009 - 02:12 PM Rate Topic: -----

#1 Shiggins  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 09-December 08

Make Form always on top and can't Focus off

Post icon  Posted 17 July 2009 - 07:10 AM

Im done with my application im making and now just tidying up little bits before i release it for beta but one problem ive came across that i can't work out or find on the internet to fix is:

I Have a About Form and a Change Log form which get selected from the Help Menu.

How can i make it so when i click on the forms it locks them so people cant click on the main form and have to click ok to close the form before they can use the main form again rather then them clicking on the main form and hiding the about and change log form.

If you can just give me a link to somewhere where i can read about it. would be great not asking to do my code unless it just 1 line.

Regards

Richard

Is This A Good Question/Topic? 0
  • +

Replies To: Make Form always on top and can't Focus off

#2 Bocard  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 222
  • Joined: 24-September 08

Re: Make Form always on top and can't Focus off

Posted 17 July 2009 - 07:48 AM

to be on top of other app you can put the code below in your form load event
 this.TopMost = true;

not sure about the focus thing

if i helped you can thank me by clicking the button below saying that this post was helpful for you :)

This post has been edited by Bocard: 17 July 2009 - 07:49 AM

Was This Post Helpful? 0
  • +
  • -

#3 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: Make Form always on top and can't Focus off

Posted 17 July 2009 - 09:58 AM

You can use ShowDialog() for showing the form and checking user's input:

MyForm frm = new MyForm();
if (frm.ShowDialog() == DialogResults.OK)
	// continue to main form
else
	// close application or something



MyForm:
	public partial class MyForm : Form {
		public MyForm() {
			InitializeComponent();

			btnOK.DialogResult = DialogResult.OK;
		}
	}



It's important to set the DialogResult property of button so the main form knows what DialogResult is returned.
Was This Post Helpful? 0
  • +
  • -

#4 Bocard  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 222
  • Joined: 24-September 08

Re: Make Form always on top and can't Focus off

Posted 17 July 2009 - 10:00 AM

i don't think this solves the "form which can't lose focus" problem..
Was This Post Helpful? 0
  • +
  • -

#5 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: Make Form always on top and can't Focus off

Posted 17 July 2009 - 11:53 AM

Well, it solves it within the current application: you can't click main window if there is another window shown as dialog. And that's what OP was looking for: not being able to focus on main form.
Was This Post Helpful? 0
  • +
  • -

#6 Bocard  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 222
  • Joined: 24-September 08

Re: Make Form always on top and can't Focus off

Posted 17 July 2009 - 11:55 AM

View Postjanne_panne, on 17 Jul, 2009 - 07:53 PM, said:

Well, it solves it within the current application: you can't click main window if there is another window shown as dialog. And that's what OP was looking for: not being able to focus on main form.

oh, sorry. i thought he wanted not to be able to lose focus at all from his form.
Was This Post Helpful? 0
  • +
  • -

#7 Shiggins  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 09-December 08

Re: Make Form always on top and can't Focus off

Posted 17 July 2009 - 12:35 PM

i dont want it be be able to lose focus like on certian applications when you click say the about form and you cant do anything else on the main form till you click ok on the about form. ive got the form popping up and closing you btnOK to close the window. but someone can just load up the about form and then click back on the main form and continue what they were doing which makes the about form hide behind the main form
Was This Post Helpful? 0
  • +
  • -

#8 firexware  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 57
  • Joined: 06-July 09

Re: Make Form always on top and can't Focus off

Posted 17 July 2009 - 01:01 PM

there must be an event that fires when the form loses focus.
just put this.TopMost = true; in that event
if there is no event you can make another thread run a loop and set the form topmost, and pause for a few miliseconds so your not using a billion cpu
if this.TopMost = true doesnt work, use the SetForegroundWindow api

This post has been edited by firexware: 17 July 2009 - 01:02 PM

Was This Post Helpful? 0
  • +
  • -

#9 Steeeve  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 58
  • Joined: 03-December 08

Re: Make Form always on top and can't Focus off

Posted 17 July 2009 - 01:01 PM

If you want it to stay on top all the time, then go to the properties bar on the form designer and change "TopMost" to True.

As for not being able to click anything else, I think putting everything in a DialogBox would be easiest, but that's just one opinion.
Was This Post Helpful? 0
  • +
  • -

#10 dkirkland  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-April 07

Re: Make Form always on top and can't Focus off

Posted 17 July 2009 - 01:15 PM

I think you need a modal dialog box.
Was This Post Helpful? 0
  • +
  • -

#11 Bocard  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 222
  • Joined: 24-September 08

Re: Make Form always on top and can't Focus off

Posted 17 July 2009 - 02:56 PM

is it me or are you guys just repeating what other users said before?

might be me it's past midnight here..00:56..i'm really tired
Was This Post Helpful? 0
  • +
  • -

#12 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Make Form always on top and can't Focus off

Posted 17 July 2009 - 05:21 PM

Just to clarify. If you want a form, like the OpenDialogForm that when it opens the user can not switch back to the original window, all you have to do is what janne_panne said. You use ShowDialog instead of Show. ShowDialog creates a modal form for the application. The user has to close it before they can get back to working in the original window.
Was This Post Helpful? 1

#13 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Make Form always on top and can't Focus off

Posted 18 July 2009 - 05:21 AM

View PostBocard, on 17 Jul, 2009 - 12:00 PM, said:

i don't think this solves the "form which can't lose focus" problem..


a form which is shown using ShowDialog() cannot lose focus unless it's closed. So that fits the description that the OP wants.
Was This Post Helpful? 0
  • +
  • -

#14 Bocard  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 222
  • Joined: 24-September 08

Re: Make Form always on top and can't Focus off

Posted 18 July 2009 - 05:52 AM

sry, my first language is not english, what i thought he needed was so nothing can be focused unless the user does sth in that form. not just inside his app.
Was This Post Helpful? 0
  • +
  • -

#15 sknake  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 32
  • Joined: 15-July 09

Re: Make Form always on top and can't Focus off

Posted 18 July 2009 - 05:56 AM

For my about screens I use .ShowDialog() and call this.BringToFront() when the form is shown so it blinks the application window in the task bar. It seems like you're trying to make this dialog very noticeable so perhaps that will help
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2