Domanda

I am using mcs version 2.10.8.1 in Ubuntu 12.04, I have the following code:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.ShowDialog();

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                string fileName;
                fileName = dlg.FileName;
                MessageBox.Show(fileName);
            }
        }
    }
}

I compile using the command

$ mcs source_code.cs -r:System.Windows.Forms.dll -r:System.Drawing.dll 

And I receive the error

source_code.cs(11,13): error CS0103: The name `InitializeComponent' does not exist in the current context
Compilation failed: 1 error(s), 0 warnings

I've seen many answers to this question in cases using Visual Basic; I would like to know what I should do to solve this problem. Thanks.

È stato utile?

Soluzione

Was your C# code originally created in Visual Studio? If so, then you'll probably have a Form1.Designer.cs file as well as the file containing the code that you've written by hand. You need to include the file in the command line.

If this isn't C# code originally created in Visual Studo, you may not even have an InitializeComponent method... but in that case you'll need more code to do anything useful in your form (like creating the button and hooking up its Click event).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top