How do I hide or unhide a form in C#
Okay, my code for Form1 which has a button that shows Form2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Form2 f2 = null;
public Form1()
{
InitializeComponent();
}
private void btnShowForm2_Click(object sender, EventArgs e)
{
if (f2 == null) { f2 = new Form2(); }
f2.Show();
}
}
}
and on Form2, I put a textbox with no events (but its text is remembered between hiding and showing Form2), and it has a button that hides its form. here's the code for Form2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void btnHideMe_Click(object sender, EventArgs e)
{
this.Hide();
}
}
}
Is it possible to turn off and on all the // comments at once on Visual Express C#?
Select the lines then:
Ctrl + K + U
And the opposite:
Ctrl + K + C
You can find the shortcuts also through the menus - Edit -> Advanced -> ... The shortcuts are listed next to the commands.
And of course, these are also on the Text Editor toolbar:
Hide / Unhide a file is not working so well
MSDN:
var attributes = File.GetAttributes(fi);
if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
{
attributes &= ~FileAttributes.Hidden;
File.SetAttributes(fi, attributes);
}
How to hide / unhide a process in C#?
Finally, the process is operating properly. Thanks to all of your help, I came up with this fix.
The p.MainWindowHandle was 0, so I had to use the user32 FindWindow() function to get the window handle.
//Initialization
int SW_SHOW = 5;
[DllImport("user32.dll",SetLastError=true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern bool EnableWindow(IntPtr hwnd, bool enabled);
And in my main function:
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "notepad";
info.UseShellExecute = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process p = Process.Start(info);
p.WaitForInputIdle();
IntPtr HWND = FindWindow(null, "Untitled - Notepad");
System.Threading.Thread.Sleep(1000);
ShowWindow(HWND, SW_SHOW);
EnableWindow(HWND, true);
References:
pinvoke.net: FindWindow()
Edit:
Removed WindowShowStyle from the dllImport declaration: you can define this as an int instead. I defined an enum called WindowShowStyle to define the constants outlined in this article. It just better fits my coding patterns to have enums defined instead of using constant or hard-coded values.
Related Topics
Entity Framework 4: How to Find the Primary Key
Console App Mouse-Click X Y Coordinate Detection/Comparison
C# Code to Linkify Urls in a String
Advantage of Using Thread.Start VS Queueuserworkitem
How to Export a Gridview.Datasource to a Datatable or Dataset
Formatting Datetime in ASP.NET Core 3.0 Using System.Text.JSON
ASP.NET Core 2.0 Disable Automatic Challenge
Easiest Way to Parse "Querystring" Formatted Data
How to Create an Instance from a String in C#
Serialize as Ndjson Using JSON.Net
Best Practice: Convert Linq Query Result to a Datatable Without Looping
Show Yes/No Instead True/False in Datagridview
C# Date Formatting Is Losing Slash Separators
Error: Must Create Dependencysource on Same Thread as the Dependencyobject Even by Using Dispatcher