C#调用用户控件中的公共方法

首先需要将用户控件定义为public的,从工具箱拖动到窗口的,转到Form的InitializeComponent();方法,将其改为public.

namespace JointOfficial.Forms
{
    partial class DemoForm
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <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

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.clientFindMain1 = new JointOfficial.UserControls.ClientFindMain();
            this.SuspendLayout();
            // 
            // clientFindMain1
            // 
            this.clientFindMain1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.clientFindMain1.Location = new System.Drawing.Point(0, 0);
            this.clientFindMain1.Name = "clientFindMain1";
            this.clientFindMain1.Size = new System.Drawing.Size(284, 262);
            this.clientFindMain1.TabIndex = 0;
            // 
            // DemoForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.clientFindMain1);
            this.Name = "DemoForm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "DemoForm";
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.Load += new System.EventHandler(this.DemoForm_Load);
            this.ResumeLayout(false);

        }

        #endregion

        public UserControls.ClientFindMain clientFindMain1;//用户控件在此....
    }
}

调用的时候:

(Application.OpenForms["DemoForm"] as Forms.DemoForm).clientFindMain1.GoSelect("http://www.myluoluo.com");

[warning]需要注意次窗口是否已经被实例化[/warning]

7 Replies to “C#调用用户控件中的公共方法”

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据