簡體   English   中英

部分嵌套的Form類與設計器

[英]Partial nested Form class with designer

我正在創建類似MessageBox(MessageBoxCustom)。 我想在一個單獨的文件中有一個帶有設計器支持的表單,這樣我就可以通過Visual Studio(MessageBoxCustomDialog)修改外觀。

我還想通過MyMessageBox外部的代碼使這個MessageBoxCustomDialog無法訪問,我正在嵌套MessageBoxCustomDialog。 我想將它移到一個單獨的文件中,所以我有設計師的支持。 也許使用部分課程? 等級怎么樣?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace System.Windows.Forms
{
    public static class MessageBoxCustom
    {
        public static void Show()
        {
            (new MessageBoxCustomDialog()).ShowDialog();
        }

        private class MessageBoxCustomDialog : Form
        {

        }
    }
}

Visual Studio Designer無法幫助您設計嵌套類。 它不是為此而做的。 它檢查文件中第一個最外層類的類型,然后決定使用哪個設計器。

如果只是設計表單的布局,我建議像往常一樣設計它。 完成項目后,您可以通過外部類(在兩個文件中)包圍該類並將其設為私有。

當您完成工作時,只需將對話框類復制並粘貼到外部類中,然后將其設置為私有。 如果你必須重新設計它,它只是復制和粘貼。

MessageBoxCustomDialog.cs:

namespace System.Windows.Forms
{
    // make sure this is the first class in the file (required by designer)
    public partial class MessageBoxCustomDialog : Form
    {
        public MessageBoxCustomDialog()
        {
            InitializeComponent();
        }
    }

    public static partial class MessageBoxCustom
    {
        public static void Show()
        {
            new MessageBoxCustomDialog().ShowDialog();
        }

        // put the MessageBoxCustomDialog class here when you are done
    }
}

MessageBoxCustomDialog.Designer.cs:

namespace System.Windows.Forms
{
    partial class MessageBoxCustomDialog
    {
        ...
    }

    partial class MessageBoxCustom
    {
        // put the MessageBoxCustomDialog class here when you are done
    }
}

使MessageBoxCustomDialog成為私有的部分內部類

private partial class MessageBoxCustomDialog : Form
{}

您必須使MessageBoxCustom部分具有相同的MessageBoxCustomDialog范圍

檔案1

using System.Windows.Forms;

namespace System.Windows.Forms
{
    public static partial class MessageBoxCustom
    {
        public static void Show()
        {
            (new MessageBoxCustomDialog()).ShowDialog();
        }

        private partial class MessageBoxCustomDialog : Form
        {

        }
    }
}

檔案2

using System.Windows.Forms;

namespace System.Windows.Forms
{
    public static partial class MessageBoxCustom
    {
        private partial class MessageBoxCustomDialog : Form
        {
            // designer code
        }
    }
}

你可能會看到這個鏈接http://msdn.microsoft.com/en-us/library/wa80x488.aspx [限制部分]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM