无法将System.Windows.Controls.TextBox添加到C#中的组框控件

时间:2011-05-25 21:20:08

标签: c#

有没有办法在C#中添加System.Windows.Controls.TextBoxGroupBox控件?

我尝试了以下操作,但它没有显示在组框中:

 public System.Windows.Controls.TextBox textBox6 = new System.Windows.Controls.TextBox();
 public System.Windows.Controls.TextBox textBox7 = new System.Windows.Controls.TextBox();
 public ElementHost sumtext = new ElementHost();
 public ElementHost loctext = new ElementHost();

 private void Form1_Load(object sender, EventArgs e)
    {
        textBox6.Name = "Summary";
        textBox7.Name = "Location";

        textBox6.FontFamily = new System.Windows.Media.FontFamily("Microsoft Sans Serif");
        textBox6.FontSize = 12;
        textBox6.SpellCheck.IsEnabled = true;

        textBox7.FontFamily = new System.Windows.Media.FontFamily("Microsoft Sans Serif");
        textBox7.FontSize = 12;
        textBox7.SpellCheck.IsEnabled = true;

        groupBox4.Controls.Add(sumtext);
        sumtext.Dock = DockStyle.None;
        sumtext.Width = 246;
        sumtext.Height = 35;
        sumtext.Child = textBox6;
        sumtext.Location = new Point(3, 33);
        sumtext.Visible = true;
        sumtext.Enabled = false;
        groupBox4.Controls.Add(sumtext);

        groupBox4.Controls.Add(loctext);
        loctext.Dock = DockStyle.None;
        loctext.Width = 246;
        loctext.Height = 35;
        loctext.Child = textBox7;
        loctext.Location = new Point(3, 90);
        loctext.Visible = true;
        loctext.Enabled = false;

        this.Controls.Add(sumtext);
        this.Controls.Add(loctext);
    }

我需要使用System.Windows.Controls.TextBox而不是Form.TextBox,因为我需要进行拼写检查。

任何帮助将不胜感激!

3 个答案:

答案 0 :(得分:1)

我更改了sumtext的Enabled属性,并删除了另一个框来缩短它: 这段代码适合我:

    public Form1()
    {
        this.Load += new System.EventHandler(this.Form1_Load);
    }

    public System.Windows.Controls.TextBox textBox6 = new System.Windows.Controls.TextBox();
    public ElementHost sumtext = new ElementHost();
    private System.Windows.Forms.GroupBox groupBox4;

    private void Form1_Load(object sender, EventArgs e)
    {
        this.groupBox4 = new System.Windows.Forms.GroupBox();
        this.SuspendLayout();
        // 
        // groupBox4
        // 
        this.groupBox4.Location = new System.Drawing.Point(57, 63);
        this.groupBox4.Name = "groupBox4";
        this.groupBox4.Size = new System.Drawing.Size(591, 238);
        this.groupBox4.TabIndex = 0;
        this.groupBox4.TabStop = false;
        this.groupBox4.Text = "groupBox1";
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(706, 478);
        this.Controls.Add(this.groupBox4);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);
        textBox6.Name = "Summary";

        textBox6.FontFamily = new System.Windows.Media.FontFamily("Microsoft Sans Serif");
        textBox6.FontSize = 12;
        textBox6.SpellCheck.IsEnabled = true;

        groupBox4.Controls.Add(sumtext);
        sumtext.Dock = DockStyle.None;
        sumtext.Width = 246;
        sumtext.Height = 35;
        sumtext.Child = textBox6;
        sumtext.Location = new Point(3, 33);
        sumtext.Visible = true;
        sumtext.Enabled = true;
        groupBox4.Controls.Add(sumtext);
    }

答案 1 :(得分:0)

这段代码真的被调用了吗?是否已将groupbox 4添加到表单中?

答案 2 :(得分:0)

您不应该将ElementHost控件添加到表单和GroupBox中,这似乎让.NET感到困惑。保持原始代码完全一样,但注释掉这两行可以使它工作:

    //this.Controls.Add(sumtext);
    //this.Controls.Add(loctext);

另外......我认为这不会伤害任何事情,但你不需要两次这样做:

    //groupBox4.Controls.Add(sumtext);