C#:当我运行程序并且Visual Studio尝试创建一个新类时,第二个表单没有加载?

时间:2015-11-13 06:44:45

标签: c# winforms visual-studio visual-studio-2012

我一直在研究这个非常简单的程序,该程序使用组合框和列表来显示“书籍”,允许打印和预览,并且作业告诉我创建表单“关于表单” ,这是在单击“关于”选项卡时加载的表单。但是每次我点击它,VS都会创建一个新类,我得到了这个

  

“未执行例外”

错误,虽然程序没有任何错误。不确定为什么Visual Studio尝试为“about form”创建一个新的类/代码,即使我已经创建了它。我认为通过VS生成东西会有所帮助,但程序仍然不能正常工作。

这是我的关于表单代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CS10AboutForm.cs
{
    public partial class frmAbout : Form
    {
        public frmAbout()
        {
            InitializeComponent();
        }



        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

我尝试加载表单时遇到的错误和由VS 创建的类:

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

namespace CS10.cs
{
    class frmAbout
    {
        internal void ShowDialog()
        {
            throw new NotImplementedException();
        }
    }
}

我使用“约”形式的其他表单的代码:

namespace CS10.cs
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void lblEdit_Click(object sender, EventArgs e)
        {

        }

        private void menuStrip2_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void addBookToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Add a new book to the list
            if (cboBooks.Text != "")
            {
                cboBooks.Items.Add(cboBooks.Text);
                cboBooks.Text = "";
            }
            else
            {
                MessageBox.Show("Enter a book to add", "Missing data",
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            cboBooks.Focus();

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void mnuEditRemove_Click(object sender, EventArgs e)
        {
            // Remove the selected book from the list
            if (cboBooks.SelectedIndex != -1)
            {
                cboBooks.Items.RemoveAt(cboBooks.SelectedIndex);
            }
            else
            {
                MessageBox.Show("First select a book to remove",
                    "No selection made", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

        }

        private void mnuEditClear_Click(object sender, EventArgs e)
        {
            // Clear the book list
            DialogResult responseDialogResult;

            responseDialogResult = MessageBox.Show("Clear the book list?",
                "Clear book list", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (responseDialogResult == DialogResult.Yes)
            {
                cboBooks.Items.Clear();
            }

        }

        private void mnuEditCount_Click(object sender, EventArgs e)
        {
            // Display a count of the books on the list
            MessageBox.Show("The number of book types is " + cboBooks.Items.Count.ToString());

        }

        private void mnuHelpAbout_Click(object sender, EventArgs e)
        {
            // Create an instance and display frmAbout 
            frmAbout frmAboutObj = new frmAbout();
            frmAboutObj.ShowDialog();   //.ShowDialog displays as a Model Form

        }

        private void mnuFileExit_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        private void mnuFilePrintPreview_Click(object sender, EventArgs e)
        {
            // Begin print preview by assigning PrintDocument
            printPreviewDialog1.Document = printDocument1;
            printPreviewDialog1.ShowDialog();

        }

        private void mnuFilePrint_Click(object sender, EventArgs e)
        {
            // Print by calling the Print method
            printDocument1.Print();
        }
        private void printDocument1_PrintPage(object sender,
               System.Drawing.Printing.PrintPageEventArgs e)
        {
            // Handle printing and print previews
            // printPreviewDialog1.ShowDialog() and printDocument1.Print() trigger
            //  PrintPage event.

            Font printFont = new Font("Arial", 12);
            Font headingFont = new Font("Arial", 14, FontStyle.Bold);
            float fltLineHeight = printFont.GetHeight();
            float fltPrintX = e.MarginBounds.Left;
            float fltPrintY = e.MarginBounds.Top;
            string strPrintLine;

            // Print Headings
            strPrintLine = "Book List ";
            e.Graphics.DrawString(strPrintLine, headingFont,
                Brushes.Black, fltPrintX, fltPrintY);
            strPrintLine = "By Lina";
            fltPrintY += fltLineHeight;
            e.Graphics.DrawString(strPrintLine, headingFont,
                Brushes.Black, fltPrintX, fltPrintY);

            // Leave a blank line between heading and detail line
            fltPrintY += fltLineHeight * 2;

            // Loop through the entire list
            for (int intIndex = 0; intIndex <= cboBooks.Items.Count - 1; intIndex++)
            {
                // Set up a line
                strPrintLine = cboBooks.Items[intIndex].ToString();
                // Send the line to the graphics page object
                e.Graphics.DrawString(strPrintLine, printFont,
                    Brushes.Black, fltPrintX, fltPrintY);

                // Increment the Y position for the next line
                fltPrintY += fltLineHeight;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void printPreviewDialog1_Load(object sender, EventArgs e)
        {

        }

        private void mnuFileExit_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void mnuFilePrint_Click_1(object sender, EventArgs e)
        {

            // Call the print process by calling the Print method
            printDocument1.Print();

        }

        private void mnuFilePrintPreview_Click_1(object sender, EventArgs e)
        {
            // Begin print preview by assigning PrintDocument
            printPreviewDialog1.Document = printDocument1;
            printPreviewDialog1.ShowDialog();

        }

        private void mnuEditRemove_Click_1(object sender, EventArgs e)
        {
             // Remove the selected book from the list
            if (cboBooks.SelectedIndex != -1)
            {
                cboBooks.Items.RemoveAt(cboBooks.SelectedIndex);
            }
            else
            {
                MessageBox.Show("First select a book to remove",
                    "No selection made", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }


        }

        private void mnuEditClear_Click_1(object sender, EventArgs e)
        {
            // Clear the book list
            DialogResult responseDialogResult;

            responseDialogResult = MessageBox.Show("Clear the book list?",
                "Clear book list", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (responseDialogResult == DialogResult.Yes)
            {
                cboBooks.Items.Clear();
            }

        }

        private void mnuEditCount_Click_1(object sender, EventArgs e)
        {
            // Display a count of the books on the list
            MessageBox.Show("The number of book types is " + cboBooks.Items.Count.ToString());
        }

        private void mnuHelpAbout_Click_1(object sender, EventArgs e)
        {
            frmAbout frmAboutObj = new frmAbout();
            frmAboutObj.ShowDialog();   //.ShowDialog displays as a Modal Form
        }       
    }
}

同样,一些代码是生成的,因为它可以解决问题,所以如果它混乱或不必要地长,我会道歉。请帮忙?谢谢。

2 个答案:

答案 0 :(得分:0)

摆脱frmAbout.ShowDialog()

中的throw new NotImplementedException();

答案 1 :(得分:0)

我决定从头开始重新使用我的大部分代码。问题是我添加了&#34;关于表格&#34;不正确;我应该选择项目&gt;添加表单而不是启动一个全新的项目。我最初也是以#34;关于Form&#34;因为我误读了指示。