Sign in to follow this  
Followers 0
JohnOne

Windows Form TopMost C# [Solved]

9 posts in this topic

#1 ·  Posted (edited)

I'm having trouble keeping my form on top.

The relevant code below, except it simply does not stay on top, and after a search all I can find as a solution

is importing user32.dll (not tried it) but I'd sooner not have to.

Any pro tips?

EDIT: solved

private void InitializeComponent()
        {
            this.SuspendLayout();
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.SystemColors.Window;
            this.ClientSize = new System.Drawing.Size(231, 164);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "Form1";
            this.ShowIcon = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
            this.Text = "";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
            this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
            this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
            this.ResumeLayout(false);
            this.TopMost = true;
        }
Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites



Setting Form.TopMost is enough. What you may be experiencing is other windows also marked as top most or you have other windows being displayed as children.

Share this post


Link to post
Share on other sites

I nothing I can think of running with topmost property/attribute.

Just browser.

Unless of course my form (the only one in the project) is a child of visual studio, but I've tried release as well as debug.

Does an app in .net require admin rights to stay on top?


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

hmmm.

seems TopMost means TopMost in more than one sense.

If I move the statement to the top...

this.TopMost = true;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

...It's all gravy.

Cheers.

Solved.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

this.ResumeLayout(false);
this.TopMost = true;

This order is wrong. You set all properties first, then you do ResumeLayout call.

You aren't editing the file that says "This code is auto generated and will be overwritten", right?

Share this post


Link to post
Share on other sites

I don't see that, I was editing the region...

"#region Windows Form Designer generated code"

For the record, I added the Topmost from withing the properties tab in VS first, but it never worked

and that's why I began to fart around in there.

And also to add "FormBorderStyle", as I could not find anywhere in properties to do that.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

You must be using an older version of Visual Studio. Newer versions actually hide that stuff in a second file to keep people from messing with it unless they know how to find it.

Share this post


Link to post
Share on other sites

VS2010 I'm using.

But I could not find a way to get the border style right, all I found was padding which I set to 0.

I'm not, as you can tell, all that clever with VS so if you have any tips to offer I'd be mighty grateful.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Border style should just be in the form properties. Probably labled FormBorderStyle.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0