PDA

View Full Version : ايجاد ماشين حساب در محيط سي شارپ



aensiyeh
شنبه 03 مرداد 1394, 15:15 عصر
سلام دوستان من ماشين حسابي طراحي کردم و کد نويسي آن هم انجام دادم .
ماشين حساب من دوتا فرم داره
فرم اول که دکمه ها 0تا 9 و عملگرهاي =+-*/ c , EC , غيره هم دارم.
در فرم دوم يه label دارم .
وقتي بر روي دکمه مساوي در فرم اول مي زنم مي خوام خروجي برنامه تو فرم دوم روي label نمايش بده

خواهش مي کنم اگر کسي مي دونه بهم جواب بده امروز وقت دارم .


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace calculator100
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
float num1, ans;
int count;
private void btnequal_Click(object sender, EventArgs e)
{
compute(count);
}
public void compute(int count)
{
switch (count)
{
case 1:
ans = num1 - float.Parse(textBox1.Text);
textBox1.Text = ans.ToString();
break;
case 2:
ans = num1 + float.Parse(textBox1.Text);
textBox1.Text = ans.ToString();
break;
case 3:
ans = num1 * float.Parse(textBox1.Text);
textBox1.Text = ans.ToString();
break;
case 4:
ans = num1 / float.Parse(textBox1.Text);
textBox1.Text = ans.ToString();
break;
default:
break;
}
Form3 f = new Form3();
f.Show();
}


private void btnone_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 1;
}
private void bttntwo_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 2;
}


private void btnthree_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 3;
}
private void btnplus_Click(object sender, EventArgs e)
{
num1 = float.Parse(textBox1.Text);
textBox1.Clear();
textBox1.Focus();
count = 2;
}
private void btnfour_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 4;
}


private void btnfive_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 5;
}
private void btnsix_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 6;
}
private void btnminus_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
num1 = float.Parse(textBox1.Text);
textBox1.Clear();
textBox1.Focus();
count = 1;
}
}
private void btnseven_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 7;
}
private void btneight_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 8;
}
private void btnnine_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 9;
}
private void btnmultiply_Click(object sender, EventArgs e)
{
num1 = float.Parse(textBox1.Text);
textBox1.Clear();
textBox1.Focus();
count = 3;
}
private void btnzero_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 0;
}
private void btnC_Click(object sender, EventArgs e)
{
textBox1.Clear();
count = 0;
}
private void btnperiod_Click(object sender, EventArgs e)
{
int c = textBox1.TextLength;
int flag = 0;
string text = textBox1.Text;
for (int i = 0; i < c; i++)
{
if (text[i].ToString() == ".")
{
flag = 1; break;
}
else
{
flag = 0;
}
}
if (flag == 0)
{
textBox1.Text = textBox1.Text + ".";
}
}
private void btndivide_Click(object sender, EventArgs e)
{
num1 = float.Parse(textBox1.Text);
textBox1.Clear();
textBox1.Focus();
count = 4;
}
private void btnCE_Click(object sender, EventArgs e)
{
if (num1 == 0 && textBox1.TextLength > 0)
{
num1 = 0; textBox1.Clear();
}
else if (num1 > 0 && textBox1.TextLength > 0)
{
textBox1.Clear();
}
}
private void btnback_Click(object sender, EventArgs e)
{
int lenght = textBox1.TextLength - 1;
string text = textBox1.Text;
textBox1.Clear();
for (int i = 0; i < lenght; i++)
textBox1.Text = textBox1.Text + text[i];
}
}
}

RmeXXXXXXXXX
شنبه 03 مرداد 1394, 15:38 عصر
خوب! شما مقداری رو که باید در فرم سوم نمایش داده بشه رو بهش می فرستین؟ خیر اینکار رو نکردین.
توی فرم سوم یه پروپرتی تعریف کنید بدین شکل:

public partial class Form3:Form
{
public string Result{
set{
lable1.Text= value;
}
}
// و سایر کدها
}

سپس خطوط پایانی دکمه مساوی در فرم دوم را بدین گونه تغییر بدید:

Form3 frm=new Form3();
frm.Result= TextBox1.Text;
frm.ShowDialog();

ناامید
شنبه 03 مرداد 1394, 16:03 عصر
سلام.
تو این مثال یک مقدار از فرم یک به کنترل لیبل فرم دو فرستاده شده، شما جوابی رو که بدست میارید به لیبل فرم دو بفرستید.

Form2 form2 = new Form2();
form2.Controls["label1"].Text = Math.Pow(2, 2).ToString();
form2.ShowDialog();

aensiyeh
شنبه 03 مرداد 1394, 17:10 عصر
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace calculator100
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}


private void label1_Click(object sender, EventArgs e)
{

public string Result{
set{
lable1.Text= value;
}

}
}
}


خب ارور ميده
چکار کنم

RmeXXXXXXXXX
شنبه 03 مرداد 1394, 17:16 عصر
متن خطا لطفاً