我需要创建一个方法,它将在某些条件下打开一个Mid form。打开Mid窗体时,我必须将MidParent
值分配给应用程序的父级。
我尝试使用ApplicationContext
类来计算当前的MID值,但当从不同的MainForm
表单调用它时,它并不能像预期的那样工作。
这就是我所做的。在Program.cs
文件中,下面是我所拥有的
namespace RM
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
private static AppContext aContext;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
aContext = new AppContext(new Main());
Application.Run(aContext);
}
public static void Quit() {
DialogResult res =
MessageBox.Show("Are You Sure you want to close the application", "Exiting Application",
MessageBoxButtons.YesNo,
MessageBoxIcon.Warning);
if (res == DialogResult.Yes) {
aContext.ExitThread();
}
}
}
class AppContext : ApplicationContext
{
//public Form MainForm { get; set; }
public static ApplicationContext CurrentContext;
public AppContext(Form mainForm)
: base(mainForm) {
CurrentContext = this;
}
}
}
当方法OpenMyForm()
被调用时,我需要分配应用程序父窗体,以便能够打开Mid窗体。下面是此方法的代码
public static void OpenMyForm(string sectionName,string[] keys,form myform) {//确保没有其他表单类型的ame类型open foreach (Application.OpenForms中的Form form){ if (form.GetType() == myform.GetType()) { form.Activate();return;}}
if (Settings._AuthenticationMode == "Thumbprint") {
var newMDIChild = myform;
// Set the Parent Form of the Child window.
newMDIChild.MdiParent = AppContext.CurrentContext.MainForm;
// Display the new form.
newMDIChild.Show();
}
if (Settings._AuthenticationMode == "Single" && UserInfo.Autherized == true) {
var role = new Roles();
if (role.hasAccess(sectionName, keys)) {
var newMDIChild = myform;
// Set the Parent Form of the Child window.
newMDIChild.MdiParent = AppContext.CurrentContext.MainForm;
// Display the new form.
newMDIChild.Show();
}
else {
Common.Alert("You do not have a permissions to perform this action!");
}
}
}
}
当我从Main() "Parent“表单调用OpenMyForm()
方法时,我没有得到任何问题。但是,当我在MID表单上调用它时,会得到以下异常
System.NullReferenceException was unhandled
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=Telerik.WinControls.UI
StackTrace:
at Telerik.WinControls.UI.RadListElement.HandleMouse(Object sender, RoutedEventArgs args)
at Telerik.WinControls.UI.RadListElement.OnBubbleEvent(RadElement sender, RoutedEventArgs args)
at Telerik.WinControls.RadElement.RaiseBubbleEvent(RadElement sender, RoutedEventArgs args)
at Telerik.WinControls.RadItem.RaiseBubbleEvent(RadElement sender, RoutedEventArgs args)
at Telerik.WinControls.RadElement.RaiseBubbleEvent(RadElement sender, RoutedEventArgs args)
at Telerik.WinControls.RadElement.RaiseBubbleEvent(RadElement sender, RoutedEventArgs args)
at Telerik.WinControls.RadItem.RaiseBubbleEvent(RadElement sender, RoutedEventArgs args)
at Telerik.WinControls.RadElement.RaiseRoutedEvent(RadElement sender, RoutedEventArgs args)
at Telerik.WinControls.RadItem.RaiseBubbleEvent(RadElement sender, RoutedEventArgs args)
at Telerik.WinControls.RadElement.RaiseRoutedEvent(RadElement sender, RoutedEventArgs args)
at Telerik.WinControls.RadElement.DoMouseUp(MouseEventArgs e)
at Telerik.WinControls.ComponentInputBehavior.OnMouseUp(MouseEventArgs e)
at Telerik.WinControls.RadControl.OnMouseUp(MouseEventArgs e)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at Telerik.WinControls.RadControl.WndProc(Message& m)
at Telerik.WinControls.UI.RadPopupControlBase.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at RM.Program.Main() in C:\Users\User\C# Projects\RM\RM\Program.cs:line 21
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
发布于 2015-01-24 04:42:52
是否只有one MdiParent?
如果是,那么你可以创建一个小的helper类:
public class MyApp
{
public static Form MdiForm
{
get {
foreach (Form frm in Application.OpenForms)
{
if (frm.IsMdiContainer)
return frm;
}
return null;
}
}
}
然后从任何地方使用它:
Form frm = new Form();
frm.MdiParent = MyApp.MdiForm;
frm.Show();
发布于 2015-01-24 13:27:23
我的代码实际上是正确的。此问题是由导致关闭意外关闭的另一个代码引起的。
发布于 2015-05-18 21:45:49
应用程序上下文应按如下方式初始化:
aContext = new AppContext();
https://stackoverflow.com/questions/28102246
复制相似问题