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

前端 未结 2 740
天命终不由人
天命终不由人 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;
    
        }
    }
    

提交回复
热议问题