Jump to content

C# Textbox'a Maksimum 4 Karakter Girmek


adige81
 Share

Recommended Posts

Pairs senin dedigin tum karakterler icin. Sadece rakam kabul etmesini istiyorsan her tusa basildiginda kontrol etmen gerekli. EGer bununla ugrasmak istemiyorsan numericupdown toolu var onu kullanabilirsin. 

 

 

NumbericUpDown.Minimum = -10;

NumericUpDown.Maximum = 10;

 

Bununla maximum ve minimumunu kontrol edebilirsin. Yok ille de ben textbox kullanacam ve textboxta rakam harici kabul ettirmeyecem ve maksimum 4 hane olacak dersen. Page_Load una textbox.maxlength=4; dersin 

textbox keypress eventine textBox1_KeyPress eklersin asagidaki kodu yazarsin. Her klavyeye bastiginda onun rakam olup olmadigin kontrol eder. 

private void textBox1_KeyPress(object sender, KeyPressEventArgs e){
if (!char.IsControl(e.KeyChar) 
&& !char.IsDigit(e.KeyChar) 
&& e.KeyChar != '.')
{
e.Handled = true;
}

// only allow one decimal point
if (e.KeyChar == '.' 
&& (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
}

Bana soracak olursan numericupdown kullanmak daha akillica sonucta sadece rakam girmek istiyorsun ve kodlamada basit her zaman en iyisidir. 

Link to comment
Share on other sites

  • Editor

Pairs senin dedigin tum karakterler icin. Sadece rakam kabul etmesini istiyorsan her tusa basildiginda kontrol etmen gerekli. EGer bununla ugrasmak istemiyorsan numericupdown toolu var onu kullanabilirsin. 

 

 

NumbericUpDown.Minimum = -10;

NumericUpDown.Maximum = 10;

 

Bununla maximum ve minimumunu kontrol edebilirsin. Yok ille de ben textbox kullanacam ve textboxta rakam harici kabul ettirmeyecem ve maksimum 4 hane olacak dersen. Page_Load una textbox.maxlength=4; dersin 

textbox keypress eventine textBox1_KeyPress eklersin asagidaki kodu yazarsin. Her klavyeye bastiginda onun rakam olup olmadigin kontrol eder. 

private void textBox1_KeyPress(object sender, KeyPressEventArgs e){
if (!char.IsControl(e.KeyChar) 
&& !char.IsDigit(e.KeyChar) 
&& e.KeyChar != '.')
{
e.Handled = true;
}

// only allow one decimal point
if (e.KeyChar == '.' 
&& (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
}

Bana soracak olursan numericupdown kullanmak daha akillica sonucta sadece rakam girmek istiyorsun ve kodlamada basit her zaman en iyisidir. 

Rakam kısmını okumamıştım :D

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...