How to hide WinForm after it run? [duplicate] - c#

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Single Form Hide on Startup
I want to hide my WinForm after it run (Not minimizing).
I used:
this.Load += new System.EventHandler(this.Form1_Load);
private void Form1_Load(object sender, EventArgs e)
{
Hide();
}
But it's not working. Can you help me do it?

In the form Load override you can use one of the following tricks:
Make the form completely transparent:
private void OnFormLoad(object sender, EventArgs e)
{
Form form = (Form)sender;
form.ShowInTaskbar = false;
form.Opacity = 0;
}
Move the form way off the screen:
private void OnFormLoad(object sender, EventArgs e)
{
Form form = (Form)sender;
form.ShowInTaskbar = false;
form.Location = new Point(-10000, -10000);
}

Try to hide the form after it has been shown and not after it has been loaded, use the Shown event instead of the Load event

You can't easily hide the form but what you can do is set the Opacity to 0, for example:
this.Opacity = 0;
If you Don't want the user to be able to see the app at all set this:
this.ShowInTaskbar = false;
Then they won't be able to see the form in the task bar and it will be invisible.
I believe this solved your "no use minimized" requirement??

Related

Where do I put custom code to handle a resize event C# windows form

I cannot work out where to put nor get the code to trigger when my main form windows are resized (ie minimize button clicked)
I am trying to trigger this code when ANY resize of the DigiDocketMain window is minimized etc, or also how I can specifically code the minimize button to do something - the ideal goal is to get the program - n minimize button click to hide the taskbar icon and show a tray icon.
I have tried placing this is the main code body and the designer code but nothing triggers it. any help would be appreciated.
private void DigiDocketMain_Resize(object sender, System.EventArgs e)
{
MessageBox.Show("You are in the Form.ResizeEnd event.");
if (this.WindowState == System.Windows.Forms.FormWindowState.Minimized)
{
this.Hide();
mainTrayIcon.Visible = true;
}
}
In your code behind add the following to the Form_Load Event
this.SizeChanged += Form1_SizeChanged;
Then implement the function, autocomplete may do this for you.
private void Form1_SizeChanged(object sender, EventArgs e)
{
// Add the code that will be called on resize events.
}
According to your description, when clicking the minimize button, you want to hide the
taskbar icon and display the tray icon.
I suggest that you set the Visible of notifyIcon1 to false in the property bar, and select a icon format image as the icon, then try the following code.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Deactivate(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.notifyIcon1.Visible = true;
this.Hide();
this.ShowInTaskbar = false;
}
}
private void notifyIcon1_Click(object sender, EventArgs e)
{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.notifyIcon1.Visible = false;
this.ShowInTaskbar = true;
}
}

Update Controls in Form 1 from Form 2 that was Created by Form 1

I checked and found this article that is referred to MANY times in this type of question, and it is NOT the answer I want...
I have a form, Form_Main frmMainPage, it creates Form_Status frmStatus.
When it does so, I disable the controls on frmMainPage so the user can't mess with things while things are processing.
When things are done processing they can close the frmStatus window to go back to frmMainPage to continue working. I cannot re-enable the controls from frmStatus. I tried to use the frmMainPage_Enter, but it goes crazy when it first loads, so that isn't really an option.
I have the following in Form_Main.cs
public void EnableForm() {
this.gridData.Enabled = true;
this.txtLocation.Enabled = true;
this.txtSupplier.Enabled = true;
this.txtItem.Enabled = true;
this.FillTable("", "", "");
}
When I use this (per article above):
private void btnClose_Click(object sender, EventArgs e) {
Form_Main f2 = new Form_Main();
f2.Show();
f2.EnableForm();
this.Close();
}
It creates a second Form_Main, which is not what I want. I want to know how to change the controls in the existing form.
Edit: No, it is not this that was also suggested. Most of the answers deal with changing controls on Form 2 from Form 1 when Form 2 is created by Form 1. In my case I need to do the opposite and change controls on Form 1 FROM Form 2, which was created by Form 1. Kind of a circular thing.
I can think of a couple of ways to do this. First (and most common) is to show the second form modally (which means that the first form's code pauses while the second form's code is running):
private void button1_Click(object sender, EventArgs e)
{
var statusForm = new frmStatus();
// ShowDialog will prevent frmMainPage from being accessible.
// This form's code will pause at the next line until the second form is closed
statusForm.ShowDialog();
}
There are occasions where you want to have both forms accessible at the same time. If this is the case, another method would be to add an event handler for the second form's FormClosed event, where you can re-enable the controls on the first form. This will allow both forms to be accessed at the same time:
private void button1_Click(object sender, EventArgs e)
{
var statusForm = new frmStatus();
// Add an event handler for the second form's FormClosed event, and
// put code in that event handler to re-enable controls on this form
statusForm.FormClosed += statusForm_FormClosed;
// Disable our controls on this form and show the second form
DisableForm();
statusForm.Show();
}
private void statusForm_FormClosed(object sender, FormClosedEventArgs e)
{
// When the second form closes, re-enable controls on this form
EnableForm();
}
private void DisableForm()
{
this.gridData.Enabled = false;
this.txtLocation.Enabled = false;
this.txtSupplier.Enabled = false;
this.txtItem.Enabled = false;
}
public void EnableForm()
{
this.gridData.Enabled = true;
this.txtLocation.Enabled = true;
this.txtSupplier.Enabled = true;
this.txtItem.Enabled = true;
this.FillTable("", "", "");
}
you dont need to do this disable enable.
you just need to show your new from with ShowDialog(); like this:
frmStatus.ShowDialog();
instead of just:
frmStatus.Show();

Datagridview shall disappear when clicking on the Form in the background

As described in the title, I have a Form with a Datagridview on the front. The datagridview is smaller than my form in the back and I want the Datagridview to disappear whenever I click anywhere else but the Datagridview.
My code looks like this:
this.dataGridView1.Leave += new System.EventHandler(this.focus);
and the Eventhandler is defined like this:
private void focus(object sender, EventArgs e)
{
if(dataGridView1.Focused == false)
{
dataGridView1.Visible = false;
}
}
My problem is that my Datagridview only disappears when a new event in my form is activated but not when I click for example in a textbox on my form.
Can anyone help me?
The Leave event will not raise if you click on Form, or a ToolStripButton, PictureBox or any other non-selectable control.
If you expect a behavior like a dropdown, you can host DataGridView in a ToolStripControlHost and show it using a ToolStripDropDown. This way when you click anywhere outside the `DataGridView, it will disappear. It will act like a dropdown menu. Also the grid can be larger than your form:
private void button1_Click(object sender, EventArgs e)
{
this.dataGridView1.Margin = new Padding(0);
var host = new ToolStripControlHost(this.dataGridView1);
this.dataGridView1.MinimumSize = new Size(200, 100);
host.Padding = new Padding(0);
var dropdown = new ToolStripDropDown();
dropdown.Padding = new Padding(0);
dropdown.Items.Add(host);
dropdown.Show(button1, 0,button1.Height);
}
Important Note: It's an example. It's better to pay attention to disposing of objects in a real world application. For example, use just a single ToolStripdropDown and dispose it when closing the form.
Change the event handler assigning to:
this.dataGridView1.Leave += new System.EventHandler(fokussiert);
Worked for me when focusing on a textbox
you want your dgv also to disapear when you click on your textbox? is that what you mean?
private void dataGridView1_Leave(object sender, EventArgs e)
{
dataGridView1.Visible = false;
}
private void Form1_Click(object sender, EventArgs e)
{
dataGridView1.Visible = false;
}

C# webbrower automatically clicking a menu button

I have seen a few of these questions on here but, none of them answered my question. I am looking to automatically click a button on the google fusion table page. I want to run this is in a windows form in visual studio 2015. I need to navigate to the file-geocode... button. I right clicked and hit the inspect button and this is the ID for the button I want
<td class="gwt-MenuItem" id="gwt-uid-209" role="menuitem" colspan="2">Geocode...</td>
is it possible to use a method similar to this?:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Document
.GetElementById("gwt-uid-209")
.InvokeMember("Click");
}
Thank you for your help.
i used the same code that you mentioned in the question but be careful because the webBrowser may invoke more than 1 events while the document is completing.
i usually set a timer with 1000ms interval;
try this, it will take 1 second more but it will do the job.
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
webBrowser1.Document.GetElementById("gwt-uid-209").InvokeMember("Click");
}
If I use the geckobrowser how do I get it to autoclick? this is what I have now.
Xpcom.Initialize("Firefox");
var geckoWebBrowser = new GeckoWebBrowser { Dock = DockStyle.Fill };
Form f = new Form();
f.Controls.Add(geckoWebBrowser);
geckoWebBrowser.Navigate("https://www.google.com/fusiontables/data?docid=1FdRUOJ9YIXT81QdfS71qXWV3m7qmzF_4TLRrKh5r#rows:id=1");
Application.Run(f);

C# Windows Forms application showing background when opening new form

I am currently making a full-screen application in C# but I have a bit of a problem. I have a overview form which shows buttons. When you click a button it will open a new form. (Overview form and new form are both full-screen). The problem is when the new form opens it will show nothing for a second (It doesnt really look to good in a full screen application). So does anyone know how to fix this? You can find the code here:
Code for opening new form:
void NewForm(Form form)
{
form.FormClosing += new FormClosingEventHandler(form_Closed);
form.Show();
Hide();
}
void form_Closed(Object sender, EventArgs e)
{
Show();
}
One of the opened forms:
public AdminPanelForm()
{
InitializeComponent();
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
}
private void AdminPanelForm_Shown(object sender, EventArgs e)
{
List<User> users = User.GetAllUsers();
foreach(User user in users)
{
ListViewItem lvi = new ListViewItem(user.UserName);
lvi.SubItems.Add(user.Role);
listUsers.Items.Add(lvi);
}
}
private void btnBack_Click(object sender, EventArgs e)
{
Close();
}
Thanks in Advance!
After form.Show() add form.Update(). Otherwise, the real form redraw will be done with delay. It may be better to call Hide first.

Categories