访问usercontrol文本框值到父窗体 [英] access usercontrol textbox values to parent form

查看:73
本文介绍了访问usercontrol文本框值到父窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将UserControl文本框值访问到父窗体,该窗体具有一个按钮和datagridview以添加这些文本框值

来自评论:

how to access usercontrol textbox values to parent form where it has a button and datagridview to add those textbox values

From comments:

/*Main Form*/
namespace UserControlsAndDataGridView
{
    public partial class Form1 : Form
    {
        private DataTable myTable = new DataTable();
        
        public Form1()
        {
            InitializeComponent();
            //set source
            this.dataGridView1.DataSource = this.myTable;
            
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           
            
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            UserControl1 frm = new UserControl1();
            // add row
            DataRow rd = this.myTable.NewRow();

            //set value
            rd[0] = userControl11.UserName;
            rd[1] = userControl11.SurName;
            rd[2] = userControl11.ParentName;
            rd[3] = userControl11.Qualification;

            //add row to table
            this.myTable.Rows.Add(rd);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }
    }
/*UserControl*/


{
    //declaring a delegate
    public delegate void myDelegateHandler(string textToString);
    
    public partial class UserControl1 : UserControl
    {
        //ParentForm pf;
        //event declaration
        public event myDelegateHandler myDelegate;
        public string UserName
        {
            get { return txtName.Text; }
            set { txtName.Text = value; }
        }
        public string SurName
        {
            get { return SurName; }
            set { SurName = value; }
        }
        public string ParentName
        {
            get { return ParentName; }
            set { ParentName = value; }
        }
        public string MobileNo
        {
            get { return MobileNo; }
            set { MobileNo = value; }
        }
        public string Qualification
        {
            get { return Qualification; }
            set { Qualification = value; }
        }
        public UserControl1()
        {
            InitializeComponent();
        }
        //public UserControl1(Form1 Parent)
        //{
        //    InitializeComponent();
        //    pf = new ParentForm();
        //    pf = Parent;

        //}

        private void UserControl1_Load(object sender, EventArgs e)
        {

        }


        
    }
}

/* designer*/
 partial class Form1
    {
        /// 

        /// Required designer variable.
        /// 

        private System.ComponentModel.IContainer components = null;

        /// 

        /// Clean up any resources being used.
        /// 

        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// 

        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// 

        private void InitializeComponent()
        {
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.Column5 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column4 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.btnAdd = new System.Windows.Forms.Button();
            this.userControl11 = new UserControlsAndDataGridView.UserControl1();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.Sus




和用户控件设计师



and
and usercontrol designer

private System.Windows.Forms.Label lblUserControls;
       private System.Windows.Forms.Label label1;
       private System.Windows.Forms.TextBox txtQualification;
       private System.Windows.Forms.TextBox txtMobileNo;
       private System.Windows.Forms.TextBox txtSurName;
       private System.Windows.Forms.TextBox txtParentName;
       private System.Windows.Forms.TextBox txtName;
       private System.Windows.Forms.Label lblQualification;
       private System.Windows.Forms.Label lblMobileNo;
       private System.Windows.Forms.Label lblParentName;
       private System.Windows.Forms.Label lblSurName;
       private System.Windows.Forms.Label lblName;

推荐答案

通过属性"公开它们是正常方法:
Exposing them via Properties is the normal way:
/// <summary>
/// Gets and sets the value of the input.
/// </summary>
public string Value
    {
    get { return tbValue.Text; }
    set { tbValue.Text = value; }
    }
/// <summary>
/// Gets and set the value of the prompt.
/// </summary>
public string Prompt
    {
    get { return labPrompt.Text; }
    set { labPrompt.Text = value; }
    }

然后,父表单仅在需要信息时访问属性.

The parent form then just accesses teh properties when it needs the information.


这篇关于访问usercontrol文本框值到父窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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