私には2つのフォームがあります。最初のフォームには、仮想テンキーがあります (私はGroupBox
と 内部に数字ボタンがあり、これが私の仮想テンキーです)。この仮想テンキーを使用して、数字を に入力しますTextBox
。TextBox
2 番目のフォームには、数字を入力する別のフォームがあります。
この 2 番目のフォームで仮想テンキーを使用したいと考えています。どうやってやるの?
誰かが私が何をすべきかを段階的に説明してくれたら、私は喜んでいます。
私には2つのフォームがあります。最初のフォームには、仮想テンキーがあります (私はGroupBox
と 内部に数字ボタンがあり、これが私の仮想テンキーです)。この仮想テンキーを使用して、数字を に入力しますTextBox
。TextBox
2 番目のフォームには、数字を入力する別のフォームがあります。
この 2 番目のフォームで仮想テンキーを使用したいと考えています。どうやってやるの?
誰かが私が何をすべきかを段階的に説明してくれたら、私は喜んでいます。
1) WinForms プロジェクトを作成します。私はそれを「ReusingUserControlsSample」と呼び
ます 2) 新しい UserControl を作成し、MyUserControlWithButtons
好きな名前を付けます
3) 習慣から、UserControl プロパティで「AutoSize=true」と AutoSizeMode="GrowAndShrink" を設定します. 4) UserControlDesigner でコントロールにボタンを配置し、「 btnLetterA
」、「btnLetterB」、「btnLetterC」という名前を付けます
。
6) UserControl のコードで、public TextBox TheOutput
プロパティを作成します。7) UserControl のコードで、手順 (5) で生成した各クリック ハンドラーに、テキスト ボックス
にテキストを追加する行を追加します。の財産。NULL をチェックすることを忘れないでください。TheOutput
TextBox
TheOutput
建てる。
8) Form1 に戻ります
9)MyUserControlWithButtons
フォームに配置し、「mykeyboard」という名前を付け
ます 10) フォームに TextBox を配置し、「mytextbox」という名前を付け
ます 11) Form1 のコードに移動します
12) 「InitializeComponent」の下のコンストラクターで、mytextboxTheOutput
を mykeyboard の
で、これです。これで、ビルドして実行できます。すべて問題ありません。「キーボード」のコード全体がユーザーコントロールにあることに注意してください。フォームは、そのテキストボックスで動作するように設定しただけです。2 番目のフォームでも同じ方法でそれを行うことができます: キーボードを配置し、テキスト ボックスを配置し、そのテキスト ボックスに書き込むようにキーボードを設定すると、同じように動作します。
コード:
MyUserControlWithButtons.cs
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
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
using System.Windows.Forms;
namespace ReusingUserControlsSample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
mykeyboard.TheOutput = mytextbox;
}
}
}
Form1.Designer.cs
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
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つのオプションがあります。
「VirtualKeypad」という名前のUserControlを作成し、そこにGroupBoxボタンとキーパッドボタンを移動してから、両方のフォームで新しい「VirtualKeypad」コントロールを使用(配置)します。コントロールは、いくつかのイベントを公開するか、テキストを配置するテキストボックスなどを指定するプロパティを持っている必要があります。
または、キーパッドを1つだけにしたい場合は、問題が発生します。キーパッドは1つである必要がありますが、キーパッドボタンは、入力されたテキストを配置する場所をどのように認識しますか?フォーカスの変更を聞く必要があるため、テキストボックス(1番目または2番目)をクリック/タッチしてからキーパッドをクリック/タッチすると、キーパッドは前にフォーカスを持っていた人を確認する必要があります(以前のフォーカスは最初または2番目のテキストボックス)次に、数字/文字をそこに配置します。やるのは少し難しいでしょう。また、WinFormsの初心者の場合、2つの別々のウィンドウ間の通信で問題が発生する可能性があります。最初にUserControlを試してみることをお勧めします。
を作成し、UserControl
その上に仮想数字パッド グループボックス/ボタンを配置します。次に、既存のグループボックス/ボタンの代わりに、各フォームに新しいユーザーコントロールを配置します。
それをコピーするだけで(Dan-oが言ったように、すべてを新しいユーザーコントロールにコピーしてから、各フォームに配置することをお勧めします)、表示されているものを交換することをお勧めします。ただし、質問に直接答えるには、 Controls コレクションを変更するだけでフォーム間でコントロールを移動できます。
//FormA
FormB formBInstance;
void button_OnClick(object sender, EventArgs e){
Controls.Remove(myControl);
formBInstance.Controls.Add(myControl);
}
しかし、その場合、いつでもどのフォーム上にあるかを管理するという暗黙の難しさがあります。本当に必要でない限り、このようなフォーム間でコントロールを交差させないことをお勧めします (必要だと思う場合は、通常、より簡単な方法があります)。
ここでは誰も実際にできないことを段階的に尋ねたので、役立つはずのユーザーコントロールの説明をここに示します。その後、他のコントロールと同じようにツールボックスから選択し、各フォームに追加します。(そして、それらを「1つとして」動作させる必要がある場合は、表示を切り替える何かを設定してください)。
その通りでした!ボタンがそこに座っているだけで、おそらく新しいコントロールでクリックを処理するコードを記述していないため、機能しませんでした。グループボックス/ボタン全体を、それを処理するコード全体に沿ってコントロールに移動する必要があります。すべてのイベントハンドラー、すべてのフォーマッターなど、キーパッドを機能させるために最初のフォームで行ったすべてのもの-今すぐUserControlに移動する必要があります。
しかし、これで終わりではありません!キーパッド処理コードは、ボタンのクリックが発生すると、テキスト ボックスにテキストを追加しますよね? これで、ユーザー コントロールにテキスト ボックスがなくなります。
新しい美しいコントロールは、テキスト ボックスから抽象化する必要があります。理想的には、 テキストボックスがまったくあると想定する必要がありますが、それはスキップしましょう。新しいユーザー コントロールのコードで、のような新しいプロパティを配置しpublic TextBox MyOutputTextbox {get;set}
ます。ここで、すべてのテキストを取得し、それに応じて UserControl コードを修正するテキストボックスであるプロパティを操作しましょう。次に、コントロールをフォームに配置します。次に、フォームのコンストラクターの両方がテキストボックスをそのプロパティに割り当てていることを確認します。
public Form1() {
InitializeComponent();
myKeyPadControl.MyOutputTextbox = txtFirstBox;
}
そしてそれはうまくいくはずです。