How to Make a Calculator in C#
شرح عمل برنامج آلة حاسبة بـ TextBox واحد فقط
بالسي شارب
نكتب في جميع buttons الموجود في التصميم لا زر = هذا الكود
textBox1.Text += "هنا نكتب الرقم كل زرار مثل 1و2و3 و+و-و/و*و.";
ثم نكتب في زر = هذا الكود
try
{
int t = 0;
if (textBox1.Text.Contains("+"))
{
t = textBox1.Text.IndexOf("+");
}
else if (textBox1.Text.Contains("-"))
{
t = textBox1.Text.IndexOf("-");
}
else if (textBox1.Text.Contains("*"))
{
t = textBox1.Text.IndexOf("*");
}
else if (textBox1.Text.Contains("/"))
{
t = textBox1.Text.IndexOf("/");
}
string x = textBox1.Text.Substring(t,1);
double x1 = Convert.ToDouble(textBox1.Text.Substring(0,t));
double x2 =Convert.ToDouble(textBox1.Text.Substring(t+1,textBox1.Text.Length-t-1));
if (x == "+")
{
listBox1.Items.Add(textBox1.Text + "=" + (x1 + x2));
textBox1.Text = (x1 + x2).ToString();
}
else if (x == "-")
{
listBox1.Items.Add(textBox1.Text + "=" + (x1 - x2));
textBox1.Text = (x1 - x2).ToString();
}
else if (x == "*")
{
listBox1.Items.Add(textBox1.Text + "=" + (x1 * x2));
textBox1.Text = (x1 * x2).ToString();
}
else if (x == "/")
{
listBox1.Items.Add(textBox1.Text + "=" + (x1 / x2));
textBox1.Text = (x1 /x2).ToString();
}
}
catch
{
textBox1.Text = "error";
}
يمكنك متابعة الفيديو به شرح الكود
لتحميل السورس كود
How to Make a Calculator in C# Windows Form Application ?
Simple scientific calculator programmed in C#.
Source code:
or
إرسال تعليق