如何在System.Windows.Controls.TextBox C#中设置位置或类似属性

时间:2014-08-26 12:09:54

标签: c# .net winforms textbox

我正在制作System.Windows.Controls.TextBox。我需要它是System.Windows.Controls.TextBox,而不是System.Windows.Forms.TextBox,因为SpellCheck需要一个方法。我已经想出了,或者查找了这个控件的大多数其他属性,但我无法在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。

2 个答案:

答案 0 :(得分:2)

尝试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

答案 1 :(得分:0)

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