在同一个项目中有两个CustomMessageBox

时间:2017-08-22 15:56:34

标签: c# winforms

我有一个Windows.Forms应用程序。它有一个菜单栏,有一个按钮。当我单击该按钮时,另一个表单打开(“CustomMessageBox”)。

这个CustomMessageBox有一个ListView,一些RichTextBox,一些标签和几个按钮。它几乎只是一种向列表添加数据的表单。一切顺利,良好,按预期工作。

以下是当前CustomMessageBox的代码:

namespace CustomMessageBoxNS {
    public partial class CustomMessageBox : Form {
        static Dictionary<string, string> currentDictionary;
        static CustomMessageBox MyBox;
        static ListView macroList;
        static Form thisWindow;
        static Label firstLabel;
        static Label secondLabel;

        public CustomMessageBox() {
            InitializeComponent();

            macroList = cmb_lb_macroList;
            firstLabel = cmb_lbl_name;
            secondLabel = cmb_lbl_action;
            thisWindow = this;
        }

        public static Dictionary<string, string> Show(Dictionary<string, string> inData, string title, string label1, string label2) {
            MyBox = new CustomMessageBox();
            currentDictionary = inData;

            thisWindow.Text = title;
            firstLabel.Text = label1;
            secondLabel.Text = label2;

            updateListBox();
            MyBox.ShowDialog();

            return currentDictionary;
        }

    //Plus a whole bunch of other methods which aren't relevant for my question.
    }
}

CustomMessageBox的用法如下:

macroDictionary = CustomMessageBox.Show(macroDictionary, "Edit Macros", "Name:", "Macro:");

这样做显示CustomMessageBox就像普通的MessageBox一样,没什么奇怪的。

现在我的问题: 我想创建另一个CustomMessageBox - 它看起来与当前的(其他Controls)完全不同。我试过:Add - &gt;现有项目 - &gt;浏览到我的CustomMessageBox项目 - &gt;选择类文件。但是当我导入它时,我得到了一大堆错误。 “两个相同”使他们发生冲突。

当我不知道它是什么情况时,很难描述它。我的猜测是我在同一个项目中不能有两个类具有相同的“公共部分类CustomMessageBox:Form”

我得到了一大堆错误,但最有说服力的是:“以下方法或属性之间的调用不明确。”

现在我想知道我是如何解决的,所以我可以拥有“两个相同但不同的”?

1 个答案:

答案 0 :(得分:0)

为新类赋予另一个名称或将其放在不同的名称空间中。

位于同一程序集的同一名称空间中的两个类显然不能具有相同的名称。

如果您决定将新类放在不同的命名空间中:

namespace NewNamespace {
    public partial class CustomMessageBox : Form {
...

...您可以使用完全限定名称来引用它:

NewNamespace.CustomMessageBox.Show(...);

...或者在您使用它的代码文件的顶部添加using指令:

using NewNamespace;

但最重要的是让新课程成为一个独特的名称。

相关问题