Reputation: 15630
I have two forms and 1 singleton class. I am initalizing the singleton class in btn_A_Click of formA.
public partial class frmA : Form
{
public frmA()
{
InitializeComponent();
frmB frmB;
}
private void btn_A_Click(object sender, EventArgs e)
{
SessionMgmt.GetInstance().StartFormB();
}
}
This is my singleton class and here I am trying to use Forms.Invoke() method.
public class SessionMgmt
{
static SessionMgmt _sessinMgr;
frmB frB;
private SessionMgmt()
{
frB = new frmB();
}
public static SessionMgmt GetInstance()
{
if (_sessinMgr != null)
return _sessinMgr;
else
{
_sessinMgr = new SessionMgmt();
return _sessinMgr;
}
}
public bool StartFormB()
{
frB.Invoke(new EventHandler(DisplayFrmB));
return true;
}
private void DisplayFrmB(Object o, EventArgs e)
{
frB.Visible = true;
frB.Refresh();
}
}
This is my formB.
public partial class frmB : Form
{
}
But from the frB.Invoke(new EventHandler(DisplayFrmB));
method it throws the following exception:
Invoke or BeginInvoke cannot be called on a control until the window handle has been created.
I can't figure out the issue, please help or advice me if I miss anything.
EDIT
The following structure is the way my current project is displaying the next form. It was done by VB.NET and I need to use similar kind of thing in the new project which uses C#. I saw the Invoke function which points to an event and then to a function. In that function it just makes the Form.Visible=true and Form.Refresh. But for understanding I just tried a POc and followed the same steps ,but it is not yet solved.
Upvotes: 0
Views: 212
Reputation: 144
Control handle IS NOT created if the control's Visible property is false. When you call Invoke you set your controls visible state to true in delegate, but handle is not created yet, so you can not call Invoke. So - you must call frB.CreateHandle(); after: frB = new frmB(); to force creation of control handle
private SessionMgmt()
{
frB = new frmB();
var h = frB.Handle;
}
Upvotes: 0
Reputation: 7602
What is reason to call invoke? Isn't this doing the job for you?
public bool StartFormB()
{
frB.Visible = true;
return true;
}
Upvotes: 2
Reputation: 11577
there are two possible reasons for that exception:
you should always check the InvokeRequired
Property before Invoking, and of course check for null before that
public bool StartFormB()
{
if (frB == null)
{
throw new ArgumentNullException("frB");
}
if (frB.InvokeRequired)
{
frB.Invoke(new EventHandler(DisplayFrmB));
}
else
{
if (frB.IsDisposed)
{
throw new ObjectDisposedException("Control is already disposed.");
}
}
return true;
}
Upvotes: 0
Reputation: 1145
Windows Forms is a wrapper around the Windows API, and that exception means the underlying window hasn't been created yet. I think it gets created when you set Visible
to true
for the first time, and there are a few other situations that do it.
See this link for a possible solution: http://blogs.msdn.com/b/mapo/archive/2011/04/27/forcing-handle-creation-in-a-net-windows-forms-control.aspx
Upvotes: 0