C# Sayısal Değer Kontrolü
-
C# biraz kurcalamaya başladım. Haliyle eski alışkanlıklardan (visual basic, asp) sentaksından vazgeçemedim :). Orada kullanılan IsNumeric() fonksiyonunu kullanmak istedim.
Girilen bir değerin sayısal olup olmadığını kontrol eden uygulama
Forma birer tane
buton, label, textbox
ekleyin.
1- Projenize Microsoft.VisualBasic dll 'ini eklemeniz gerekir.
( projeniz >References > sağ kilik >Add Refernce... > gelen referanslar listesinden Microsoft.VisualBasic > OK diyoruz. )
2- Daha sonra ise using Microsoft.VisualBasic; .diyerek bu dll kullanmak istediğinizi belirtmelisiniz.Kodlar
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string deger = textBox1.Text.ToString();
if (deger == "")
{
label1.Text = "Boş değer girmeyiniz.";
}
else
{
bool sonuc = Information.IsNumeric(deger);
if (sonuc == true)
{
label1.Text = "Sayısal sonuç.";
}
else
{
label1.Text = "Sayısal olmayan sonuç.";
}
}
}
}
}
-
yadaa..
string str = textBox1.Text.ToString();
int sayi;
if( int.TryParse(str, out sayi) )
label1.Text = "Sayısal sonuç.";
else
label1.Text = "Sayısal olmayan sonuç.";
