如何在winforms c#中将值传递给用户控件 [英] How to pass values to a user control in winforms c#

查看:51
本文介绍了如何在winforms c#中将值传递给用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 winforms 应用程序,我在其中以编程方式创建用户控件并将值传递给它.当我运行程序时,用户控件中的所有变量都为空.我不知道我做错了什么.当我查找类似的程序时,看起来我有相同的代码,但它不起作用.也许这里有人可以提供帮助.

I have a winforms app that where I programmatically create a user control and pass values to it. When I run the program all the variables in the user control are null. I don't know what I'm doing wrong. When I look up similar programs it looks like I have the same code, but it's not working. Maybe someone on here can help.

这里是主要的表单代码:

Here is the main form code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Net;

namespace AddPanel
{
    public partial class Form1 : Form
    {

        public Form1()
        {

            InitializeComponent();

            DisplayImage();
        }


        private void DisplayImage()
        {

            FileStream fs = new FileStream("ntst.jpg", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);

            cameraTable = (from x in db.CamTable1s
                           select x).ToList();

            test nt = new test();
            nt.Location = new System.Drawing.Point(33, 20);
            nt.Name = "test1";
            nt.usrID = "username";
            nt.IPadd = "ipaddress";
            nt.pswd = "password";
            nt.ws = fs;
            nt.Size = new System.Drawing.Size(408, 266);
            this.Controls.Add(nt);

        }    

    }
}

用户控制代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Net;

using AForge;
using AForge.Video;
using AForge.Controls;
using AForge.Video.DirectShow;

namespace AddPanel
{
    public partial class test : UserControl
    {
        public string IPadd { get; set; }
        public string usrID { get; set; }
        public string pswd { get; set; }
        public string filename { get; set; }
        public FileStream ws { get; set; }


        public test()
        {

            JPEGStream jpegSource1 = new JPEGStream("http://" + IPadd + "/jpg/image.jpg?resolution=320x240");
            jpegSource1.Login = usrID;
            jpegSource1.Password = pswd;
            jpegSource1.NewFrame += new NewFrameEventHandler(jpegSource1_NewFrame);
            //source1.VideoSourceError += new VideoSourceErrorEventHandler(source1_VideoSourceError);
            jpegSource1.VideoSourceError += new VideoSourceErrorEventHandler(jpegSource1_VideoSourceError);
            Player1.VideoSource = jpegSource1;

            InitializeComponent();
        }

        void jpegSource1_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            Bitmap image = new Bitmap(eventArgs.Frame, 320, 240);

            image.Save(ws, System.Drawing.Imaging.ImageFormat.Bmp);
        }

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

            Bitmap ErPic = new Bitmap(320, 240);
            using (var g = Graphics.FromImage(ErPic))
            {
                using (var arialFontLarge = new Font("Arial", 15))
                {
                    g.DrawString("Camera Offline", arialFontLarge, Brushes.White, 75, 100);
                }

            }
            ErPic.Save(ws, ImageFormat.Bmp);
        }

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

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

推荐答案

问题是我对在表单之间使用变量的理解是错误的.

The problem was that my understanding of using variables between forms was wrong.

我应该把

public string IPadd { get; set; }
public string usrID { get; set; }
public string pswd { get; set; }
public string filename { get; set; }
public FileStream ws { get; set; }

在 Form1 中,然后在测试中访问它们.像这样:表格1

in Form1 and then accessed them in test. Like this: Form1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Net;

using AForge.Video;
using AForge.Controls;
using AForge.Video.DirectShow;

namespace AddPanel
{
    public partial class Form1 : Form
    {
        private string iPadd;
        public string IPadd
        {
            get { return iPadd;}
            set
            {
                iPadd = value;

            }
        }

        private string usrID;
        public string UsrID
            {
            get { return usrID; }
            set
            {
                usrID = value;
            }
        }

        private string Pswd;
        public string pswd
        { 
            get{return Pswd;}
            set
            {
                Pswd = value;
            }
        }
        private string Filename;
        public string filename
        {
            get { return Filename; }
            set
            {
                Filename = value;
            }
         }

        public Form1()
        {


            InitializeComponent();


            DisplayImage();
        }


        private void DisplayImage()
        {



            FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);


            UsrID = "webservice";
            IPadd = "10.108.212.100";
            pswd = "E4emw@tch!ngU";
            WS = fs;

            test nt = new test();
            nt.Location = new System.Drawing.Point(33, h);
            nt.Name = "test1";
            nt.Size = new System.Drawing.Size(408, 266);
            this.Controls.Add(nt);

        } 

    }
}

然后我可以像这样在测试中访问这些变量:

Then I can access those variables in test like this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Net;

using AForge;
using AForge.Video;
using AForge.Controls;
using AForge.Video.DirectShow;

namespace AddPanel
{
    public partial class test : UserControl
    {    

        public test()
        {
            Form1 f1 = new Form1();
            JPEGStream jpegSource1 = new JPEGStream("http://" + f1.IPadd + "/jpg/image.jpg?resolution=320x240");
            jpegSource1.Login = f1.UsrID;
            jpegSource1.Password = f1.pswd;
            jpegSource1.NewFrame += new NewFrameEventHandler(jpegSource1_NewFrame);
            //source1.VideoSourceError += new VideoSourceErrorEventHandler(source1_VideoSourceError);
            jpegSource1.VideoSourceError += new VideoSourceErrorEventHandler(jpegSource1_VideoSourceError);
            Player1.VideoSource = jpegSource1;

            InitializeComponent();
        }

        void jpegSource1_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            Bitmap image = new Bitmap(eventArgs.Frame, 320, 240);

            image.Save(ws, System.Drawing.Imaging.ImageFormat.Bmp);
        }

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

            Bitmap ErPic = new Bitmap(320, 240);
            using (var g = Graphics.FromImage(ErPic))
            {
                using (var arialFontLarge = new Font("Arial", 15))
                {
                    g.DrawString("Camera Offline", arialFontLarge, Brushes.White, 75, 100);
                }

            }
            ErPic.Save(ws, ImageFormat.Bmp);
        }

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

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

这篇关于如何在winforms c#中将值传递给用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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