用過winform應該都知道TextBox就是供使用者輸入的文字區塊
<TextBox /> - 單行
<TextBox AcceptsReturn="True"
TextWrapping="Wrap" />
AcceptsReturn讓TextBox變多行,
TextWrapping="Wrap"當TextBox的字過長時,會自動把原本輸入的字往上移,讓使用者輸入新的字
其他屬性
SpellCheck.IsEnabled="True"
Language="en-US" - 開啟自動拼字檢查功能
IsReadOnly="True" - TextBox只能唯讀
綁定SelectionChanged事件
<DockPanel Margin="10">
<TextBox
SelectionChanged="TextBox_SelectionChanged"
DockPanel.Dock="Top" />
<TextBox
Name="txtStatus" AcceptsReturn="True"
TextWrapping="Wrap" IsReadOnly="True" />
</DockPanel>
private void
TextBox_SelectionChanged(object sender, RoutedEventArgs e)
{
TextBox textBox =
sender as TextBox;
txtStatus.Text =
"Selection starts at character #" + textBox.SelectionStart +
Environment.NewLine;
txtStatus.Text +=
"Selection is " + textBox.SelectionLength + " character(s)
long" + Environment.NewLine;
txtStatus.Text +=
"Selected text: '" + textBox.SelectedText + "'";
}
// textBox.SelectionStart - 判斷鼠標在哪個字元,從0開始
// Environment.NewLine - 斷行
// textBox.SelectionLength - User選了幾個字元
// textBox.SelectedText - User選了什麼字
PasswordBox - 供使用者輸入密碼的區塊
TextBox的內容改為隱藏
<PasswordBox />
可以更改成想要的字
<PasswordBox PasswordChar="X"
/>
設定最大長度
<PasswordBox MaxLength="6"
/>
用C#獲得PasswordBox的密碼
IntPtr p =
System.Runtime.InteropServices.Marshal.SecureStringToBSTR(txtPassword.SecurePassword);
string password =
System.Runtime.InteropServices.Marshal.PtrToStringBSTR(p);