Advertisement
Kirbiflint

Calcolatrice by Kirbiflint Form4.cs

Dec 21st, 2017
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 25.78 KB | None | 0 0
  1. Calcolatrice by Kirbiflint:
  2. Form4.cs
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. using System.Collections.Specialized;
  14. namespace Calcolatrice_by_Kirbiflint2
  15. {
  16.     public partial class Form2 : Form
  17.     {
  18.         public Form2()
  19.         {
  20.             InitializeComponent();
  21.         }
  22.         Double Numero;
  23.        /// int tipooperaz = 0;
  24.         int Num;
  25.         int Risult;
  26.         int Dec;
  27.         long Numerodaconv;
  28.         ///string risultatodeci;
  29.         //int Dec;
  30.         ///string hexdec;
  31.         private void button2_Click(object sender, EventArgs e)
  32.         {
  33.             textBox1.Text = "";
  34.             textBox2.Text = "";
  35.             textBox3.Text = "";
  36.             textBox4.Text = "";
  37.             textBox5.Text = "";
  38.             textBox6.Text = "";
  39.             textBox7.Text = "";
  40.         }
  41.  
  42.         private void button1_Click(object sender, EventArgs e)
  43.         {
  44.  
  45.             //string s;
  46.             //string num;
  47.             //byte ascii;
  48.  
  49.             if (String.IsNullOrEmpty(textBox1.Text))
  50.             {
  51.                 MessageBox.Show("Enter your number which you want to convert and try again!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  52.                 //dataGridView1.Rows.Clear();
  53.             }
  54.             else
  55.  
  56.              
  57.  
  58.             //int Numerodaconv = int.Parse(textBox1.Text);
  59.             Numerodaconv = long.Parse(textBox1.Text);
  60.             char numconvertich = Convert.ToChar(Numerodaconv);
  61.             /// s = Convert.ToString(Numerodaconv) + Encoding.ASCII.GetString(new byte[] { 65 });
  62.             //Numerodaconv = Convert.ToInt32(textBox1.Text);
  63.             /* textBox3.Text = Convert.ToString(Numerodaconv, 2);
  64.              textBox4.Text = Convert.ToString(Numerodaconv, 16); //Numerodaconv.ToString();
  65.              textBox2.Text = Numerodaconv.ToString(); //Convert.ToString(Numerodaconv, 16);
  66.              textBox5.Text = Convert.ToString(Numerodaconv, 8);*/
  67.             textBox3.Text = Convert.ToString(Numerodaconv, 2);
  68.             textBox4.Text = Numerodaconv.ToString();
  69.             textBox2.Text = Convert.ToString(Numerodaconv, 16);
  70.             textBox5.Text = Convert.ToString(Numerodaconv, 8);
  71.             textBox7.Text = Convert.ToString(numconvertich);
  72.  
  73.             /*num = Convert.ToString(textBox1.Text);
  74.             for (int i = 0; i < num.Length; i = i + 1)
  75.             {
  76.  
  77.  
  78.                 ascii = (byte)num[i];
  79.                 textBox7.Text =  " {0}"}, ascii );
  80.                      
  81.             }*/
  82.             ///textBox7.Text = Convert.ToString(ascii);
  83.             /*string ASCIIval = textBox7.Text;
  84.  
  85.             int value = Convert.ToInt32(ASCIIval);
  86.             // Try to parse the input into an Int32
  87.             if (Int32.TryParse(ASCIIval, out value))
  88.             {
  89.                 // Parse was successful
  90.                 if (value >= 0 and value < 128) {
  91.                     //value entered was within the valid ASCII range
  92.                     //cast value to a char and print it
  93.                     char c = (char)value;
  94.                     Console.WriteLine(c);
  95.                 }
  96.             }*/
  97.         }
  98.  
  99.         private void button3_Click(object sender, EventArgs e)
  100.         {
  101.             bool contieneletter = false;
  102.             string Numb = textBox1.Text.Trim();
  103.  
  104.  
  105.             if (String.IsNullOrEmpty(textBox1.Text) == String.IsNullOrEmpty(textBox4.Text))
  106.             {
  107.                 MessageBox.Show("Enter your decimal value and retry!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  108.                 return;
  109.             }
  110.  
  111.             else
  112.            
  113.             for (int i = 0; i < Numb.Length; i++)
  114.             {
  115.                 if (!char.IsNumber(Numb[i])) //== String.IsNullOrEmpty(textBox4.Text))
  116.                 {
  117.                     contieneletter = true;
  118.                 }
  119.                 if (contieneletter) ///== String.IsNullOrEmpty(textBox4.Text))
  120.                 {
  121.                     MessageBox.Show("Please first delete the number or the letter above!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  122.  
  123.                         return;
  124.                 }
  125.                
  126.  
  127.             }
  128.             //if (String.IsNullOrEmpty(textBox1.Text)) //else
  129.             //{
  130.             if (String.IsNullOrEmpty(textBox1.Text))
  131.             {
  132.                 Numero = Convert.ToDouble(textBox4.Text);
  133.                 textBox4.Text = "Enter your second value!";
  134.  
  135.                 Risult = 1;
  136.  
  137.                 ///MessageBox.Show("Ok now you can convert!");
  138.  
  139.                 ///else
  140.                 /// contieneletter = false;  
  141.             //}
  142.  
  143.             }
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.             /*if (String.IsNullOrEmpty(textBox4.Text ) == String.IsNullOrEmpty(textBox1.Text))
  157.            {
  158.                MessageBox.Show("Enter your decimal value and retry!");
  159.            }
  160.  
  161.            else  */
  162.  
  163.             ////int Decimale = int.Parse(textBox4.Text);
  164.  
  165.             //MessageBox.Show("First delete the number above and try again!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  166.  
  167.  
  168.             //MessageBox.Show("Enter your decimal value and retry!");
  169.  
  170.             //textBox4.Text
  171.             //if (textBox4.Text == "")
  172.             //{
  173.             /// MessageBox.Show("Enter your decimal value and retry!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  174.  
  175.             //if (String.IsNullOrEmpty(textBox4.Text) == textBox1.Text.Contains)')) //textBox1.Text.Length > 0)
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.             /*else
  183.  
  184.  
  185.  
  186.  
  187.           ///textBox3.Text = textBox3.Text; */
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.             /*if (Risult == 1)
  198.             {
  199.  
  200.                 Risult = Convert.ToInt32(Numero) + Convert.ToInt32(textBox3.Text, 2);
  201.                 textBox6.Text = Convert.ToString(Risult);
  202.             }*/
  203.  
  204.             //Numero = Convert.ToDouble(textBox4.Text) + Convert.ToDouble(Convert.ToInt32(textBox3.Text, 2));
  205.  
  206.  
  207.  
  208.  
  209.             //textBox4.Text = "0";
  210.  
  211.             ///int Binario = Convert.ToInt32(textBox3.Text);
  212.  
  213.  
  214.             //textBox6.Text = Convert.ToString(Numero);
  215.  
  216.  
  217.  
  218.             ///string risultatodeci;
  219.  
  220.             /*if (textBox4.Text == "")
  221.             {
  222.  
  223.  
  224.                 MessageBox.Show("Enter your decimal value and retry!");
  225.  
  226.             }
  227.  
  228.             else
  229.  
  230.             risultatodeci = textBox4.Text;
  231.             textBox4.Text = "Enter your second decimal value!";
  232.             Risult = 1;
  233.  
  234.             ////Num = Convert.ToInt32(textBox4.Text);
  235.             //Risult = 1;
  236.             ////textBox4.Text = "Enter the decimal value to calculate!";
  237.  
  238.  
  239.  
  240.             /*if (Risult == 1)
  241.  
  242.             {
  243.  
  244.                 //textBox4.Text = "0";
  245.                 Num = Convert.ToInt32(textBox4.Text);
  246.                 textBox4.Text = "0";
  247.  
  248.                 ///textBox4.Text = Convert.ToString(Numerodaconv, 16)
  249.  
  250.  
  251.  
  252.                 //textBox6.Text = textBox4.Text + Convert.ToString(Num) ;
  253.  
  254.  
  255.  
  256.             }
  257.                 /*textBox4.Text = "0";
  258.                 Numero = Convert.ToDouble(textBox4.Text);
  259.                 textBox4.Text = "0";
  260.  
  261.  
  262.  
  263.  
  264.  
  265.                     textBox6.Text = Convert.ToString(Numero) + textBox4.Text;*/
  266.  
  267.  
  268.         }
  269.  
  270.  
  271.         private void button10_Click(object sender, EventArgs e)
  272.         {
  273.             int secondovaloretxt;
  274.  
  275.             if (String.IsNullOrEmpty(textBox4.Text))
  276.             {
  277.                 MessageBox.Show("Sorry to calculate the result, enter the decimal value!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  278.                 return;
  279.             }
  280.             else
  281.  
  282.             secondovaloretxt = System.Convert.ToInt32(textBox4.Text);
  283.             switch (Risult)
  284.             {
  285.  
  286.  
  287.                 case 1:
  288.                     Risult = secondovaloretxt + Convert.ToInt32(Numero);
  289.                     textBox6.Text = Convert.ToString(Risult);
  290.                     break;
  291.  
  292.  
  293.                 case 2:
  294.  
  295.                     Risult = secondovaloretxt - Convert.ToInt32(Numero);
  296.                     textBox6.Text = Convert.ToString(Risult);
  297.                     break;
  298.  
  299.             }
  300.         }
  301.  
  302.         private void button6_Click(object sender, EventArgs e)
  303.         {
  304.             if (textBox2.Text == "")
  305.             {
  306.                 MessageBox.Show("Enter your Hex decimal value and retry!");
  307.             }
  308.             else
  309.  
  310.             Num = Convert.ToInt32(textBox2.Text);
  311.             textBox2.Text = "Enter your second value!";
  312.             Risult = 1;
  313.                
  314.  
  315.            
  316.     }
  317.  
  318.         private void button11_Click(object sender, EventArgs e)
  319.         {
  320.             switch (Risult)
  321.             {
  322.  
  323.                 case 1:
  324.  
  325.                     textBox6.Text = Convert.ToString(Convert.ToInt32(Num)) + Num;
  326.                     break;
  327.  
  328.  
  329.  
  330.             }
  331.         }
  332.  
  333.         private void button4_Click(object sender, EventArgs e)
  334.         {
  335.             bool contieneletter = false;
  336.             string Numb = textBox1.Text.Trim();
  337.  
  338.  
  339.             if (String.IsNullOrEmpty(textBox1.Text) == String.IsNullOrEmpty(textBox4.Text))
  340.             {
  341.                 MessageBox.Show("Enter your decimal value and retry!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  342.                 return;
  343.             }
  344.  
  345.             else
  346.  
  347.                 for (int i = 0; i < Numb.Length; i++)
  348.                 {
  349.                     if (!char.IsNumber(Numb[i])) //== String.IsNullOrEmpty(textBox4.Text))
  350.                     {
  351.                         contieneletter = true;
  352.                     }
  353.                     if (contieneletter) ///== String.IsNullOrEmpty(textBox4.Text))
  354.                     {
  355.                         MessageBox.Show("Please first delete the number or the letter above!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  356.  
  357.                         return;
  358.                     }
  359.  
  360.  
  361.                 }
  362.             //if (String.IsNullOrEmpty(textBox1.Text)) //else
  363.             //{
  364.             if (String.IsNullOrEmpty(textBox1.Text))
  365.             {
  366.                 Numero = Convert.ToDouble(textBox4.Text);
  367.                 textBox4.Text = "Enter your second value!";
  368.  
  369.                 Risult = 2;
  370.  
  371.                 ///MessageBox.Show("Ok now you can convert!");
  372.  
  373.                 ///else
  374.                 /// contieneletter = false;  
  375.             //}
  376.  
  377.             }
  378.         }
  379.  
  380.         private void button9_Click(object sender, EventArgs e)
  381.         {
  382.  
  383.            
  384.             if (String.IsNullOrEmpty(textBox4.Text) ==  String.IsNullOrEmpty(textBox2.Text))
  385.             {
  386.                 MessageBox.Show("Enter your Decimal Value and try again!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  387.                 //dataGridView1.Rows.Clear();
  388.             }
  389.  
  390.             else if ((String.IsNullOrEmpty(textBox4.Text)))
  391.  
  392.             {
  393.                 ///int Valoreintero = Convert.ToInt32(textBox2.Text);
  394.                 // Convert integer 182 as a hex in a string variable
  395.                 //Valoreintero =
  396.                 //string Valore = textBox2.Text;
  397.                 string hexValue = textBox2.Text; //Valoreintero.ToString("X");
  398.                 textBox2.Text = "Ok the conversion is ok!";
  399.                 // Convert the hex string back to the number
  400.                 int intAgain = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);
  401.                 //string Hex;
  402.                 //string Decim = "0x142CBD";
  403.                 // this works, and returns 1322173
  404.                 //int hexDecim = Convert.ToInt32(Decim, 16);
  405.                 ///int hexDecim = Convert.ToInt32(textBox2.Text);
  406.                 //string Decim;
  407.                 //Decim = Convert.ToString(hexDecim);
  408.                 //textBox4.Text = hexDecim.ToString();
  409.                 textBox4.Text = Convert.ToString(intAgain);
  410.  
  411.             }
  412.             else
  413.             {
  414.  
  415.                 Dec = int.Parse(textBox4.Text);
  416.                 textBox4.Text = "Ok the conversion is ok!";
  417.                 //Dec = Convert.ToInt32(textBox4.Text);
  418.                 //hexdec = Dec.ToString();
  419.                 textBox2.Text = Dec.ToString("x");
  420.                 //textBox2.Text = Hex;
  421.  
  422.             }
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.             //else
  431.  
  432.  
  433.  
  434.         }
  435.  
  436.         private void button12_Click(object sender, EventArgs e)
  437.         {
  438.             if (String.IsNullOrEmpty(textBox2.Text) == String.IsNullOrEmpty(textBox3.Text))
  439.             {
  440.                 MessageBox.Show("Enter your Hex Value and try again!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  441.                 //dataGridView1.Rows.Clear();
  442.                 return;
  443.             }
  444.  
  445.             else if ((String.IsNullOrEmpty(textBox3.Text)))
  446.  
  447.             {
  448.  
  449.                 ///char KeyChar;
  450.  
  451.                
  452.  
  453.  
  454.                 /*string hex = "bacg123";
  455.                 long output;
  456.                 long.TryParse(hex, System.Globalization.NumberStyles.HexNumber, null, out output);*/
  457.  
  458.  
  459.  
  460.  
  461.                 /*int v;
  462.                 string test = "FF";
  463.                 if (Int32.TryParse(test, NumberStyles.HexNumber, CultureInfo.CurrentCulture, out v))
  464.                     Console.WriteLine("Is HEX:" + v.ToString());,
  465.                          //System.Globalization.NumberStyles.HexNumber,
  466.                          //System.Globalization.CultureInfo.InvariantCulture, out res))
  467.                 {
  468.  
  469.                     //IT'S A VALID HEX
  470.                 }*/
  471.  
  472.  
  473.  
  474.  
  475.                 ///int Valoreintero = Convert.ToInt32(textBox2.Text);
  476.                 // Convert integer 182 as a hex in a string variable
  477.                 //Valoreintero =
  478.                 //string Valore = textBox2.Text;
  479.                 //string hexValue = textBox2.Text; //Valoreintero.ToString("X");
  480.                 string binario = "";
  481.                 string valorehex = textBox2.Text;
  482.  
  483.                 binario = Convert.ToString(Convert.ToInt32(valorehex, 16), 2);
  484.                 ///return binaryval;
  485.                 // Convert the hex string back to the number
  486.                 //int intAgain = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);
  487.                 //string Hex;
  488.                 //string Decim = "0x142CBD";
  489.                 // this works, and returns 1322173
  490.                 //int hexDecim = Convert.ToInt32(Decim, 16);
  491.                 ///int hexDecim = Convert.ToInt32(textBox2.Text);
  492.                 //string Decim;
  493.                 //Decim = Convert.ToString(hexDecim);
  494.                 //textBox4.Text = hexDecim.ToString();
  495.                 textBox3.Text = binario;
  496.                 //return;
  497.             }
  498.             else if (String.IsNullOrEmpty(textBox2.Text))
  499.             {
  500.                 string binario = textBox3.Text;
  501.                 string Hexvalue = Convert.ToInt32(binario, 2).ToString("x");
  502.                 /// Dec = int.Parse(textBox4.Text);
  503.                  textBox3.Text = "Ok the conversion is ok!";
  504.                  //Dec = Convert.ToInt32(textBox4.Text);
  505.                  //hexdec = Dec.ToString();
  506.                  //textBox2.Text = Dec.ToString("x");
  507.                  textBox2.Text = Hexvalue;
  508.  
  509.             }
  510.         }
  511.  
  512.         private void button5_Click(object sender, EventArgs e)
  513.         {
  514.             if (String.IsNullOrEmpty(textBox3.Text) == String.IsNullOrEmpty(textBox5.Text))
  515.             {
  516.                 MessageBox.Show("Enter your Binary Value and try again!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  517.                 //dataGridView1.Rows.Clear();
  518.                 return;
  519.             }
  520.  
  521.             else if ((String.IsNullOrEmpty(textBox5.Text)))
  522.  
  523.             {
  524.                 ///int Valoreintero = Convert.ToInt32(textBox2.Text);
  525.                 // Convert integer 182 as a hex in a string variable
  526.                 //Valoreintero =
  527.                 //string Valore = textBox2.Text;
  528.                 //string hexValue = textBox2.Text; //Valoreintero.ToString("X");
  529.                 //string binario = "";
  530.                 //string valorehex = textBox2.Text;
  531.                 string binary = textBox3.Text;
  532.                 int octal = Convert.ToInt32(binary, 2);
  533.                 textBox3.Text = "Ok the conversion is ok!";
  534.  
  535.                 ///binario = Convert.ToString(Convert.ToInt32(valorehex, 16), 2);
  536.                 ///return binaryval;
  537.                 // Convert the hex string back to the number
  538.                 //int intAgain = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);
  539.                 //string Hex;
  540.                 //string Decim = "0x142CBD";
  541.                 // this works, and returns 1322173
  542.                 //int hexDecim = Convert.ToInt32(Decim, 16);
  543.                 ///int hexDecim = Convert.ToInt32(textBox2.Text);
  544.                 //string Decim;
  545.                 //Decim = Convert.ToString(hexDecim);
  546.                 //textBox4.Text = hexDecim.ToString();
  547.                 textBox5.Text = Convert.ToString(octal, 8);
  548.                 //return;
  549.             }
  550.             else if (String.IsNullOrEmpty(textBox3.Text))
  551.             {
  552.                 //string octalval = textBox5.Text;
  553.                 //string octal = textBox5.Text;
  554.  
  555.                 //string octal = textBox5.Text;
  556.                 //int octvalinteger = Convert.ToInt32(octal);  
  557.  
  558.                 ///int bin = Convert.ToInt32(octal);
  559.                 //Convert.ToString(octalval, 2);
  560.                 ///string Hexvalue = Convert.ToInt32(binario, 2).ToString("x");
  561.                 /// Dec = int.Parse(textBox4.Text);
  562.                 ///textBox2.Text = "Ok the conversion is ok!";
  563.                 //Dec = Convert.ToInt32(textBox4.Text);
  564.                 //hexdec = Dec.ToString();
  565.                 //textBox2.Text = Dec.ToString("x");
  566.                 //   textBox3.Text = Convert.ToString(octvalinteger, 2);
  567.  
  568.                 string[] octalnum = new string[25];
  569.  
  570.                 int r, i = 0;
  571.  
  572.                 int num = Convert.ToInt32(textBox5.Text);
  573.  
  574.                 while (num != 0)
  575.  
  576.                 {
  577.  
  578.                     r = num % 10;
  579.  
  580.                     switch (r)
  581.  
  582.                    {
  583.  
  584.                         case 0:
  585.  
  586.                             octalnum[i++] = "000";
  587.  
  588.                             break;
  589.  
  590.                         case 1:
  591.  
  592.                             octalnum[i++] = "001";
  593.  
  594.                             break;
  595.  
  596.                         case 2:
  597.  
  598.                             octalnum[i++] = "010";
  599.  
  600.                             break;
  601.  
  602.                         case 3:
  603.  
  604.                             octalnum[i++] = "011";
  605.  
  606.                             break;
  607.  
  608.                         case 4:
  609.  
  610.                             octalnum[i++] = "100";
  611.  
  612.                             break;
  613.  
  614.                         case 5:
  615.  
  616.                             octalnum[i++] = "101";
  617.  
  618.                             break;
  619.  
  620.                         case 6:
  621.  
  622.                             octalnum[i++] = "110"; ;
  623.  
  624.                             break;
  625.  
  626.                         case 7:
  627.  
  628.                             octalnum[i++] = "111";
  629.  
  630.                             break;
  631.  
  632.                        
  633.  
  634.                     }
  635.  
  636.                     num = num / 10;
  637.  
  638.                 }
  639.  
  640.  
  641.  
  642.                 for (int j = i - 1; j >= 0; j--)
  643.  
  644.                 {
  645.                     textBox5.Text = "Ok the conversion is ok!";
  646.                     textBox3.Text += octalnum[j];
  647.  
  648.                 }
  649.  
  650.             //ax: { }
  651.  
  652.             }
  653.         }
  654.  
  655.         private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
  656.         {
  657.  
  658.             ///bool hextesto;
  659.  
  660.             char c = e.KeyChar;
  661.  
  662.             if (c != '\b' && !((c <= 0x66 && c >= 61) || (c <= 0x46 && c >= 0x41) || (c >= 0x30 && c <= 0x39)))
  663.             {
  664.                 e.Handled = true;
  665.                 ////hextesto = true;
  666.                 return;
  667.             }
  668.             /////else
  669.  
  670.             ///MessageBox.Show("Invalid hex number!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  671.  
  672.             /*if (e.Handled == false)
  673.            {
  674.  
  675.  
  676.            } */
  677.  
  678.             ///else if (!e.Handled.ToUpperInvariant().Contains("TIME"))
  679.  
  680.  
  681.             // MessageBox.Show("Invalid hex number!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  682.  
  683.             /*if (hextesto == false)
  684.             {
  685.  
  686.  
  687.                 MessageBox.Show("Invalid hex number!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  688.  
  689.             }*/
  690.  
  691.         }
  692.  
  693.         private void textBox2_TextChanged(object sender, EventArgs e)
  694.         {
  695.  
  696.         }
  697.  
  698.         private void textBox4_TextChanged(object sender, EventArgs e)
  699.         {
  700.  
  701.         }
  702.  
  703.         private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
  704.         {
  705.             if (!char.IsControl(e.KeyChar)
  706.                 && !char.IsDigit(e.KeyChar)
  707.                 && e.KeyChar != '.')
  708.             {
  709.                 e.Handled = true;
  710.  
  711.             }
  712.             if (e.KeyChar == '.')
  713.             {
  714.  
  715.                 if (textBox4.Text.Contains("."))
  716.                     e.Handled = true;
  717.  
  718.             }
  719.         }
  720.  
  721.         private void textBox6_TextChanged(object sender, EventArgs e)
  722.         {
  723.            
  724.         }
  725.  
  726.         private void textBox6_KeyPress(object sender, KeyPressEventArgs e)
  727.         {
  728.            
  729.         }
  730.  
  731.         private void textBox1_TextChanged(object sender, EventArgs e)
  732.         {
  733.            
  734.  
  735.             if (String.IsNullOrEmpty(textBox1.Text))
  736.             {
  737.                 textBox2.Text = "";
  738.                 textBox3.Text = "";
  739.                 textBox4.Text = "";
  740.                 textBox5.Text = "";
  741.                 textBox6.Text = "";
  742.  
  743.  
  744.  
  745.             }
  746.         }
  747.  
  748.         private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
  749.         {
  750.  
  751.  
  752.             e.Handled = !("\b01".Contains(e.KeyChar));
  753.         }
  754.  
  755.         private void textBox5_KeyPress(object sender, KeyPressEventArgs e)
  756.         {
  757.             if (!char.IsControl(e.KeyChar)
  758.                 && !char.IsDigit(e.KeyChar)
  759.                 && e.KeyChar != '.')
  760.             {
  761.                 e.Handled = true;
  762.  
  763.             }
  764.             if (e.KeyChar == '.')
  765.             {
  766.  
  767.                 if (textBox1.Text.Contains("."))
  768.                     e.Handled = true;
  769.  
  770.             }
  771.  
  772.         }
  773.  
  774.         private void textBox5_TextChanged(object sender, EventArgs e)
  775.         {
  776.            
  777.         }
  778.  
  779.         private void Form2_Load(object sender, EventArgs e)
  780.         {
  781.  
  782.         }
  783.  
  784.         private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
  785.         {
  786.             if (!char.IsControl(e.KeyChar)
  787.                 && !char.IsDigit(e.KeyChar)
  788.                 && e.KeyChar != '.')
  789.             {
  790.                 e.Handled = true;
  791.  
  792.             }
  793.             if (e.KeyChar == '.')
  794.             {
  795.  
  796.                 if (textBox1.Text.Contains("."))
  797.                     e.Handled = true;
  798.  
  799.             }
  800.         }
  801.  
  802.         private void button6_Click_1(object sender, EventArgs e)
  803.         {
  804.             if (String.IsNullOrEmpty(textBox5.Text) == String.IsNullOrEmpty(textBox7.Text))
  805.             {
  806.                 MessageBox.Show("Enter your Octal Value and try again!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  807.                 //dataGridView1.Rows.Clear();
  808.                 return;
  809.             }
  810.  
  811.             else if (String.IsNullOrEmpty(textBox7.Text))
  812.  
  813.             {
  814.  
  815.                 string nmb = textBox5.Text;
  816.                 int nmbun = Convert.ToInt32(nmb, 8);
  817.                 char numascii = Convert.ToChar(nmbun);
  818.                 textBox5.Text = "Ok the conversion is ok!";
  819.                 textBox7.Text = Convert.ToString(numascii);
  820.                 /*string numoc = textBox5.Text;
  821.                 int Nmb = int.Parse(numoc);
  822.                 ////////int oct = int.Parse(numoc, 2);
  823.                
  824.                 char num = Convert.ToChar(Nmb);
  825.                 textBox5.Text = "Ok the conversion is ok!";
  826.                 textBox7.Text = Convert.ToString(num); */
  827.  
  828.  
  829.  
  830.                 /*string octalval;
  831.  
  832.                 octalval = textBox5.Text;*/
  833.  
  834.  
  835.  
  836.  
  837.             }
  838.  
  839.             else if (String.IsNullOrEmpty(textBox5.Text))
  840.             {
  841.  
  842.  
  843.                 string asciicode = textBox7.Text;
  844.                 char ascii = Convert.ToChar(asciicode);
  845.                 textBox7.Text = "Ok the conversion is ok!";
  846.                 textBox5.Text = Convert.ToString(ascii, 8);
  847.  
  848.  
  849.  
  850.             }
  851.  
  852.  
  853.  
  854.  
  855.             }
  856.  
  857.  
  858.     }
  859.  
  860.  
  861.  
  862.  
  863.  
  864.         /*char c = e.KeyChar;
  865.  
  866.         if (c != '\b' && !((c <= 0x66 && c >= 61) || (c <= 0x46 && c >= 0x41) || (c >= 0x30 && c <= 0x39)))
  867.         {
  868.             e.Handled = true;
  869.             return;
  870.         }
  871.         else
  872.  
  873.             e.Handled = false;
  874.  
  875.  
  876.         if (e.Handled == false)
  877.         {
  878.  
  879.  
  880.             MessageBox.Show("Invalid hex number!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  881.             textBox2.Text = "";
  882.             return;
  883.  
  884.         }*/
  885.         //else
  886.  
  887.         //e.Handled = false;
  888.  
  889.  
  890.  
  891.         ///e.Handled = false;
  892.  
  893.         //if (e.Handled == false)
  894.         ///{
  895.  
  896.         ///   MessageBox.Show("Invalid hex number!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  897.  
  898.         ///}
  899.     }
  900.  
  901. ////(IEnumerable<char> chars)
  902.  
  903.  
  904.     /*public class Check
  905. {
  906.  
  907.     public static void Main(string[] args)
  908.     {
  909.  
  910.  
  911.         var a = IsOctal("023"); //true
  912.         var b = IsOctal("678"); //false
  913.  
  914.  
  915.  
  916.     }
  917.     public static bool IsOctal(string text)
  918.     {
  919.         //get a collection of chars that each text element can be
  920.         var possibleChars = Enumerable.Range(0, 8)
  921.             .Select(x => x.ToString()[0]);
  922.  
  923.         //"are there any chars in 'text' that are not in 'possibleChar'?"
  924.         var anyAreInvalid = text
  925.             .Where(x => !possibleChars.Contains(x))
  926.             .Any();
  927.  
  928.         //return the inverse of your question
  929.         return !anyAreInvalid;
  930.     }
  931.  
  932.  
  933.  
  934.  
  935. }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement