2011-05-25 68 views
0

有什么方法可以在C#中添加System.Windows.Controls.TextBoxGroupBox控件?无法将System.Windows.Controls.TextBox添加到C#中的组框控件中

我尝试以下,但它并没有在组合框中显示出来:

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我需要它拼写检查。

任何帮助将不胜感激!

+0

你可以把WPF控件插入到的WinForms使用ElementHost的像他这样做 – wangburger 2011-05-25 21:29:25

+0

是groubBox4'System.WIndows.Controls.GroupBox'或'System.Windows.Forms.GroupBox'? [试图用C#拼写检查类]的 – 2011-05-25 21:39:01

+0

可能重复(http://stackoverflow.com/questions/4024798/trying-to-use-the-c-spellcheck-class) – 2011-05-25 21:51:22

回答

0

这段代码实际上是否被调用? groupbox 4已添加到表单了吗?

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

如果你移动你的组框位置到不同的地方它的工作?我的位置在271,268点,但文本框位于主窗体上的3,33,而不在组框中。 – user770344 2011-05-26 00:45:47

+0

@ user770344:在你的代码的底部,你有这两行:this.Controls.Add(sumtext); this.Controls.Add(loctext); 这可能会导致您的问题。删除它们? – David 2011-05-26 15:06:26

0

你不应该将你的ElementHost控件到窗体中,你的组框,这似乎是令人困惑的.NET。正是保持你原有的代码,只是注释掉这两条线使得它的工作:

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

而且......我不认为这是伤害任何东西,但你并不需要这样做两次:

//groupBox4.Controls.Add(sumtext); 
+1

看起来像评论这两行工作!我想我现在看到发生了什么,我将控件添加到groupbox4,然后将它重新读回到“this”,将它从groupbox中移除。谢谢!!! – user770344 2011-05-26 04:08:15

相关问题