【问题标题】:How to pass values to a user control in winforms c#如何在winforms c#中将值传递给用户控件
【发布时间】:2014-11-24 23:05:51
【问题描述】:

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

这里是主要的表单代码:

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();
        }
    }
}

【问题讨论】:

  • 您在何处/何时看到这些空值
  • 你的意思是传递给 jpegSource1 的值是空的吗?您在用户控件的构造函数中填充它们并且之前没有初始化它们。因此,它们将具有字符串的默认值(空)和类的空值。
  • @BradleyDotNET 当我运行调试并检查用户控件中的“IPadd”时,它们为空。
  • 但是在什么。如果您签入构造函数,则分配尚未发生。如果你事后检查,这似乎是一个严重的问题。

标签: c# winforms user-controls


【解决方案1】:

您的 UserControls 构造函数在您新建 UserControl 时运行,您在事后设置值。我个人会创建一个可以传入初始设置的构造函数,或者创建一个在填充值后执行的方法来初始化它。

类似这样的:

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()
    {
        InitializeComponent();
    }
    public test(string Ip, string Id, string Pass, string file, FileStream stream )
    {
        InitializeComponent();

        IPadd = Ip;
        usrID = Id;
        pswd = Pass;
        filename = file;
        ws = stream;

        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);
        pegSource1.VideoSourceError += new VideoSourceErrorEventHandler(jpegSource1_VideoSourceError);
        Player1.VideoSource = jpegSource1;

    }
}

【讨论】:

  • 这设置了我想要的值,谢谢。现在我需要找出另一个错误:当我单击开始按钮时,它给出了“Player1.VideoSource.Start();”的错误。有什么想法吗?
  • 错误是什么?您的开始按钮是在用户控件上还是在包含表单上?
  • NullReferenceException 未处理。它在用户控件中。
  • 在该行放一个断点并检查什么是空的。 Player1 或视频源
  • 看起来 Videosource 为空。
【解决方案2】:

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

我应该放

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

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);

        } 

    }
}

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

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();
        }
    }
}

【讨论】:

  • 您实际上是在您的用户控件中创建一个新的 Form1,它与托管您的用户控件的 Form1 是分开的。如果您打算这样做,请将您的 form1 分配给 usercontrols 父属性,然后将其转换为 form1。我仍然会按照我在回答中建议的方式进行操作,这样您的用户控件就不需要知道其父级的内部结构。
  • 我现在遇到了这个问题。我会查一下你的建议
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多