【问题标题】:How to set location or similar property in System.Windows.Controls.TextBox C#如何在 System.Windows.Controls.TextBox C# 中设置位置或类似属性
【发布时间】:2014-08-26 12:09:54
【问题描述】:

我正在制作System.Windows.Controls.TextBox。我需要它是System.Windows.Controls.TextBox,而不是System.Windows.Forms.TextBox,因为一个方法需要它来进行拼写检查。我已经弄清楚或查找了该控件的大多数其他属性,但在 Google、Stack Overflow 或 Microsoft 上都找不到。

这是我正在使用的代码:

this.tbSearch.Name = "tbSearch";
//this.tbSearch.LOCATION    //this needs to be replaced
this.tbSearch.Width = 313;
this.tbSearch.Height = 20;
this.tbSearch.TabIndex = 2;
this.tbSearch.TextChanged += new System.Windows.Controls.TextChangedEventHandler(this.tbSearch_TextChanged);

感谢任何帮助!

编辑:

我正在使用 WinForms。

【问题讨论】:

  • 有一些插件可以为Forms.Textbox 提供拼写检查属性。这可能更容易。
  • System.Windows.Controls 类是 WPF 控件。您不能将 WPF 控件直接放在 WinForm 窗体中。您需要使用 ElementHost 控件“托管”它。
  • @krillgar 你有链接吗?谢谢!
  • @cullub 这是来自 MSDN 的 walkthrough
  • NHunspell 是我过去使用的那个。使用起来相当容易。

标签: c# .net winforms textbox


【解决方案1】:

试试Margin 属性,System.Windows.Thickness 对象:

this.tbSearch.Margin = new Thickness(0, 0, 50, 50);

更新

似乎完全可以在 WPF 中工作。

CS:

private void Button_Click(object sender, RoutedEventArgs e)
{
    this.tbSearch.Margin = new Thickness(this.tbSearch.Margin.Left - 10,
    this.tbSearch.Margin.Top - 10,
    this.tbSearch.Margin.Right,
    this.tbSearch.Margin.Bottom);
}

XAML:

<Button Content="Button" HorizontalAlignment="Left" Margin="55,37,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<TextBox Name="tbSearch" HorizontalAlignment="Left" Height="23" Margin="198,159,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>

带有 ElementHost 的 WinForms

【讨论】:

  • 设置了一种边界;我想要一些东西来设置控件相对于表单的位置,或者它是父对象。无论如何,谢谢!
  • 您要添加到 System.Windows.Form 的表单是?我假设 WPF System.Windows.Window 或 Control (它完全适用,请参阅更新)
【解决方案2】:

您要将此添加到表单中吗?如果是这样,表单上的所有控件都支持设置 .Top 和 .Left 以相对于前者的左上角定位元素。

【讨论】:

  • 我将其添加到表单中,但是,这是一个 System.Windows.Control 对象,而不是 System.Windows.Forms。感谢您的帮助!
  • 我不确定我是否理解。您要向 WinForm 添加 WPF 对象(System.Windows.Control.Textbox 在 PresentationFramework 程序集中)?您能说明将它添加到表单控件集合的位置吗?
猜你喜欢
  • 1970-01-01
  • 2018-12-11
  • 1970-01-01
  • 1970-01-01
  • 2019-09-20
  • 2014-12-26
  • 1970-01-01
  • 2020-07-27
  • 1970-01-01
相关资源
最近更新 更多