TextBox etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
TextBox etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

26 Şubat 2018 Pazartesi

WPF TextBox

Giriş
Şu satırı dahil ederiz.
using System.Windows.Controls.TextBox;
XAML şöyledir.
<TextBox
    SelectionBrush="#FF54FF50"
    x:Name="textbox" 
    Margin="10,53,0,0" 
    FontSize="24" 
    HorizontalAlignment="Left"
    Width="341" 
    Height="285" 
    VerticalAlignment="Top" 
    TextChanged="Textbox_TextChanged"
    IsReadOnly="True" 
    CaretBrush="Black" 
    BorderBrush="Black" 
    Foreground="Black" 
    FontWeight="Bold" 
    Grid.ColumnSpan="2" 
    Padding="0,5,0,0" 
    HorizontalContentAlignment="Center"  
    VerticalContentAlignment="Center" 
    VerticalScrollBarVisibility="Auto" 
    TextWrapping="Wrap" />
GetLineText metodu
Örnek ver

IsReadOnly Alanı
Şöyle yaparız.
if (textBox.IsReadOnly){...}
LineCount Alanı
Örnek ver

SelectAll metodu
Şöyle yaparız.
textBox.SelectAll();
TextChanged metodu
Şöyle yaparız.
<TextBox
  x:Name="SearchText"
  Margin="0"
  TextWrapping="Wrap"
  Text="" Height="25"
  GotFocus="SearchText_GotFocus"
  TextInput="SearchText_TextInput"
  TextChanged="SearchText_TextChanged" />

Winforms TextBox Sınıfı

AutoCompleteCustomSource Alanı
AutoCompleteStringCollection Sınıfı yazısına taşıdım.

AutoCompleteMode Alanı
Şöyle yaparız.
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
DataBindings Alanı
BindingSource eklemek için şöyle yaparız.
textBox1.DataBindings.Add(new System.Windows.Forms.Binding
("Text", this.table1BindingSource, "ContactID", true));
ContactID alanını almak istersek şöyle yaparız.
var binding = textBox1.DataBindings[0];
var member =  binding.BindingMemberInfo.BindingMember;

MessageBox.Show(member);
ForeColor Alanı
Metni kırmızı renk yapmak için şöyle yaparız.
textBox1.ForeColor = Color.Red;
GotFocus ve LostFocus Event'leri
Focus gelince ve kaybedilince çağrılır.
private void Form1_Load()
{
  textBox1.Text = "Enter Email Account";
  textBox1.GotFocus += new EventHandler(textBox1_GotFocus);
  textBox1.LostFocus += new EventHandler(textBox1_Leave);
}
Olaylar şöyledir.
protected void textBox1_GotFocus(object sender, EventArgs e)
{...}
ve
protected  void textBox1_Leave(object sender, EventArgs e)
{...}
KeyPress Event'i
Şöyle yaparız. Eğer karakteri ekranda görmek istemiyorsak e.Handled = true döneriz
void OnKeyPress(object sender, KeyPressEventArgs e)
{
  if (!Char.IsDigit(e.KeyChar) && (e.KeyChar >= 0) &&
      (e.KeyChar != (char)Keys.Back) || (e.KeyChar == '.'))
    e.Handled = true;
}
Sadece 10'tane virgül karakterine izin vermek istersek şöyle yaparız.
void OnKeyPress(object sender, KeyPressEventArgs e)
{
  // Allow max 10 tags in the tag box
  string tags = tagBox.Text;
  int count = tags.Split(',').Length - 1;
  if (count > 9 && e.KeyChar == ',')
  {
    e.Handled = true;
  }
  else        
    e.Handled = false;
}
Renkler
Şöyle yapabiliriz.
textBox1.WaterMarkForeColor = Color.Green;
textBox1.WaterMarkActiveForeColor = Color.Green;
textBox1.ForeColor = Color.Green;
textBox1.WaterMark = "Browse To The File Location";
ScrollToCaret
Tam ne işe yarar bilmiyorum. Sanırım imleci en başa alıyor. Şöyle kullanılır.
textBox1.ScrollToCaret();
SelectionStart - Metin Seçimi
Select edilmiş metni değiştirmek istersek, Select() metodunu veya SelectionStart+SelectionLength alanlarını kullanırız. SelectionStart ile başlama karakterini ve kaç karakter olması gerektiğini belirtiriz.
textBox1.SelectionStart = 0;
textBox1.SelectionLength = 2;
Eğer hiç bir şey seçilmesin istiyorsak şöyle yaparız. İmleç sona gider.
textBox1.Select(textBox1.Text.Length, 0);
Eğer hiç bir şey seçilmesin istiyorsak ve imleci başa almak istersek şöyle yapabiliriz.
textBox1.SelectionStart = 0;
textBox1.SelectionLength = 0;
Ya da şöyle yaparız.
textBox1.Select(0, 0);
Text Alanı
Şöyle yaparız.
string text = textBox1.Text ;
TextChangedEvent
.Text property ile yeni text atanırsa bu event çağrılmaz.

Örnek
Çoğunlukla downcast yapmaya mecbur kalırız. Şöyle yaparız.
private void TextBox_TextChanged(object sender, EventArgs e)
{
  TextBox box = (TextBox)sender;
  box.BackColor = string.IsNullOrEmpty(box.Text) ? Color.Red : Color.White;
}
Örnek
Şöyle yaparız.
private void TextChanged(object sender, EventArgs e)
{
  textBox1.Text = "test";
}