Trying to make a dialog window remember its last position - c#

I am creating a simple dialogue window in C#, and want to remember where it is placed so that I can open another in the same place later (during the same application run, so no need for config files etc). I can easily save the Location (a Point) or the Bounds (a Rectangle), but on creating another form, calling form.ShowDialog() resets both:
Form form= new Form();
form.Location = ptSavedLocation;
//now form.Location is correct
form.ShowDialog();
//now form.Location is default again, and form is displayed where I don't want it.
How can I get the form to respect its Location (or Bounds, or any other appropriate property / setter) ? Thanks!

Set the forms start position to Manual
eg.
Form form= new Form();
form.StartPosition = FormStartPosition.Manual;
form.Location = ptSavedLocation;
//now form.Location is correct
form.ShowDialog();
//now form.Location is default again, and form is displayed where I don't want it.

Set the forms StartPosition property to Manual

Related

Maximizing a form, and disabling user to resize the form

I am trying to create a form that is maxed out, without allowing the user to resize it. I tried to maximize the FormWindowState, and remove the minimize and maximize button. By setting this.MimimumSize and this.MaximumSize to this.Size (the maximized size), this should account to a maximized form.
Yet, when I run it, the form becomes a really small square. Any thoughts on how I can fix this?
public partial class Testscherm : Form
{
public Testscherm()
{
this.WindowState = FormWindowState.Maximized;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MinimumSize = this.Size;
this.MaximumSize = this.Size;
this.MaximizeBox = false;
this.MinimizeBox = false;
InitializeComponent();
}
}
Try calling InitializeComponent() first, then add any statements which changes attributes/properties of the form. Otherwise, the designer generated code may undo any of changes you did beforehand.
Second, Form.Size does not deliver the form's size in maximized state. You could instead iterate over
System.Windows.Forms.Screen.AllScreens
then get the actual screen size along the lines of
System.Windows.Forms.Screen.AllScreens.First().WorkingArea.Size;
and assign it to this.Size;
Another problem here is, as soon as you assign this, this.MaximizeBox = false, Winforms forbids WindowState to be FormWindowState.Maximized. This is obviously "by design". What you probably want here is to use
this.FormBorderStyle = FormBorderStyle.None;
which does not only remove the maximum and minimum buttons, but also the close button. If you need such button, consider to add your own close button to the window.
Putting this all together gives
InitializeComponent();
this.WindowState = FormWindowState.Maximized;
this.FormBorderStyle = FormBorderStyle.None;
this.Size = System.Windows.Forms.Screen.AllScreens.First().WorkingArea.Size;
this.MinimumSize = this.Size;
this.MaximumSize = this.Size;
But: are you sure it is a good idea what you are trying there? How will this behave on a machine with two monitors of different resolution for example? It may be ok for a program which uses specific hardware and works as a dedicated software which takes over the machine exclusively (something like a Kiosk mode). Note the window may still be moved around using certain Win-<Key> keyboard shortcuts.

How to open two same Winforms on Maximised and minimized WindowState?

I have an game as an Winform in Full Screen (no borders), maximized. But I want to present them on an different screen, like a beamer and control it on the original screen, so I need the same screen, opened twice:
One at my own screen Maximized, no borders
One at the second screen, normal state with title bar.
I'm quite sure, I'm thinking about this too easily. What I tried is:
new game().Show();
new game().Show(WindowState = FormWindowState.Normal));
But then It says:
Argument 1: cannot convert from 'System.Windows.Forms.FormWindowState' to 'System.Windows.Forms.IWin32Window'
So, is this hard to do?
Try this:
game g1 = new game();
g1.Show();
game g2 = new game();
g2.WindowState = FormWindowState.Normal;
g2.Show();
What you tried doesn't work because Form.Show() expects either nothing or another IWin32Window (typically another Form) as an argument, and you're passing a FormWindowState.
Note that, while this shows you how to open two forms of the same class with different window states, it doesn't show you how to make two different forms show the same game content at once, which will be much more involved.

Getting a text area in the system tray

i was just wondering what i need to research to be able to have a programme that is in the system tray, when the user clicks the programme icon, just above the system tray a small text area appears allowing the user to type in a search condition. There is plenty of resources for c# and getting your programme in the system tray, but then it just opens as normal, which is not quite what i am looking for.
Thanks
One way to accomplish this is to use a standard WinForms window which contains a single text box and has no border. This window can then be displayed and positioned as normal (likely using many of the existing samples) but will appear as a floating text box.
var form = new MyTextBoxForm();
form.FormBorderStyle = BorderStyle.None;
form.StartPosition = FormStartPosition.Manual;
// position the form
form.ShowDialog();
Handle the NotifyIcon.Click event and show your form in the desired location.
For example:
var screen = Screen.PrimaryScreen;
form.Left = screen.WorkingArea.Right - form.Width;
form.Top = screen.WorkingArea.Bottom - form.Height;
Maybe with this Make your program in the system + add a menu you could try editing the menu, like you'd do a regular menu with toolstrips.... and change the label by a textbox.
Just a random idea.

How to dock a windows form in C#?

I just would like to know if it is possible to dock a windows form on top of the user screen? I have been trying to do this by manually setting the position of my form to the coordinates I want. But using this method, however, allows the user to change the position of the form just by dragging it. I want to make the form docked to the upper portion of the screen since this window form will server as a menu for the project I am making.
Thanks a lot. :)
I would consider using the Control.Dock property along with one of the DockStyle enumeration values.
You might need to play with the Layout too, so that you may layout your form's controls differently depending on the DockStyle selected.
You will need, in my point of view, to consider the Control.Location property so that you get to know which DockStyle value to dock your form with.
EDIT #1
Your Windows Form has a Dock property as it inherits from Control.
Let's consider the following :
Each time your form comes closer to your right-side of the screen, for example, or of the MDI container, you want to dock right, right ? (Little word play here... =P) So, you have to subscribe to the Control.LocationChanged event.
private void myForm_LocationChanged(object sender, EventArgs e) {
if (this.Location.X > 900) then
this.Dock = DockStyle.Right;
else if (this.Location.X < 150) then
this.Dock = DockStyle.Left;
else if (this.Location.Y > 600) then
this.Dock = DockStyle.Bottom;
else if (this.Location.Y < 150) then
this.Dock = DockStyle.Top;
else
this.Dock = DockStyle.None;
}
Indeed, instead of constant values, you should use the current desktop resolution and calculate a ratio from it where you want your docking to occur.
***Disclaimer:****This code is provided as-is and has not been tested. This algorithm is hopefully enough to guide you through the docking process as you need it. Further assistance may be brought upon request.* =)
It seems the Form.DesktopLocation property is the righter tool for the job as for your main window, meaning your MDI container, for instance. As for the other windows, I would go along with something that looks like the code sample provided.
Does this help?
EDIT #2
If you want to prevent Form's overlapping, perhaps the Control.BringToFront() method could do it before or after your call to the Control.Show() method, depending on what works best for you.
So after some tweaks I finally was able to get this code working.
this.DesktopLocation = new Point((Screen.PrimaryScreen.Bounds.Width / 2 - 420), 0);
I placed that line below the InitializeComponent() and it docks my form to the center of the screen with whatever resolution values.
By setting the FormBorderStyle of your form to None, you take the drag handle away from the user so they cannot move it via the mouse.
Then you just need to place it where you want.
If you really want to take away the users options you can also set the ShowInTaskbar property to false

Equivalent of Swing's pack in C#

I am developing a quick app in C#. One thing that I want to do is the equivalent of Swing's (java) pack(). This is a call that allows me to say to a form (Frame in Java), resize yourself to the size of the sum of the components within yourself.
I have searched and searched (the components change dynamically so I cannot simply hardcode the form to the right size) but I cannot find the equivalent method in C#.
Does anyone know what it is called?
You don't even need write code in order to 'pack' the form. In the forms designer, set the form's AutoSize property to true, and set the AutoSizeMode property to GrowAndShrink, repeat this for any controls on the form which may also change size.
And voila!
At runtime (only), the form will dynamically resize itself to accommodate all the controls. If you want it to keep a little distance from the controls so that the controls won't 'stick' to the edges, you can set the Padding property (I'd recommend at least a value of 12,12,12,12).
Also in xaml:
<Window SizeToContent="WidthAndHeight" />
Another solution, available only if the image is smaller than the screen:
Form frm = new Form();
PictureBox pbx = new PictureBox();
pbx.Image = Image.FromFile( _imagePath.Text );
pbx.Dock = DockStyle.Fill;
frm.Controls.Add( pbx );
frm.Size = pbx.Image.Size;
frm.Show();

Categories