Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

How to manipulate a user control's custom property?

$
0
0

Hi! I'd like to ask as to how am I going to able to change my user control's property when i run my application? I used this code:

USER CONTROL:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class UserControl1 : UserControl
    {

        public UserControl1()
        {
            InitializeComponent();
        }

        public string UserID
        {
            get
            {
                return textBox1.Text;
            }
            set
            {
                textBox1.Text = value;
            }
        }
    }
}

But it only works when I edit the UserID manually in the properties window of my form after I included my user control to the form.

Here is the code as well whenever I want to change my the property of my user control's text box using the form:

FORM:

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;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        UserControl1 control = new UserControl1();
        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            control.UserID = "CHANGE!!!";
        }
    }
}

If its possible, I do hope you guys can help me out since this is my first time creating a user control and I want to create a user control sa I can create a group of controls under 1 control and use properties to manipulate the values being passed to the controls of inside my user control and also for me to be able to instantiate my user control whenever I need it to appear in my form multiple times without adding it manually on design view.

Thanks for those who will help :)



Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>