2016-12-21 17 views
0

私自身のTextBoxクラスを開発中です。テキストボックス、テキスト領域を設定

SmallOffice.Windows.Forms 
{ 
    public class TextBox:System.Windows.Forms.TextBox 
    { 
     System.Windows.Forms.Button button; 

     public TextBox() 
     { 
       this.button = new System.Windows.Forms.Button(); 
       this.Controls.Add(this.button); 
     } 
    } 
} 

問題は次のとおりです。テキストの論理領域をどのように割り当てるか。たとえば、TextBoxのサイズはSize(200, 20)に設定され、テキストの論理領域はRectangle(0, 0, 130, 20)に設定されます。

このようにすると、TextBoxに2つの領域があります。テキストの左側とボタンの右側。 これは、テキストがボタンと重ならないようにするために必要です。

私に手がかりを与える、プロンプトまたは何か助けてください。

+4

テキストボックスにボタンが含まれていないを取得し、テキストボックスの設定テキストがユーザーコントロールにプロパティを定義するには

。むしろ、ユーザーコントロール内にボタンとテキストボックスの両方が含まれています。 –

+1

また、TextBoxのサブクラス化は、レガシーでないコントロールのサブクラス化と同様に機能しません。 – TaW

答えて

2

ユーザーコントロールを作成します。 2列1行の表レイアウトパネルを親コンテナにドッキングし、背景色を白に設定します。ユーザーコントロールの境界線のスタイルを3Dに設定します。 最初の列のテキストボックスと2番目の列のボタンを取ります。

ユーザーコントロールデザイナーのファイルのコードは次のようにする必要があります

namespace ProgressBar 
{ 
partial class Tbox 
{ 
    /// <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.panel1 = new System.Windows.Forms.Panel(); 
     this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); 
     this.panel2 = new System.Windows.Forms.Panel(); 
     this.textBox1 = new System.Windows.Forms.TextBox(); 
     this.button1 = new System.Windows.Forms.Button(); 
     this.panel1.SuspendLayout(); 
     this.tableLayoutPanel1.SuspendLayout(); 
     this.panel2.SuspendLayout(); 
     this.SuspendLayout(); 
     // 
     // panel1 
     // 
     this.panel1.Controls.Add(this.tableLayoutPanel1); 
     this.panel1.Dock = System.Windows.Forms.DockStyle.Fill; 
     this.panel1.Location = new System.Drawing.Point(0, 0); 
     this.panel1.Name = "panel1"; 
     this.panel1.Size = new System.Drawing.Size(275, 42); 
     this.panel1.TabIndex = 0; 
     // 
     // tableLayoutPanel1 
     // 
     this.tableLayoutPanel1.BackColor = System.Drawing.SystemColors.ButtonHighlight; 
     this.tableLayoutPanel1.ColumnCount = 2; 
     this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F)); 
     this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 50F)); 
     this.tableLayoutPanel1.Controls.Add(this.panel2, 0, 0); 
     this.tableLayoutPanel1.Controls.Add(this.button1, 1, 0); 
     this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill; 
     this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0); 
     this.tableLayoutPanel1.Name = "tableLayoutPanel1"; 
     this.tableLayoutPanel1.RowCount = 1; 
     this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F)); 
     this.tableLayoutPanel1.Size = new System.Drawing.Size(275, 42); 
     this.tableLayoutPanel1.TabIndex = 0; 
     // 
     // panel2 
     // 
     this.panel2.Controls.Add(this.textBox1); 
     this.panel2.Dock = System.Windows.Forms.DockStyle.Fill; 
     this.panel2.Location = new System.Drawing.Point(3, 3); 
     this.panel2.Name = "panel2"; 
     this.panel2.Size = new System.Drawing.Size(219, 36); 
     this.panel2.TabIndex = 0; 
     // 
     // textBox1 
     // 
     this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill; 
     this.textBox1.Location = new System.Drawing.Point(0, 0); 
     this.textBox1.Multiline = true; 
     this.textBox1.Name = "textBox1"; 
     this.textBox1.Size = new System.Drawing.Size(219, 36); 
     this.textBox1.TabIndex = 0; 
     // 
     // button1 
     // 
     this.button1.Dock = System.Windows.Forms.DockStyle.Fill; 
     this.button1.Location = new System.Drawing.Point(228, 3); 
     this.button1.Name = "button1"; 
     this.button1.Size = new System.Drawing.Size(44, 36); 
     this.button1.TabIndex = 1; 
     this.button1.Text = "X"; 
     this.button1.UseVisualStyleBackColor = true; 
     // 
     // Tbox 
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); 
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; 
     this.Controls.Add(this.panel1); 
     this.Name = "Tbox"; 
     this.Size = new System.Drawing.Size(275, 42); 
     this.panel1.ResumeLayout(false); 
     this.tableLayoutPanel1.ResumeLayout(false); 
     this.panel2.ResumeLayout(false); 
     this.panel2.PerformLayout(); 
     this.ResumeLayout(false); 

    } 

    #endregion 

    private System.Windows.Forms.Panel panel1; 
    private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1; 
    private System.Windows.Forms.Panel panel2; 
    private System.Windows.Forms.TextBox textBox1; 
    private System.Windows.Forms.Button button1; 

} 
} 

あなたは欲望の外観にもパネルを使用することができます。適切な位置とサイズのために各コントロールのドックプロパティを設定します。

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

をし、フォームへのアクセスで、それが好き:

tbox1.textBoxText = "Hello"; 
+0

私はすでにその方法を試しており、深刻な設計時の問題がありました。私の状況では私は自分のやり方でそれをしなければならない。 – Bahrom

+0

あなたはどのような問題に直面していますか? –

+0

データバインディング。最初は、MyUserControlとTextBox、適切なレイアウトのButtonを作成しました。私は、MyUserControlをデザイナーにTextBoxとして公開しなければならないと願っていました。 – Bahrom

関連する問題