首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用ApplicationContext通过C#设置MidParent值

如何使用ApplicationContext通过C#设置MidParent值
EN

Stack Overflow用户
提问于 2015-01-23 10:48:26
回答 3查看 3K关注 0票数 0

我需要创建一个方法,它将在某些条件下打开一个Mid form。打开Mid窗体时,我必须将MidParent值分配给应用程序的父级。

我尝试使用ApplicationContext类来计算当前的MID值,但当从不同的MainForm表单调用它时,它并不能像预期的那样工作。

这就是我所做的。在Program.cs文件中,下面是我所拥有的

代码语言:javascript
运行
复制
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;}}

代码语言:javascript
运行
复制
    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表单上调用它时,会得到以下异常

代码语言:javascript
运行
复制
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: 
EN

回答 3

Stack Overflow用户

发布于 2015-01-24 04:42:52

是否只有one MdiParent?

如果是,那么你可以创建一个小的helper类:

代码语言:javascript
运行
复制
public class MyApp
{

    public static Form MdiForm
    {
        get {
            foreach (Form frm in Application.OpenForms)
            {
                if (frm.IsMdiContainer)
                    return frm;
            }
            return null;
        }
    }

}

然后从任何地方使用它:

代码语言:javascript
运行
复制
        Form frm = new Form();
        frm.MdiParent = MyApp.MdiForm;
        frm.Show();
票数 0
EN

Stack Overflow用户

发布于 2015-01-24 13:27:23

我的代码实际上是正确的。此问题是由导致关闭意外关闭的另一个代码引起的。

票数 0
EN

Stack Overflow用户

发布于 2015-05-18 21:45:49

应用程序上下文应按如下方式初始化:

代码语言:javascript
运行
复制
aContext = new AppContext();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28102246

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档