加载Visual Studio解决方案会在设计器中产生未处理的异常:“找不到路径(文件路径)的一部分”

时间:2019-02-24 05:03:06

标签: c# winforms stack-trace windows-forms-designer designer

我加载我的项目,然后弹出这两个窗口,一个接一个(我在下面解释了第一个窗口):

First window

Second window

但是有时显示的第二个窗口是这个窗口:

Second window

但是,在第二个第二个窗口中显示了一个模式。

我注意到,如果我通过在驱动器上的文件资源管理器>解决方案位置(不是在Visual Studio中,文件>打开)中双击解决方案文件来加载我的解决方案,那么第二个显示G驱动器的窗口(我具有解决方案的笔式驱动器。)

但是,如果我通过加载Visual Studio来加载解决方案,然后选择“文件”>“打开”并选择解决方案(在同一G驱动器上),则第二个显示参考C驱动器的窗口。

假设显示的第一个窗口表明,每次加载解决方案时,都会在用户控件中以主窗体调用一种方法:

private void EndQuiz()
    {
        this.Visible = false;
        questionNumber = 1;
        MessageBox.Show("You finished the quiz with " + score + " score", "Complete");
        score = 0;
    }

此用户控件的类具有EndQuiz()方法(您会在大约一半的位置找到它):

public partial class QuizUC : UserControl
{
    public Label LabelTheme { get { return labelTheme; } }
    public Label LabelQuestionNumber { get { return labelQuestionNumber; } }
    public Label LabelQuestion { get { return labelQuestion; } }
    public Label LabelScore { get { return labelScore; } }

    public Button BtnOption1 { get { return btnOption1; } }
    public Button BtnOption2 { get { return btnOption2; } }
    public Button BtnOption3 { get { return btnOption3; } }
    public Button BtnOption4 { get { return btnOption4; } }

    public PictureBox PictureBox1 { get { return pictureBox1; } }

    Random rnd;
    Bitmap quizImage;

    public string correctAnswer;

    int correctAnswerNo = 0, questionsAsked = 0, questionNumber, rndQuestionNo, score;
    string question, answer;
    string[] individualAnswers;
    bool isOver = false;

    List<string> questions;
    List<int> usedQuestions;
    List<string> answers;

    private int _noOfQuestions;
    private string _selectedTheme = "";

    // quiz not loading on second time DONE
    // selected 5 questions, gave 6 DONE
    // duplicate questions
    // label question overlapping with buttons

    public QuizUC()
    {
        InitializeComponent();
    }

    public void ShowAll()
    {
        labelTheme.Show();
        labelQuestion.Show();
        labelQuestionNumber.Show();
        btnOption1.Show();
        btnOption2.Show();
        btnOption3.Show();
        btnOption4.Show();
        labelScore.Show();
        pictureBox1.Show();
    }

    public void SetUpVariables(int noOfQuestions, string selectedTheme)
    {
        _noOfQuestions = noOfQuestions < 5 || noOfQuestions > 15 ? 5 : noOfQuestions;
        _selectedTheme = selectedTheme ?? "";
    }

    private void QuizUC_VisibleChanged(object sender, EventArgs e)
    {
        if (this.Visible)
        {
            SetUpQuiz();
        }
    }

    private void QuizUC_Load(object sender, EventArgs e)
    {
        ReadQuestionsAndAnswersFromFile();
    }

    private void SetUpQuiz()
    {
        SetLabelSizes(); // question text will not go off the edge
        labelScore.Text = "Score: 0";
        SetTransparency();
        SetQuestionNumber();

        usedQuestions = new List<int>();

        questionsAsked = 0;
        questionNumber = 1;
        score = 0;

        RandomiseQuestion();
        labelTheme.Text = "Theme: Space";
        labelQuestionNumber.Text = $"Question number: {questionNumber}";

        quizImage = new Bitmap("solarsystem.jpg");
        pictureBox1.Image = quizImage;
    }

    private void ReadQuestionsAndAnswersFromFile()
    {
        string questionFile = "quiz/questions.txt", answerFile = "quiz/answers.txt";
        questions = File.ReadAllLines(questionFile).ToList();
        answers = File.ReadAllLines(answerFile).ToList();
    }

    private void SetLabelSizes()
    {
        labelQuestion.MaximumSize = new Size(labelQuestion.Width, 0);
        labelQuestion.AutoSize = true;
    }

    public void SetTransparency()
    {
        labelQuestion.BackColor = Color.Transparent;
        labelTheme.BackColor = Color.Transparent;
        labelQuestionNumber.BackColor = Color.Transparent;
        labelScore.BackColor = Color.Transparent;
    }

    public void SetQuestionNumber()
    {
        labelQuestionNumber.Text = "Question number: " + questionNumber;
    }

    private void EndQuiz()
    {
        this.Visible = false;
        questionNumber = 1;
        MessageBox.Show("You finished the quiz with " + score + " score", "Complete");
        score = 0;
    }

    public void ResetQuiz()
    {
        questionsAsked = 0;
        score = 0;
        labelScore.Text = "Score " + score;
        questionNumber = 1;
        questions = File.ReadAllLines("quiz/questions.txt").ToList();
        answers = File.ReadAllLines("quiz/answers.txt").ToList();
        usedQuestions.Clear();
    }

    public void RandomiseQuestion()
    {
        questionsAsked++;
        if (questionsAsked == _noOfQuestions + 1)
        {
            EndQuiz();
            ResetQuiz();
            return;
        }
        rnd = new Random();

        // ShowAll();
        rndQuestionNo = rnd.Next(0, questions.Count);

        question = questions.Count == 1 ? questions[0] : questions[rndQuestionNo];
        answer = answers[rndQuestionNo];

        questions.RemoveAt(rndQuestionNo);
        answers.RemoveAt(rndQuestionNo);

        individualAnswers = answer.Split(',');
        for (int i = 0; i < individualAnswers.Length; i++)
        {
            if (individualAnswers[i].Contains('*'))
            {
                correctAnswerNo = i;
            }
        }

        individualAnswers[correctAnswerNo] = individualAnswers[correctAnswerNo].Trim('*');
        correctAnswer = individualAnswers[correctAnswerNo];

        labelQuestion.Text = question;
        btnOption1.Text = individualAnswers[0];
        btnOption2.Text = individualAnswers[1];
        btnOption3.Text = individualAnswers[2];
        btnOption4.Text = individualAnswers[3];
    }

    private void IncreaseScore(int score)
    {
        labelScore.Text = "Score: " + score.ToString();
    }

    private bool CheckToReturn()
    {
        var noOfQuestions = Single.GetSingle().AskHowManyQuestionsForm.NoOfQuestions;

        if (questionNumber == noOfQuestions)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    private void btnOption1_Click(object sender, EventArgs e)
    {
        if (btnOption1.Text == correctAnswer)
        {
            MessageBox.Show("Correct answer!", "Correct");
            questionNumber++;
            SetQuestionNumber();
            score++;
            IncreaseScore(score);
            isOver = CheckToReturn();
            RandomiseQuestion();
        }
        else
        {
            MessageBox.Show("Incorrect answer!", "Incorrect");
            questionNumber++;
            SetQuestionNumber();
            isOver = CheckToReturn();
            RandomiseQuestion();
        }
    }

    private void btnOption2_Click(object sender, EventArgs e)
    {
        if (btnOption2.Text == correctAnswer)
        {
            MessageBox.Show("Correct answer!", "Correct");
            questionNumber++;
            SetQuestionNumber();
            score++;
            IncreaseScore(score);
            isOver = CheckToReturn();
            RandomiseQuestion();
        }
        else
        {
            MessageBox.Show("Incorrect answer!", "Incorrect");
            questionNumber++;
            SetQuestionNumber();
            isOver = CheckToReturn();
            RandomiseQuestion();
        }
    }

    private void btnOption3_Click(object sender, EventArgs e)
    {
        if (btnOption3.Text == correctAnswer)
        {
            MessageBox.Show("Correct answer!", "Correct");
            questionNumber++;
            SetQuestionNumber();
            score++;
            IncreaseScore(score);
            isOver = CheckToReturn();
            RandomiseQuestion();
        }
        else
        {
            MessageBox.Show("Incorrect answer!", "Incorrect");
            questionNumber++;
            SetQuestionNumber();
            isOver = CheckToReturn();
            RandomiseQuestion();
        }
    }

    private void btnOption4_Click(object sender, EventArgs e)
    {
        if (btnOption4.Text == correctAnswer)
        {
            MessageBox.Show("Correct answer!", "Correct");
            questionNumber++;
            SetQuestionNumber();
            score++;
            IncreaseScore(score);
            isOver = CheckToReturn();
            RandomiseQuestion();
        }
        else
        {
            MessageBox.Show("Incorrect answer!", "Incorrect");
            questionNumber++;
            SetQuestionNumber();
            isOver = CheckToReturn();
            RandomiseQuestion();
        }
    }
}

每当我在Visual Studio中打开“菜单设计”窗口时,都会显示:

Menu design error

这里可能是什么问题?很抱歉,如果我没有包含所有必需的代码,请告诉我如果需要还包括哪些内容。感谢您阅读所有这些内容。

编辑:查看评论以获取更多信息。

此外,在此之前我有一个灰色背景,但现在窗体看起来像这样,后面有错误消息(我将其他控件拖出了非QuizUC的方式,正如注释中所解释的那样,它从设计中消失了视图):

Menu design screen error

0 个答案:

没有答案