【问题标题】:Argument Exception - the path is not of a legal form?参数异常 - 路径不是合法形式?
【发布时间】:2020-04-08 04:26:21
【问题描述】:

我希望创建一个使用“拖放”问题的测验,其中涉及用户将标签拖动到图片框,如果它们都匹配,则两者都会消失并且分数会更新。 为了实现这一点,我使用类来添加多个问题。 但是,出现运行时错误,我不知道如何修复它,因为它会阻止程序运行并出现相应的图片和标签

public partial class DragNDropQuestions : Form
    {
        public DragNDropQuestions()
        {
            InitializeComponent();
            AllowDrop();
            SetupQuestions();
            lblScore.Text = "Score: " + itemsleft;
        }
        int itemsleft = 0;


        private List<DragNDropClass> questions = new List<DragNDropClass>();

        private int _currentQuestion = 0;


        private void SetupQuestions()
        {
            questions.Add(new DragNDropClass(new[] {"Red","Blue","Yellow"},new[] {Image.FromFile("../../Resources/Blue.jpg"),Image.FromFile(" ../../Resources/Red.jpg"),Image.FromFile("../../Resources/Yellow.jpg") },new[] { "Blue", "Red", "Yellow" }));
            NextQuestion();
        }
        private void AllowDrop()
        {
            pictureBox1.AllowDrop = true;
            pictureBox2.AllowDrop = true;
            pictureBox3.AllowDrop = true;
        }

        private void DragNDropQuestions_Load(object sender, EventArgs e)
        {

        }

        private void LabelGrabbed(object sender, DragEventArgs e)
        {
            Label selectedlabel = (Label)sender;
            selectedlabel.DoDragDrop(selectedlabel.Text, DragDropEffects.Copy);
        }

        private void NextQuestion()
        {
            string[] labels = questions[_currentQuestion].LabelClass;
            Image[] pictures = questions[_currentQuestion].PictureBox;
            Random random = new Random();
            int rand = random.Next(0, 2);
            label1.Text = labels[0];
            label2.Text = labels[1];
            label3.Text = labels[2];
            pictureBox1.Image = (pictures[0]);
            pictureBox2.Image = (pictures[1]);
            pictureBox3.Image = (pictures[2]);


        }
        private void AnswerUpdate(object sender,DragEventArgs e)
        {
            string[] result = (string[])e.Data.GetData(DataFormats.Text);



            if (_currentQuestion < questions.Count - 1)
            {
                if (result == questions[_currentQuestion].CorrectAnswers)
                {
                    itemsleft++;
                    lblScore.Text = "Score: " + itemsleft;
                }
            }

        }

        private void PictureBox2_Click(object sender, EventArgs e)
        {

        }

        private void AllowDragDropCopy(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void WindowsDragDrop(object sender, DragEventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }
    }

如何克服这个问题并继续进行测验? 这是错误: enter image description here

   static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }

【问题讨论】:

  • 您好。请同时包含您得到的错误和堆栈跟踪

标签: c# class object runtime argumentexception


【解决方案1】:

我认为可能是" ../../Resources/Red.jpg" 开头的流氓空间导致了这里的问题。

如果这没有帮助,那么在没有更多信息的情况下很难确定,但看起来问题在于找到图像位置。这可能是因为路径是相对的;它们将被视为相对于当前工作文件夹,可能与正在执行的程序集的位置不同。

要调试它,我建议您先尝试评估图像的绝对路径。您可以尝试以下方法:

var currentDirectory = System.IO.Directory.GetCurrentDirectory(); 

var exePath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);

var absolutePath = Path.GetFullPath("../../Resources/Red.jpg");

测试此代码应该可以帮助您找出问题所在。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多