this.Visible is not working in Windows Forms
Yes, the Visible property is a big deal in Windows Forms, that's what actually gets the handle created and causes OnLoad() to run. In other words, the window doesn't exist until it gets visible. And it will ignore attempts to undo this.
It is pretty common to want to still create the handle but not make the window visible if you use a NotifyIcon. You can achieve this by overriding SetVisibleCore:
protected override void SetVisibleCore(bool value) {
if (!this.IsHandleCreated) {
value = false;
CreateHandle();
}
base.SetVisibleCore(value);
}
Beware that OnLoad still won't run until the window actually gets visible so move code into the constructor if necessary. Just call Show() in the NotifyIcon's context menu event handler to make the window visible.
Can't set .Net panel to visible
The standard mistake is that you accidentally put the mainpanel inside the loginpanel. So when you make loginpanel invisible, the mainpanel can never become visible. This accident is common in the designer, it won't let you put two panels on top of each other. You fix it with View + (Other Windows) + Document Outline. Drag mainpanel and drop it on the form. You'll have to fix the Location property by editing it in the Properties window instead of moving the panel with the mouse.
An entirely different approach is to use a TabControl. Easy in the designer, you just need to hide the tabs at runtime. Code is here.
Or use two UserControls.
How to make a form visible or invisible in C#
Solution 1: if you want to display the Settings
Form first time, you need to create an instance variable of that Form
and call ShowDialog()
method to display it.
Try This:
void SpecificFunction()
{
//--at the end of MyFunction call settings form
Settings frmSettings=new Settings();
frmSettings.ShowDialog();
}
Solution 2: if you want to display the Settings
Form which was hidden before , you need to to follow the below steps make it Visible
.
Step 1: identify/obtain the Settings
Form by using Application.OpenForms[]
Collection.
Step 2: create a new instance variable of a Form
by casting the obtained Settings
Form in above step.
Step 3: Call the Show()
Method on the created instance variable to Show/Display
the Settings
form.
Try This:
void SpecificFunction()
{
//--at the end of MyFunction call settings form
Form frmSettings = (Settings) Application.OpenForms["Settings"];
frmSettings.Show();
}
C# UserControl Visible Property Not Changing
I didn't break C#! :)
Turns out the culprit was the Form.Visible property. Before Form.Visible is set to true, any and all controls on the form will be invisible (Visible = false) no matter what.
However, you can still set Visible properties - they just won't take effect until the Form.Visible property is set to true.
In other words, when I called ucFollow.Visible = true
, my program was indeed registering it - however, at that point in the code, ucFollow's parent Form.Visible was still false. Therefore, both the Debugger and my print statements recognized, "Hey, this control's parent form is still not visible, so this control is not visible. Period."
As soon as the form was made visible, all the changes took effect and everything worked great.
Moral of the story: Don't rely on the Visibility properties of your controls unless the form containing them is already visible and running.
C# Panel is not visible
Make sure that you did not put the second panel inside the first panel. If you dragged the panels from the toolbox, there is high probability of this.
To make sure, open the Document Outline
window (View->Other Windows->Document Outline), and look at the relation between panels. Make sure that they are not contained in one another. They must be at the same level of nesting.
If it is like this:
then select the inner panel and press the left arrow button above the window. Then it should look like:
which is the correct one.
When is Control.Visible = true turns out to be false?
If you set a parent/container control to Visible = false
then setting any child controls to Visible = true
will have no effect what so ever. The Visible
property of the child control will still be false
.
I don't know if it's what happens in this case since I don't know the structure of the controls but it seems to be a likely scenario.
To solve this you need to first set the parent/contianer control to Visible = true
and THEN the child control(s).
How can an application that has a not visible WinForm be restored from another application?
Hans Passant pointed me in the proper direction with his comment to my question referring to this question.
My final code looked like this.
using System;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices; // Add reference to Microsoft.VisualBasic
namespace MyApp
{
class Program : WindowsFormsApplicationBase
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
var app = new Program();
app.Run(args);
}
public Program()
{
this.IsSingleInstance = true;
this.EnableVisualStyles = true;
this.MainForm = new fMain();
}
protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
{
if (this.MainForm.WindowState == FormWindowState.Minimized) {
this.MainForm.Show(); // Unhide if hidden
this.MainForm.WindowState = FormWindowState.Normal; //Restore
}
this.MainForm.Activate();
}
}
}
Related Topics
Read Connection String from Web.Config
C# Elegant Way to Check If a Property's Property Is Null
Oraclecommand SQL Parameters Binding
Forms Authentication Across Sub-Domains
Retrieve Current Url from C# Windows Forms Application
How to List All Processes Running in Windows
How to Use a MySQL User Defined Variable in a .Net MySQLcommand
Timeouts with Long Running ASP.NET MVC Core Controller Httppost Method
Wpf: How to Dynamically Add Controls in Dynamically Created Wpf Window
Access a Remote Directory from C#
C# Selenium 'Expectedconditions Is Obsolete'
How to Get More Than 1000 Records from a Directorysearcher
The ':' Character, Hexadecimal Value 0X3A, Cannot Be Included in a Name
How to Get All Instances of All Loaded Types That Implement a Given Interface
How to Run a Test Method with Multiple Parameters in Mstest
How to Kill a Process Using Vb.Net or C#