How to minimize and maximize in C#.Net? - c#

I want to minimize and maximize manually in C#.net.
I changed form's BorderStyle into none.
So there are no maximize,minimize and close button from bar.
I want to manually create with button like those functions.
I want to do three functions in button click events.

You have to set the forms WindowState property something like this:
In Windows Forms:
private void button1_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
In WPF:
private void button1_Click(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Minimized;
}

Form.WindowState Property
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.windowstate%28v=VS.90%29.aspx
public FormWindowState WindowState { get; set; }
For example -
var form = new Form();
form.WindowState = FormWindowState.Maximized;
form.WindowState = FormWindowState.Minimized;
form.WindowState = FormWindowState.Normal;
However, if you are in the code behind on the main form (or any form) just do this -
WindowState = FormWindowState.Maximized;

If you're using WindowsForms you have to change the WindowState property :)

private void button4_Click(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Normal)
{
this.WindowState = FormWindowState.Maximized;
}
else
{
this.WindowState = FormWindowState.Normal;
}

Related

c# Why can I not return my application from minimize?

I have googled this and all the sites I have seen tell me the same thing for minimizing my app and then returning it to normal. I can minimize it fine but when I click on the icon in the tray then nothing happens. Here is my code.
private void Form1_SizeChanged(object sender, EventArgs e)
{
bool PointerNotOnTaskbar = Screen.GetWorkingArea(this).Contains(Cursor.Position);
if (this.WindowState == FormWindowState.Minimized && PointerNotOnTaskbar)
{
notifyIcon1.Icon = SystemIcons.Application;
this.ShowInTaskbar = false;
notifyIcon1.Visible = true;
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.ShowInTaskbar = true;
WindowState = FormWindowState.Normal;
notifyIcon1.Visible = false;
}
I have also tried
this.WindowState = FormWindowState.Normal;
Comment out this: this.ShowInTaskbar = false;
If the form is minimized and not visible in taskbar will take a spell to bring it back!

How to make Application ontop of all windows and allow message box to be shown correctly

I am writing an application, that on specific conditions must make my application ontop of all other windows applications.
I do this in timer code as shown below that runs every 10 ms. That is fine. However if i need to throw a message box if there is an error then the messagebox ok button cannot be pressed because the form is made topmost every 10 ms - The button can be seen but you cant click ok.
Assumably because the first time you click the button brings the errorbox modal and by the time you click the ok button 10 ms has passed and the form is now ontop again.
How can i fix this issue ?
Timer
private void timer1_Tick(object sender, EventArgs e)
{
this.Visible = true;
this.TopMost = true;
this.WindowState = FormWindowState.Maximized;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
}
Show an Error
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Some error occured");
}
private void timer1_Tick(object sender, EventArgs e)
{
if (!this.TopMost == true)
this.TopMost = true;
this.Visible = true;
this.WindowState = FormWindowState.Maximized;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Some error occured");
}

Opening Winform from taskbar

I have a Winform, which is hidden initially on startup.
Thereafter the user is free to click the notification icon in the bottom and show it if they want, and when it is minimized it needs to go back into the system tray.
It starts in the tray fine - no problems. When you click to show it the first time though it appears, then for a fraction of a second looks like it is disappearing, then comes back. So it looks like it flickers a bit.
Then when you minimize it, it goes into the system tray as it should which is fine. When you click to show it again though (any time after you have done it once) it sort of glides in from either the system tray or the taskbar, I would prefer it to just appear, without the little animation.
public class Program : Form
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Program());
}
private NotifyIcon trayIcon;
private ContextMenu trayMenu;
public Program()
{
trayMenu = new ContextMenu();
trayMenu.MenuItems.Add("Exit", OnExit);
trayMenu.MenuItems.Add("Show", OnShow);
trayIcon = new NotifyIcon();
trayIcon.Text = "MyTrayApp";
trayIcon.Icon = new Icon(SystemIcons.Application, 40, 40);
trayIcon.ContextMenu = trayMenu;
trayIcon.Visible = true;
}
protected override void OnLoad(EventArgs e)
{
Visible = false;
ShowInTaskbar = false;
base.OnLoad(e);
}
protected override void OnResize(EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
Visible = false;
ShowInTaskbar = false;
}
base.OnResize(e);
}
private void OnExit(object sender, EventArgs e)
{
Application.Exit();
}
private void OnShow(object sender, EventArgs e)
{
Visible = true;
ShowInTaskbar = true;
TopMost = true;
WindowState = FormWindowState.Normal;
}
protected override void Dispose(bool isDisposing)
{
if (isDisposing)
{
trayIcon.Dispose();
}
base.Dispose(isDisposing);
}
}
Any pointers on how to just get it to appear properly would be really appreciated.
Update
I have found the cause of the flickering, it happened when TopMost is set last, after is has been shown, so it redraws it on top, which makes sense.
So as it stands, it's just getting it to appear and disappear without the animations.
EDIT: You should only set the Visibility property in Onload(), just use Show() and Hide() to avoid the animations and be sure to change the WindowState accordingly.
I put your code into a regular Form (not in the Program.cs file) and deleted all superfluous code. This is what I ended up with and it doesn't show the "double" animation.
public partial class Form1 : Form
{
private NotifyIcon trayIcon;
private ContextMenu trayMenu;
public Form1()
{
InitializeComponent();
this.ClientSize = new System.Drawing.Size(284, 262);
this.Name = "Program";
trayMenu = new ContextMenu();
trayMenu.MenuItems.Add("Exit");
trayMenu.MenuItems.Add("Show", FormShow);
trayIcon = new NotifyIcon();
trayIcon.Text = "MyTrayApp";
trayIcon.Icon = new Icon(SystemIcons.Application, 40, 40);
trayIcon.ContextMenu = trayMenu;
trayIcon.Visible = true;
TopMost = true;
Resize += new EventHandler(Form1_Resize);
}
void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
ShowInTaskbar = false;
Hide();
trayIcon.Visible = true;
}
}
void FormShow(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
WindowState = FormWindowState.Normal;
}
ShowInTaskbar = true;
Show();
Focus();
trayIcon.Visible = false;
}
protected override void OnLoad(EventArgs e)
{
Visible = false;
ShowInTaskbar = false;
}
}

Completely Maximized C# WinForm

Is there a way to make the C# form completely covers the whole screen? I would be doing this without explorer.exe running, if that makes it easier. I don't want it to be full screen, because I want other programs to be able to run above it. Thanks!
private void frm_Load(object sender, EventArgs e)
{
ControlBox = false;
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
}
private void frm_KeyDown(object sender, KeyEventArgs e)
{
// restore form on Escape key press.
if (e.KeyCode == Keys.Escape)
{
ControlBox = true;
FormBorderStyle = FormBorderStyle.Sizable;
WindowState = FormWindowState.Normal;
}
}

c# how to FormWindowState.Normal

i have this code:
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
about About = new about();
About.ShowDialog();
}
it minimizes the parent window state to minimized and displays a splash form.
my question is when the splash screen closes how do i get back to parentwindowstate.normal?
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
about About = new about();
About.ShowDialog();
this.WindowState = FormWindowState.Normal;
}
If you're using ShowDialog instead of Show; you can add
this.WindowState = FormWindowState.Normal;
after the ShowDialog call. (ShowDialog is blocking, unlike Show.)
Call ShowDialog() like this:
About.ShowDialog(this);
Then, in the About form's FormClosing event, put:
this.Parent.WindowState = WindowState.Normal;

Categories