首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在另一个表单上显示GroupBox及其内容?

如何在另一个表单上显示GroupBox及其内容?
EN

Stack Overflow用户
提问于 2012-08-11 06:39:31
回答 5查看 4.5K关注 0票数 0

我有两个表格。在第一个表单中,我有一个虚拟的数字键盘(我有一个GroupBox,里面有数字按钮,这是我的虚拟数字键盘)。使用这个虚拟的数字键盘,我将数字输入到TextBox中。在第二个表单中,我有另一个TextBox,我可以在其中输入数字。

我想在第二个表单上使用我的虚拟数字键盘。我该怎么做呢?

如果有人一步一步地向我解释我应该做什么,我会很高兴。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2012-08-11 07:59:40

1)创建一个WinForms项目,我叫它"ReusingUserControlsSample“

2)创建一个新的UserControl,将其命名为MyUserControlWithButtons或其他您喜欢的名称

3)出于习惯,在UserControl属性上设置AutoSize=true和AutoSizeMode=GrowAndShrink。稍后,您可能会了解它们的用途

4)在控件上放置一些按钮,将它们命名为"btnLetterA","btnLetterB","btnLetterC“

5)双击每个按钮,这样就会生成点击处理程序

6)在用户控件的代码中,创建一个public TextBox TheOutput属性

7)在用户控件的代码中,在步骤(5)中生成的每个单击处理程序中,添加一行向TheOutput textbox的TextBox属性添加一些文本。记住检查TheOutput是否为空。

构建。

8)返回Form1

9)将键盘放在表单上,将其命名为“myMyUserControlWithButtons

10)在表单上放置一个TextBox,将其命名为"mytextbox“

11)转到Form1的代码

12)在te构造函数中,在"InitializeComponent“下面,将mytextbox赋值给mykeyboard的TheOutput

这就是了。现在您可以构建并运行它了,一切都应该正常了。请不要说“键盘”的全部代码都在用户控件中。表单仅将其设置为与文本框一起使用。在第二种情况下,你可以用同样的方法:放置键盘,放置文本框,将键盘设置为写入该文本框,它将同样起作用。

代码:

MyUserControlWithButtons.cs

代码语言:javascript
复制
using System;
using System.Windows.Forms;

namespace ReusingUserControlsSample
{
    public partial class MyUserControlWithButtons : UserControl
    {
        public MyUserControlWithButtons()
        {
            InitializeComponent();
        }

        public TextBox TheOutput { get; set; }

        private void btnLetterA_Click(object sender, EventArgs e)
        {
            TheOutput.Text += "A";
        }

        private void btnLetterB_Click(object sender, EventArgs e)
        {
            TheOutput.Text += "B";
        }

        private void btnLetterC_Click(object sender, EventArgs e)
        {
            TheOutput.Text += "C";
        }
    }
}

MyUserControlWithButtons.cs

代码语言:javascript
复制
namespace ReusingUserControlsSample
{
    partial class MyUserControlWithButtons
    {
        /// <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 Component 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.btnLetterA = new System.Windows.Forms.Button();
            this.btnLetterB = new System.Windows.Forms.Button();
            this.btnLetterC = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // btnLetterA
            // 
            this.btnLetterA.Location = new System.Drawing.Point(3, 3);
            this.btnLetterA.Name = "btnLetterA";
            this.btnLetterA.Size = new System.Drawing.Size(66, 21);
            this.btnLetterA.TabIndex = 0;
            this.btnLetterA.Text = "The \"A\"";
            this.btnLetterA.UseVisualStyleBackColor = true;
            this.btnLetterA.Click += new System.EventHandler(this.btnLetterA_Click);
            // 
            // btnLetterB
            // 
            this.btnLetterB.Location = new System.Drawing.Point(66, 30);
            this.btnLetterB.Name = "btnLetterB";
            this.btnLetterB.Size = new System.Drawing.Size(66, 21);
            this.btnLetterB.TabIndex = 0;
            this.btnLetterB.Text = "The \"B\"";
            this.btnLetterB.UseVisualStyleBackColor = true;
            this.btnLetterB.Click += new System.EventHandler(this.btnLetterB_Click);
            // 
            // btnLetterC
            // 
            this.btnLetterC.Location = new System.Drawing.Point(3, 57);
            this.btnLetterC.Name = "btnLetterC";
            this.btnLetterC.Size = new System.Drawing.Size(66, 21);
            this.btnLetterC.TabIndex = 0;
            this.btnLetterC.Text = "The \"C\"";
            this.btnLetterC.UseVisualStyleBackColor = true;
            this.btnLetterC.Click += new System.EventHandler(this.btnLetterC_Click);
            // 
            // MyUserControlWithButtons
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.AutoSize = true;
            this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.Controls.Add(this.btnLetterC);
            this.Controls.Add(this.btnLetterB);
            this.Controls.Add(this.btnLetterA);
            this.Name = "MyUserControlWithButtons";
            this.Size = new System.Drawing.Size(135, 81);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button btnLetterA;
        private System.Windows.Forms.Button btnLetterB;
        private System.Windows.Forms.Button btnLetterC;
    }
}

Form1.cs

代码语言:javascript
复制
using System.Windows.Forms;

namespace ReusingUserControlsSample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            mykeyboard.TheOutput = mytextbox;
        }
    }
}

Form1.Designer.cs

代码语言:javascript
复制
namespace ReusingUserControlsSample
{
    partial class Form1
    {
        /// <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.mytextbox = new System.Windows.Forms.TextBox();
            this.mykeyboard = new ReusingUserControlsSample.MyUserControlWithButtons();
            this.SuspendLayout();
            // 
            // mytextbox
            // 
            this.mytextbox.Location = new System.Drawing.Point(84, 38);
            this.mytextbox.Name = "mytextbox";
            this.mytextbox.Size = new System.Drawing.Size(100, 20);
            this.mytextbox.TabIndex = 0;
            // 
            // mykeyboard
            // 
            this.mykeyboard.AutoSize = true;
            this.mykeyboard.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.mykeyboard.Location = new System.Drawing.Point(66, 122);
            this.mykeyboard.Name = "mykeyboard";
            this.mykeyboard.Size = new System.Drawing.Size(135, 81);
            this.mykeyboard.TabIndex = 1;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 264);
            this.Controls.Add(this.mykeyboard);
            this.Controls.Add(this.mytextbox);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox mytextbox;
        private MyUserControlWithButtons mykeyboard;
    }
}

Program.cs

代码语言:javascript
复制
using System;
using System.Windows.Forms;

namespace ReusingUserControlsSample
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
票数 2
EN

Stack Overflow用户

发布于 2012-08-11 06:43:47

创建一个UserControl,并将您的虚拟数字键盘分组框/按钮放在上面。然后在每个表单上替换现有的组框/按钮,添加新的用户控件。

票数 1
EN

Stack Overflow用户

发布于 2012-08-11 06:53:00

您有两个选择:

  • 或者创建一个名为"VirtualKeypad“的UserControl,并将GroupBox和键盘按钮移动到那里,然后在这两个窗体上使用(放置)新的"VirtualKeypad”控件。你的控件必须公开一些事件,或者有一个属性告诉它将文本放在哪个文本框中,等等。
  • ,或者,如果你只想有一个键盘,你的就有麻烦了。键盘必须是一个,但是你的键盘按钮如何知道把输入的文本放在哪里呢?你必须倾听焦点的变化,这样当你点击/触摸文本框(第一个或第二个),然后点击/触摸小键盘时,键盘将不得不检查谁在他之前获得了焦点(是第一个还是第二个文本框上的旧焦点),然后将数字/字母放在那里。这样做会有点棘手..此外,如果您是WinForms的初学者,您可能会在两个独立窗口之间的通信方面遇到一些问题。我建议你先试试UserControl。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11910144

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档