如何将 form1 中的变量与用户控件一起使用



我在项目中创建了一个用户控件:右键单击项目名称>添加>用户控件。

然后在用户控件设计器中,我添加了一些控件。在用户控件构造函数中,我做到了:

public DopplerEffect()
        {
            InitializeComponent();
            InitGifFile = @"C:UsersmeAppDataLocalmwsmwstemp_directoryfile000050.gif";
            pen = new Pen(Color.Red, 3);
            formloadfirsttime = true;
            numericUpDown1.Value = 200;
            brush = new SolidBrush(Color.Red);
            trackBar2.Minimum = 1;
            trackBar2.Enabled = false;
            bmpnew = new Bitmap(512, 512);
            label1.Text = "";
            path_exe = Path.GetDirectoryName(Application.LocalUserAppDataPath);
            ScanClouds = Path.Combine(path_exe, ScanClouds);
            if (!Directory.Exists(ScanClouds))
            {
                Directory.CreateDirectory(ScanClouds);
            }
            ConvertedBmpDir = Path.Combine(ScanClouds, ConvertedBmpDir);
            if (!Directory.Exists(ConvertedBmpDir))
            {
                Directory.CreateDirectory(ConvertedBmpDir);
            }               
            b = new Bitmap(InitGifFile);
            b1 = new Bitmap(InitGifFile);
            pictureBox1.Image = b;
            ConvertedBmp = ConvertTo24(InitGifFile);
            mymem = ToStream(ConvertedBmp, ImageFormat.Bmp);
            startButton.Enabled = true;
            pauseButton.Enabled = false;
        }

现在我正在为 gif 文件使用静态目录:

问题是当我将用户控件拖动到 form1 设计器时,它将处理文件:file000050.gif

但是我希望它使用我在form1代码中的某个地方拥有的文件,我有一个名为:next_file

因此,当我在form1设计器中拖动用户控件时,我需要以某种方式自动next_file因此InitGifFile改为file000050.gif应该是next_file

我该怎么做?

您必须将属性添加到您的用户控件...类似的东西(在你的代码中)

public partial DopplerEffect : UserControl 
{
    private string m_nextFile;
    public string NextFile
    {
        get { return m_nextFile; }
        set 
        { 
           m_nextFile = value; 
           DoSomethingWithNextFile(); // do loading of your image
        }
    }
    public DopplerEffect() { ... }
}

因此,当您将用户控件拖到窗体上时,您现在有一个可以设置的属性 NextFile。

最新更新