0

Is there any way to add a System.Windows.Controls.TextBox to GroupBox controls in C#?

I tried the following but it doesn't show up in the groupbox:

 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);
    }

I need to use System.Windows.Controls.TextBox rather than Form.TextBox as I need it for spell check.

Any help would be greatly appreciated!

user770344
  • 473
  • 2
  • 8
  • 16
  • You can put WPF controls into Winforms by using the ElementHost like he's doing – wangburger May 25 '11 at 21:29
  • is groubBox4 `System.WIndows.Controls.GroupBox` or `System.Windows.Forms.GroupBox` ? – Bala R May 25 '11 at 21:39
  • possible duplicate of [Trying to use the C# SpellCheck class](http://stackoverflow.com/questions/4024798/trying-to-use-the-c-spellcheck-class) – Hans Passant May 25 '11 at 21:51
  • Are you sure its not showing up? both ElementHosts are disabled and your never setting textbox6 or textbox7 with Text. You are giving them a name but nothing for textbox6.Text – JMcCarty May 25 '11 at 21:52

3 Answers3

1

I changed the Enabled property of the sumtext, and removed the other box to shorten it: This code works for me:

    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);
    }
David
  • 1,743
  • 1
  • 18
  • 25
  • If you move your group box location to a different point does it work? mine is located at point 271, 268 but the textboxes are at 3, 33 on the main form and not in the groupbox. – user770344 May 26 '11 at 00:45
  • @user770344: at the bottom of your code you have these 2 lines: this.Controls.Add(sumtext); this.Controls.Add(loctext); This is probably causing your issues. Remove them? – David May 26 '11 at 15:06
0

Is this code actually getting called? Has groupbox 4 been added to the form yet?

wangburger
  • 1,073
  • 1
  • 18
  • 28
0

You should not be adding your ElementHost controls to your Form AND your GroupBox, it appears to be confusing .NET. Keeping your original code exactly as-is but commenting out these two lines makes it work:

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

Also... I don't think it's hurting anything, but you don't need to do this twice:

    //groupBox4.Controls.Add(sumtext);
Michael Edenfield
  • 28,070
  • 4
  • 86
  • 117
  • 1
    Looks like commenting out those two lines worked! I think I see what happened now, I added the control to groupbox4 then readded it back to the 'this' removing it away from groupbox. Thank you!!! – user770344 May 26 '11 at 04:08