如何使用Windows窗体中的一个按钮控制多个用户控件? [英] How do I control multiple user controls with one button in Windows form?

查看:74
本文介绍了如何使用Windows窗体中的一个按钮控制多个用户控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以动态创建用户控件的表单,并且每个用户控件都有一个开始按钮和一个停止按钮.有没有一种方法可以在表格上具有全部开始"和全部停止"按钮?

I have a form that will dynamically create user controls, and each of those user controls has a start button and a stop button. Is there a way to have a start all and stop all button on the form?

我试图让一个布尔值在单击主窗体按钮时设置为true,但是一旦创建了用户控件,它就不会检查布尔值.

I tried having a Boolean that gets set true when the main form button is clicked, but once the user control is created it does not check the Boolean value.

这是主要形式的代码(vdoPlayer是用户控件):

Here is the code for the main form (vdoPlayer is the user control):

namespace AutoPopulateVideoSaving
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
            DisplayImage();
        }

        private void DisplayImage()
        {
            int h = 20;

            for (int i = 0; i < 6; i++)
            {
                int w = i % 2;
                vdoPlayer np = new vdoPlayer();

                np.Location = new System.Drawing.Point((33 + 408 * w), h);
                np.Name = "test" + i.ToString();
                np.Size = new System.Drawing.Size(408, 266);
                this.Controls.Add(np);
                h = h + (266 * w);
            }

        }

        private void StartAllBut_Click(object sender, EventArgs e)
        {

        }

        private void StopAllBut_Click(object sender, EventArgs e)
        {

        }    
    }
}

以下是用户控件的代码:

Here is the code for the user control:

namespace AutoPopulateVideoSaving
{
    public partial class vdoPlayer : UserControl
    {

        public vdoPlayer()
        {
            InitializeComponent();
            VariableClass2.InitiateVariables();
            JPEGStream jpegSource1 = new JPEGStream("http:// IP address /jpg/image.jpg?resolution=320x240");
            jpegSource1.Login = username;
            jpegSource1.Password = password;
            jpegSource1.NewFrame += new NewFrameEventHandler(jpegSource1_NewFrame);
            jpegSource1.VideoSourceError += new VideoSourceErrorEventHandler(jpegSource1_VideoSourceError);
            Player1.VideoSource = jpegSource1;
        }

        void jpegSource1_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            Bitmap image = new Bitmap(eventArgs.Frame, 320, 240);
            image.Save(someFile, System.Drawing.Imaging.ImageFormat.Bmp);
        }

        void jpegSource1_VideoSourceError(object sender, VideoSourceErrorEventArgs eventArgs)
        {
            //Error handler
            Debug.WriteLine(eventArgs.Description);
        }

        private void StartBut_Click(object sender, EventArgs e)
        {
            Player1.VideoSource.Start();
        }

        private void StopBut_Click(object sender, EventArgs e)
        {
            Player1.VideoSource.Stop();
        }    
    }
}

我不知道要在Form1的按钮单击事件中添加什么来控制其余部分.就像我之前说过的那样,我尝试使用布尔值,但是它没有用.这有可能吗?

I don't know what to put in Form1's button click events to control the rest. Like I said before, I tried using a Boolean, but it didn't work. Is this even possible?

推荐答案

首先, vdoPlayer 需要公开此功能,以供其他对象调用.像这样:

First, vdoPlayer needs to expose this functionality for other objects to invoke. Something like this:

public void StartVideo()
{
    Player1.VideoSource.Start();
}

public void StopVideo()
{
    Player1.VideoSource.Stop();
}

(一旦到位,该类的单击处理程序和其他功能应使用这些方法,而不是在上调用 Start() Stop()直接使用VideoSource ,只是为了获得更简洁的代码.)

(Once those are in place, that class' click handlers and other functionality should use those methods instead of invoking Start() and Stop() on VideoSource directly, just for cleaner code.)

此外,该表格应保留其所有相关控件的列表.这样的事情应该可以解决问题:

Additionally, the form should retain a list of all of its relevant controls. Something like this should do the trick:

public partial class Form1 : Form
{

    private List<vdoPlayer> videoPlayers;

    public Form1()
    {
        InitializeComponent();
        videoPlayers = new List<vdoPlayer>();
        DisplayImage();
    }

    // etc.
}

每当动态创建此类控件时,只需将其添加到该列表中即可.销毁后,将其从列表中删除.(您可以跳过显式列表,并尝试动态遍历 Controls 集合以动态构建列表,但这会变得很丑陋.)

Any time such a control is dynamically created, simply add it to that list. When it's destroyed, remove it from the list. (You could skip the explicit list and try to dynamically traverse Controls collections to build the list on the fly, but that can get pretty ugly.)

现在,表单的按钮只需要调用视频播放器控件上的操作即可:

Now your form's buttons just need to invoke the operations on the video player controls:

private void StartAllBut_Click(object sender, EventArgs e)
{
    foreach (var videoPlayer in videoPlayers)
        videoPlayer.StartVideo();
}

private void StopAllBut_Click(object sender, EventArgs e)
{
    foreach (var videoPlayer in videoPlayers)
        videoPlayer.StopVideo();
}  

这篇关于如何使用Windows窗体中的一个按钮控制多个用户控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆