Windows Develop Bookmark and Share   
 index > Windows Forms General > Issues with form positioning
 

Issues with form positioning

Greetings,

I'm trying to add a feature to my project's toolbars to allow them to "float"; this is, at user request, the ToolStrip object is removed from its ToolStripContainer, added to a new, empty Form with Text equal to the strip's Text and with FormBorderStyle=FormBorderStyle.SizableToolWindow, then show the form.
The problem I'm facing is to figure out the right value for that form's Location. My goal is to have the strip itself to be exactly where it was before "floating" it; but I can't define directly the strip's location, I need to compute a location for the form such as, after drawing borders and title-bar, the strip ends up in the desired position.
I have tried several combinations of calls to PointToClient and PointToScreen from the ToolStripContainer, the ToolStrip, and the form itself, taking the strip's Location as my starting point, but I haven't managed to get the form appear anywhere close to where I'm trying.

To put the question more explicitly: having a Point p that represents a screen coordinate, how should I compute the Location for a Form so the top-left corner of the form's client rectangle matches p ?

Thanks in advance for any answer and/or suggestions.

Regards,
herenvardo
herenvardo  Thursday, September 10, 2009 2:17 PM
This worked well:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1 {
public partial class ToolStripForm : Form {
private ToolStrip mStrip;
public ToolStripForm(ToolStrip strip) {
InitializeComponent();
mStrip = strip;
this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
}
protected override void OnLoad(EventArgs e) {
Point pos = mStrip.PointToScreen(Point.Empty);
this.Controls.Add(mStrip);
this.ClientSize = mStrip.Size;
Point newpos = mStrip.PointToScreen(Point.Empty);
this.Location = new Point(this.Left + pos.X - newpos.X,
this.Top + pos.Y - newpos.Y);
}
}
}

Usage:

private void toolStripButton2_Click(object sender, EventArgs e) {
new ToolStripForm(toolStrip1).Show();
}


Hans Passant.
  • Marked As Answer byherenvardo Thursday, September 10, 2009 3:30 PM
  •  
nobugz  Thursday, September 10, 2009 3:00 PM
This worked well:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1 {
public partial class ToolStripForm : Form {
private ToolStrip mStrip;
public ToolStripForm(ToolStrip strip) {
InitializeComponent();
mStrip = strip;
this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
}
protected override void OnLoad(EventArgs e) {
Point pos = mStrip.PointToScreen(Point.Empty);
this.Controls.Add(mStrip);
this.ClientSize = mStrip.Size;
Point newpos = mStrip.PointToScreen(Point.Empty);
this.Location = new Point(this.Left + pos.X - newpos.X,
this.Top + pos.Y - newpos.Y);
}
}
}

Usage:

private void toolStripButton2_Click(object sender, EventArgs e) {
new ToolStripForm(toolStrip1).Show();
}


Hans Passant.
  • Marked As Answer byherenvardo Thursday, September 10, 2009 3:30 PM
  •  
nobugz  Thursday, September 10, 2009 3:00 PM
Worked like charm, thanks a lot!

Just a comment, if someone else were to rely on that answer to achieve a similar goal, besides some more project-specific tweaks, I had to add a quite criticall line within the OnLoad event:
_mStrip.Location = Point.Empty; // or = new Point(0, 0) if you prefer

Otherwise, when "floating" a strip that is not on the first row/column of its ToolStripContainer's panel, it will be placed "outside" of the form ^^;

Luckily, the code you posted adds the item before resizing the form, so a step-by-step execution was enough to spot the missplaced bar before the form got shrunk.
herenvardo  Thursday, September 10, 2009 3:40 PM

You can use google to search for other answers

Custom Search

More Threads

• Any ideas what is causing this?
• How to make a help file?
• Redrawing issues
• Disable an item in a listview
• Notify even if screensaver active...
• Listview organizing
• DetailsView: multiple columns
• Tab Control
• OpenFileDialog.Filter Question
• How to call 32 bit odbc on 64 bit machine ?