How to pass values to a user control in winforms c#

前端 未结 2 741
天命终不由人
天命终不由人 2021-01-25 15:22

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\'

相关标签:
2条回答
  • 2021-01-25 15:39

    Your UserControls constructor gets run when you new up your UserControl, you are setting your values after the fact. I would personally either create a constructor that you can pass in your initial settings or make a method that you execute to initialize it after you populate your values.

    Something like this:

    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;
    
        }
    }
    
    0 讨论(0)
  • 2021-01-25 15:46

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

    I should have put

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

    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();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题