how to make fullscreen toggle button on/off - c#

I'm working on a Win Forms project where I'm trying to create an button to toggle full screen on/off every time i click the button,so if you click once, it becomes full screen, if you click again, it becomes normal again.
please tell me how to do that.

I am not sure about the exact requirements but this should help you.
Add Form1_Load and button1_Click events.
private void Form1_Load(object sender, EventArgs e)
{
// Normal when the form opens
WindowState = FormWindowState.Normal;
}
private void button1_Click(object sender, EventArgs e)
{
// Toggle on a button click
WindowState = WindowState == FormWindowState.Normal ? FormWindowState.Maximized : FormWindowState.Normal;
}

Related

C# PerformClick method in invisible form

I am stuck on using a PerformClick method. I have a main form which is called mymainform, and I have some subforms. When loading main form, I am creating subforms and hide them with Form Visibility and access some elements on subforms.
My problem is clicking a button on subform1 from MainForm. I have written the below code and it didn't work. Normally DayModeButton changes the boolen; isDay = !isDay
After clicking the button1 on mainform it doesn't change the isDay boolen.
private void button1_Click(object sender, EventArgs e)
{
mysubform1.DayModeButton.PerformClick();
button1.Text = mysubform1.isDay.ToString();
}
If I write this code it works, but I don't want to show and hide the form because it is not a good view for users.
private void button1_Click(object sender, EventArgs e)
{
mysubform1.Visible=true;
mysubform1.DayModeButton.PerformClick();
button1.Text = mysubform1.isDay.ToString();
mysubform1.Visible=false;
}
Can anyone help me in performing a Click event in invisible forms?
Thanks
Make sure your DayModeButton is Public and not Private.

Parts of the form disappear when WindowState is set to maximized

This problem is happening when the FormBorderStyle = None and a button is used to set the WindowState. Chunks of the form turn invisible when the form is restored from a minimized state or is maximized from a normal state.
The code for maximizing and minimize:
private void MaximizeButton_Click(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Normal)
{
WindowState = FormWindowState.Maximized;
}
else
{
WindowState = FormWindowState.Normal;
}
}
private void minimize_button_Click(object sender, EventArgs e)
{
WindowState = FormWindowState.Minimized;
}
I have failed to recreate this glitch in any other winform based application of mine.
I appreciate the help , hope you all are having a comfortable covid stay.
Simply refresh the window once its state becomes maximized. Unlike WPF, Winforms doesn't have a StateChanged event, so use SizeChanged to detect if the window it's maximized, and refresh it.
private void Window_SizeChanged(object sender, EventArgs e)
{
if(WindowState == FormWindowState.Maximized)
Refresh();
}

c# make a program minimize to task bar

I am trying to make my application minimize to task bar/tray
Here is my code so far that I have pulled together from other SO posts and other people seem to have it working but my app minimizes to the tray but when I click it in the tray it doesn't reopen.
private void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
this.notifyIcon1.Visible = true;
this.notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
}
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
just to explain again the problem is the application minimizes to the tray but when I click on the icon it doesn't restore the app to normal. Instead it does nothing.
I found my problem.
what I didn't do was this
Set the NotifyIcon's visible property to false in the property editor. Now go to the property editor of Form1, click on the little lightning symbol to access the events, and double click on the Resize event, and change the code to:
and I also didn't do this
Finally we need the code to make the program show up again when the icon is double clicked. So double click on NotifyIcon1 in the designer,
I found this information here
Dreamincode
This is what I have used in the past but I fund a copy online at the link below
if (FormWindowState.Minimized == this.WindowState)
{
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
found here: http://www.codeproject.com/Articles/27599/Minimize-window-to-system-tray

C# Minimize to Tray

I know you will be thinking "Not again this question", as I found like a hundred results when I searched for it. But when I put in the code as described on the pages here, it just minimizes to right above the start menu.
This is the code I use (I added a message box to see if the code gets triggered, but the message box never pops up):
private void Form1_Resize(object sender, EventArgs e)
{
MessageBox.Show("Works1");
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
Because I don't know if it links to Form1 or Form, I have tried both, to no avail.
private void Form_Resize(object sender, EventArgs e)
{
MessageBox.Show("Works");
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
Now, when you double click on the Form, it puts this line in the Form1.Designer.cs:
this.Load += new System.EventHandler(this.Form1_Load);
Do I need a similar line to trigger the minimize event?
As you can see, I am completely lost :)
Oh, and it doesn't minimize to the taskbar, as I am using the following code to hide the form on run:
protected override void OnLoad(EventArgs e)
{
Visible = false; // Hide form window.
ShowInTaskbar = false; // Remove from taskbar.
base.OnLoad(e);
}
You need the event
private void Form1_Resize(object sender, EventArgs e)
{
}
Creating Event Handlers on the Windows Forms Designer
Add a NotifyIcon component to your Form. Make sure you set an icon via the properties pane otherwise it will be invisible.
Create an event handler for the form's Control.SizeChanged event. In that event handler place the following code:
sample code:
private void MainForm_SizeChanged(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
ShowInTaskbar = false;
}
And then to make the form visible again the NotifyIcon.MouseDoubleClick event handler you can place the following code:
private void trayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
WindowState = FormWindowState.Normal;
ShowInTaskbar = true;
}
The basic thing you need to know is events. Events are triggered when certain things happen to your form (or any control). For example, when the form is resized, or loaded, or clicked, an event is raised. You can hook into this event to execute your own code when the event happens.
In your case you want to execute code to minimize the form, on the event that the form is resized. So you need to hook your method to the resize event. The name of your method is not relevant, so let's use a better name:
private void HideWhenMinimized(object sender, EventArgs e)
{
MessageBox.Show("Works1");
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
To hook your HideWhenMinimized method into the Resize event of the form, you have to do it like this:
this.Resize += new System.EventHandler(this.HideWhenMinimized);
If you add that line of code in the form's constructor or Load event, then your code gets called as soon as the form is resized.

How to make Close button work as Minimize: C# Winforms

How can I make the (default) Close button (at top right corner) in my application, to work it as Minimize.
Actually I want to minimize the application on clicking the cross-symbol, but exit the application, when use clicks on my menu option Exit.
I wrote this code for minimizing the form on clicking close button:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (minimize_on_close == "Yes")
{
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
}
}
and wrote this code for exiting the application, on clicking exit from menu options.
private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
{
Application.Exit();
}
But now, when I click on Exit menu option, then also the form is being minimized, and not exiting.
Can anyone please help?
Check to see whether FormClosingEventArgs.CloseReason is equal to CloseReason.UserClosing before deciding to minimize the window. Alternatively, compare for CloseReason.ApplicationExitCall.
From the documentation for CloseReason:
Members
...
UserClosing
The user is closing the form through the user interface (UI), for example by clicking the Close button on the form window, selecting Close from the window's control menu, or pressing ALT+F4.
...
ApplicationExitCall
The Exit method of the Application class was invoked.
Try this
EDIT
May use Resize Event to do it,
private void Form1_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
this.Hide();
}
Then using the FormClosing Event to cancel Close and minimize the Form as below
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
}
}

Categories

Resources