Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Calcolatrice by Kirbiflint:
- Form4.cs
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Collections.Specialized;
- namespace Calcolatrice_by_Kirbiflint2
- {
- public partial class Form2 : Form
- {
- public Form2()
- {
- InitializeComponent();
- }
- Double Numero;
- /// int tipooperaz = 0;
- int Num;
- int Risult;
- int Dec;
- long Numerodaconv;
- ///string risultatodeci;
- //int Dec;
- ///string hexdec;
- private void button2_Click(object sender, EventArgs e)
- {
- textBox1.Text = "";
- textBox2.Text = "";
- textBox3.Text = "";
- textBox4.Text = "";
- textBox5.Text = "";
- textBox6.Text = "";
- textBox7.Text = "";
- }
- private void button1_Click(object sender, EventArgs e)
- {
- //string s;
- //string num;
- //byte ascii;
- if (String.IsNullOrEmpty(textBox1.Text))
- {
- MessageBox.Show("Enter your number which you want to convert and try again!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- //dataGridView1.Rows.Clear();
- }
- else
- //int Numerodaconv = int.Parse(textBox1.Text);
- Numerodaconv = long.Parse(textBox1.Text);
- char numconvertich = Convert.ToChar(Numerodaconv);
- /// s = Convert.ToString(Numerodaconv) + Encoding.ASCII.GetString(new byte[] { 65 });
- //Numerodaconv = Convert.ToInt32(textBox1.Text);
- /* textBox3.Text = Convert.ToString(Numerodaconv, 2);
- textBox4.Text = Convert.ToString(Numerodaconv, 16); //Numerodaconv.ToString();
- textBox2.Text = Numerodaconv.ToString(); //Convert.ToString(Numerodaconv, 16);
- textBox5.Text = Convert.ToString(Numerodaconv, 8);*/
- textBox3.Text = Convert.ToString(Numerodaconv, 2);
- textBox4.Text = Numerodaconv.ToString();
- textBox2.Text = Convert.ToString(Numerodaconv, 16);
- textBox5.Text = Convert.ToString(Numerodaconv, 8);
- textBox7.Text = Convert.ToString(numconvertich);
- /*num = Convert.ToString(textBox1.Text);
- for (int i = 0; i < num.Length; i = i + 1)
- {
- ascii = (byte)num[i];
- textBox7.Text = " {0}"}, ascii );
- }*/
- ///textBox7.Text = Convert.ToString(ascii);
- /*string ASCIIval = textBox7.Text;
- int value = Convert.ToInt32(ASCIIval);
- // Try to parse the input into an Int32
- if (Int32.TryParse(ASCIIval, out value))
- {
- // Parse was successful
- if (value >= 0 and value < 128) {
- //value entered was within the valid ASCII range
- //cast value to a char and print it
- char c = (char)value;
- Console.WriteLine(c);
- }
- }*/
- }
- private void button3_Click(object sender, EventArgs e)
- {
- bool contieneletter = false;
- string Numb = textBox1.Text.Trim();
- if (String.IsNullOrEmpty(textBox1.Text) == String.IsNullOrEmpty(textBox4.Text))
- {
- MessageBox.Show("Enter your decimal value and retry!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- else
- for (int i = 0; i < Numb.Length; i++)
- {
- if (!char.IsNumber(Numb[i])) //== String.IsNullOrEmpty(textBox4.Text))
- {
- contieneletter = true;
- }
- if (contieneletter) ///== String.IsNullOrEmpty(textBox4.Text))
- {
- MessageBox.Show("Please first delete the number or the letter above!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- }
- //if (String.IsNullOrEmpty(textBox1.Text)) //else
- //{
- if (String.IsNullOrEmpty(textBox1.Text))
- {
- Numero = Convert.ToDouble(textBox4.Text);
- textBox4.Text = "Enter your second value!";
- Risult = 1;
- ///MessageBox.Show("Ok now you can convert!");
- ///else
- /// contieneletter = false;
- //}
- }
- /*if (String.IsNullOrEmpty(textBox4.Text ) == String.IsNullOrEmpty(textBox1.Text))
- {
- MessageBox.Show("Enter your decimal value and retry!");
- }
- else */
- ////int Decimale = int.Parse(textBox4.Text);
- //MessageBox.Show("First delete the number above and try again!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- //MessageBox.Show("Enter your decimal value and retry!");
- //textBox4.Text
- //if (textBox4.Text == "")
- //{
- /// MessageBox.Show("Enter your decimal value and retry!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- //if (String.IsNullOrEmpty(textBox4.Text) == textBox1.Text.Contains)')) //textBox1.Text.Length > 0)
- /*else
- ///textBox3.Text = textBox3.Text; */
- /*if (Risult == 1)
- {
- Risult = Convert.ToInt32(Numero) + Convert.ToInt32(textBox3.Text, 2);
- textBox6.Text = Convert.ToString(Risult);
- }*/
- //Numero = Convert.ToDouble(textBox4.Text) + Convert.ToDouble(Convert.ToInt32(textBox3.Text, 2));
- //textBox4.Text = "0";
- ///int Binario = Convert.ToInt32(textBox3.Text);
- //textBox6.Text = Convert.ToString(Numero);
- ///string risultatodeci;
- /*if (textBox4.Text == "")
- {
- MessageBox.Show("Enter your decimal value and retry!");
- }
- else
- risultatodeci = textBox4.Text;
- textBox4.Text = "Enter your second decimal value!";
- Risult = 1;
- ////Num = Convert.ToInt32(textBox4.Text);
- //Risult = 1;
- ////textBox4.Text = "Enter the decimal value to calculate!";
- /*if (Risult == 1)
- {
- //textBox4.Text = "0";
- Num = Convert.ToInt32(textBox4.Text);
- textBox4.Text = "0";
- ///textBox4.Text = Convert.ToString(Numerodaconv, 16)
- //textBox6.Text = textBox4.Text + Convert.ToString(Num) ;
- }
- /*textBox4.Text = "0";
- Numero = Convert.ToDouble(textBox4.Text);
- textBox4.Text = "0";
- textBox6.Text = Convert.ToString(Numero) + textBox4.Text;*/
- }
- private void button10_Click(object sender, EventArgs e)
- {
- int secondovaloretxt;
- if (String.IsNullOrEmpty(textBox4.Text))
- {
- MessageBox.Show("Sorry to calculate the result, enter the decimal value!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- else
- secondovaloretxt = System.Convert.ToInt32(textBox4.Text);
- switch (Risult)
- {
- case 1:
- Risult = secondovaloretxt + Convert.ToInt32(Numero);
- textBox6.Text = Convert.ToString(Risult);
- break;
- case 2:
- Risult = secondovaloretxt - Convert.ToInt32(Numero);
- textBox6.Text = Convert.ToString(Risult);
- break;
- }
- }
- private void button6_Click(object sender, EventArgs e)
- {
- if (textBox2.Text == "")
- {
- MessageBox.Show("Enter your Hex decimal value and retry!");
- }
- else
- Num = Convert.ToInt32(textBox2.Text);
- textBox2.Text = "Enter your second value!";
- Risult = 1;
- }
- private void button11_Click(object sender, EventArgs e)
- {
- switch (Risult)
- {
- case 1:
- textBox6.Text = Convert.ToString(Convert.ToInt32(Num)) + Num;
- break;
- }
- }
- private void button4_Click(object sender, EventArgs e)
- {
- bool contieneletter = false;
- string Numb = textBox1.Text.Trim();
- if (String.IsNullOrEmpty(textBox1.Text) == String.IsNullOrEmpty(textBox4.Text))
- {
- MessageBox.Show("Enter your decimal value and retry!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- else
- for (int i = 0; i < Numb.Length; i++)
- {
- if (!char.IsNumber(Numb[i])) //== String.IsNullOrEmpty(textBox4.Text))
- {
- contieneletter = true;
- }
- if (contieneletter) ///== String.IsNullOrEmpty(textBox4.Text))
- {
- MessageBox.Show("Please first delete the number or the letter above!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- }
- //if (String.IsNullOrEmpty(textBox1.Text)) //else
- //{
- if (String.IsNullOrEmpty(textBox1.Text))
- {
- Numero = Convert.ToDouble(textBox4.Text);
- textBox4.Text = "Enter your second value!";
- Risult = 2;
- ///MessageBox.Show("Ok now you can convert!");
- ///else
- /// contieneletter = false;
- //}
- }
- }
- private void button9_Click(object sender, EventArgs e)
- {
- if (String.IsNullOrEmpty(textBox4.Text) == String.IsNullOrEmpty(textBox2.Text))
- {
- MessageBox.Show("Enter your Decimal Value and try again!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- //dataGridView1.Rows.Clear();
- }
- else if ((String.IsNullOrEmpty(textBox4.Text)))
- {
- ///int Valoreintero = Convert.ToInt32(textBox2.Text);
- // Convert integer 182 as a hex in a string variable
- //Valoreintero =
- //string Valore = textBox2.Text;
- string hexValue = textBox2.Text; //Valoreintero.ToString("X");
- textBox2.Text = "Ok the conversion is ok!";
- // Convert the hex string back to the number
- int intAgain = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);
- //string Hex;
- //string Decim = "0x142CBD";
- // this works, and returns 1322173
- //int hexDecim = Convert.ToInt32(Decim, 16);
- ///int hexDecim = Convert.ToInt32(textBox2.Text);
- //string Decim;
- //Decim = Convert.ToString(hexDecim);
- //textBox4.Text = hexDecim.ToString();
- textBox4.Text = Convert.ToString(intAgain);
- }
- else
- {
- Dec = int.Parse(textBox4.Text);
- textBox4.Text = "Ok the conversion is ok!";
- //Dec = Convert.ToInt32(textBox4.Text);
- //hexdec = Dec.ToString();
- textBox2.Text = Dec.ToString("x");
- //textBox2.Text = Hex;
- }
- //else
- }
- private void button12_Click(object sender, EventArgs e)
- {
- if (String.IsNullOrEmpty(textBox2.Text) == String.IsNullOrEmpty(textBox3.Text))
- {
- MessageBox.Show("Enter your Hex Value and try again!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- //dataGridView1.Rows.Clear();
- return;
- }
- else if ((String.IsNullOrEmpty(textBox3.Text)))
- {
- ///char KeyChar;
- /*string hex = "bacg123";
- long output;
- long.TryParse(hex, System.Globalization.NumberStyles.HexNumber, null, out output);*/
- /*int v;
- string test = "FF";
- if (Int32.TryParse(test, NumberStyles.HexNumber, CultureInfo.CurrentCulture, out v))
- Console.WriteLine("Is HEX:" + v.ToString());,
- //System.Globalization.NumberStyles.HexNumber,
- //System.Globalization.CultureInfo.InvariantCulture, out res))
- {
- //IT'S A VALID HEX
- }*/
- ///int Valoreintero = Convert.ToInt32(textBox2.Text);
- // Convert integer 182 as a hex in a string variable
- //Valoreintero =
- //string Valore = textBox2.Text;
- //string hexValue = textBox2.Text; //Valoreintero.ToString("X");
- string binario = "";
- string valorehex = textBox2.Text;
- binario = Convert.ToString(Convert.ToInt32(valorehex, 16), 2);
- ///return binaryval;
- // Convert the hex string back to the number
- //int intAgain = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);
- //string Hex;
- //string Decim = "0x142CBD";
- // this works, and returns 1322173
- //int hexDecim = Convert.ToInt32(Decim, 16);
- ///int hexDecim = Convert.ToInt32(textBox2.Text);
- //string Decim;
- //Decim = Convert.ToString(hexDecim);
- //textBox4.Text = hexDecim.ToString();
- textBox3.Text = binario;
- //return;
- }
- else if (String.IsNullOrEmpty(textBox2.Text))
- {
- string binario = textBox3.Text;
- string Hexvalue = Convert.ToInt32(binario, 2).ToString("x");
- /// Dec = int.Parse(textBox4.Text);
- textBox3.Text = "Ok the conversion is ok!";
- //Dec = Convert.ToInt32(textBox4.Text);
- //hexdec = Dec.ToString();
- //textBox2.Text = Dec.ToString("x");
- textBox2.Text = Hexvalue;
- }
- }
- private void button5_Click(object sender, EventArgs e)
- {
- if (String.IsNullOrEmpty(textBox3.Text) == String.IsNullOrEmpty(textBox5.Text))
- {
- MessageBox.Show("Enter your Binary Value and try again!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- //dataGridView1.Rows.Clear();
- return;
- }
- else if ((String.IsNullOrEmpty(textBox5.Text)))
- {
- ///int Valoreintero = Convert.ToInt32(textBox2.Text);
- // Convert integer 182 as a hex in a string variable
- //Valoreintero =
- //string Valore = textBox2.Text;
- //string hexValue = textBox2.Text; //Valoreintero.ToString("X");
- //string binario = "";
- //string valorehex = textBox2.Text;
- string binary = textBox3.Text;
- int octal = Convert.ToInt32(binary, 2);
- textBox3.Text = "Ok the conversion is ok!";
- ///binario = Convert.ToString(Convert.ToInt32(valorehex, 16), 2);
- ///return binaryval;
- // Convert the hex string back to the number
- //int intAgain = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);
- //string Hex;
- //string Decim = "0x142CBD";
- // this works, and returns 1322173
- //int hexDecim = Convert.ToInt32(Decim, 16);
- ///int hexDecim = Convert.ToInt32(textBox2.Text);
- //string Decim;
- //Decim = Convert.ToString(hexDecim);
- //textBox4.Text = hexDecim.ToString();
- textBox5.Text = Convert.ToString(octal, 8);
- //return;
- }
- else if (String.IsNullOrEmpty(textBox3.Text))
- {
- //string octalval = textBox5.Text;
- //string octal = textBox5.Text;
- //string octal = textBox5.Text;
- //int octvalinteger = Convert.ToInt32(octal);
- ///int bin = Convert.ToInt32(octal);
- //Convert.ToString(octalval, 2);
- ///string Hexvalue = Convert.ToInt32(binario, 2).ToString("x");
- /// Dec = int.Parse(textBox4.Text);
- ///textBox2.Text = "Ok the conversion is ok!";
- //Dec = Convert.ToInt32(textBox4.Text);
- //hexdec = Dec.ToString();
- //textBox2.Text = Dec.ToString("x");
- // textBox3.Text = Convert.ToString(octvalinteger, 2);
- string[] octalnum = new string[25];
- int r, i = 0;
- int num = Convert.ToInt32(textBox5.Text);
- while (num != 0)
- {
- r = num % 10;
- switch (r)
- {
- case 0:
- octalnum[i++] = "000";
- break;
- case 1:
- octalnum[i++] = "001";
- break;
- case 2:
- octalnum[i++] = "010";
- break;
- case 3:
- octalnum[i++] = "011";
- break;
- case 4:
- octalnum[i++] = "100";
- break;
- case 5:
- octalnum[i++] = "101";
- break;
- case 6:
- octalnum[i++] = "110"; ;
- break;
- case 7:
- octalnum[i++] = "111";
- break;
- }
- num = num / 10;
- }
- for (int j = i - 1; j >= 0; j--)
- {
- textBox5.Text = "Ok the conversion is ok!";
- textBox3.Text += octalnum[j];
- }
- //ax: { }
- }
- }
- private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
- {
- ///bool hextesto;
- char c = e.KeyChar;
- if (c != '\b' && !((c <= 0x66 && c >= 61) || (c <= 0x46 && c >= 0x41) || (c >= 0x30 && c <= 0x39)))
- {
- e.Handled = true;
- ////hextesto = true;
- return;
- }
- /////else
- ///MessageBox.Show("Invalid hex number!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- /*if (e.Handled == false)
- {
- } */
- ///else if (!e.Handled.ToUpperInvariant().Contains("TIME"))
- // MessageBox.Show("Invalid hex number!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- /*if (hextesto == false)
- {
- MessageBox.Show("Invalid hex number!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }*/
- }
- private void textBox2_TextChanged(object sender, EventArgs e)
- {
- }
- private void textBox4_TextChanged(object sender, EventArgs e)
- {
- }
- private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (!char.IsControl(e.KeyChar)
- && !char.IsDigit(e.KeyChar)
- && e.KeyChar != '.')
- {
- e.Handled = true;
- }
- if (e.KeyChar == '.')
- {
- if (textBox4.Text.Contains("."))
- e.Handled = true;
- }
- }
- private void textBox6_TextChanged(object sender, EventArgs e)
- {
- }
- private void textBox6_KeyPress(object sender, KeyPressEventArgs e)
- {
- }
- private void textBox1_TextChanged(object sender, EventArgs e)
- {
- if (String.IsNullOrEmpty(textBox1.Text))
- {
- textBox2.Text = "";
- textBox3.Text = "";
- textBox4.Text = "";
- textBox5.Text = "";
- textBox6.Text = "";
- }
- }
- private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
- {
- e.Handled = !("\b01".Contains(e.KeyChar));
- }
- private void textBox5_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (!char.IsControl(e.KeyChar)
- && !char.IsDigit(e.KeyChar)
- && e.KeyChar != '.')
- {
- e.Handled = true;
- }
- if (e.KeyChar == '.')
- {
- if (textBox1.Text.Contains("."))
- e.Handled = true;
- }
- }
- private void textBox5_TextChanged(object sender, EventArgs e)
- {
- }
- private void Form2_Load(object sender, EventArgs e)
- {
- }
- private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (!char.IsControl(e.KeyChar)
- && !char.IsDigit(e.KeyChar)
- && e.KeyChar != '.')
- {
- e.Handled = true;
- }
- if (e.KeyChar == '.')
- {
- if (textBox1.Text.Contains("."))
- e.Handled = true;
- }
- }
- private void button6_Click_1(object sender, EventArgs e)
- {
- if (String.IsNullOrEmpty(textBox5.Text) == String.IsNullOrEmpty(textBox7.Text))
- {
- MessageBox.Show("Enter your Octal Value and try again!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- //dataGridView1.Rows.Clear();
- return;
- }
- else if (String.IsNullOrEmpty(textBox7.Text))
- {
- string nmb = textBox5.Text;
- int nmbun = Convert.ToInt32(nmb, 8);
- char numascii = Convert.ToChar(nmbun);
- textBox5.Text = "Ok the conversion is ok!";
- textBox7.Text = Convert.ToString(numascii);
- /*string numoc = textBox5.Text;
- int Nmb = int.Parse(numoc);
- ////////int oct = int.Parse(numoc, 2);
- char num = Convert.ToChar(Nmb);
- textBox5.Text = "Ok the conversion is ok!";
- textBox7.Text = Convert.ToString(num); */
- /*string octalval;
- octalval = textBox5.Text;*/
- }
- else if (String.IsNullOrEmpty(textBox5.Text))
- {
- string asciicode = textBox7.Text;
- char ascii = Convert.ToChar(asciicode);
- textBox7.Text = "Ok the conversion is ok!";
- textBox5.Text = Convert.ToString(ascii, 8);
- }
- }
- }
- /*char c = e.KeyChar;
- if (c != '\b' && !((c <= 0x66 && c >= 61) || (c <= 0x46 && c >= 0x41) || (c >= 0x30 && c <= 0x39)))
- {
- e.Handled = true;
- return;
- }
- else
- e.Handled = false;
- if (e.Handled == false)
- {
- MessageBox.Show("Invalid hex number!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- textBox2.Text = "";
- return;
- }*/
- //else
- //e.Handled = false;
- ///e.Handled = false;
- //if (e.Handled == false)
- ///{
- /// MessageBox.Show("Invalid hex number!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- ///}
- }
- ////(IEnumerable<char> chars)
- /*public class Check
- {
- public static void Main(string[] args)
- {
- var a = IsOctal("023"); //true
- var b = IsOctal("678"); //false
- }
- public static bool IsOctal(string text)
- {
- //get a collection of chars that each text element can be
- var possibleChars = Enumerable.Range(0, 8)
- .Select(x => x.ToString()[0]);
- //"are there any chars in 'text' that are not in 'possibleChar'?"
- var anyAreInvalid = text
- .Where(x => !possibleChars.Contains(x))
- .Any();
- //return the inverse of your question
- return !anyAreInvalid;
- }
- }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement