private void Button_click(object sender, EventArgs e) { Button clickedButton = (Button)sender; ButtonStruct bs = (ButtonStruct)clickedButton.Tag; if (bs.isNumber) { if (resultBox.Text == "0") { resultBox.Text = null; } resultBox.Text += clickedButton.Text; } else { if (bs.isDecimalSeparator) { if (!resultBox.Text.Contains(bs.Content)) { resultBox.Text = clickedButton.Text; } } if (bs.isPlusMinusSign) { if (!resultBox.Text.Contains(bs.Content)) { resultBox.Text = "-" + resultBox.Text; } else { resultBox.Text = resultBox.Text.Replace("-", " "); } } } }
private void makebuttons(ButtonStruct[,] buttons) { int buttonWidth = 82; int buttonHeight = 60; int posX = 0; int posY = 101; for (int i = 0; i < buttons.GetLength(0); i++) { posX = 0; for (int j = 0; j < buttons.GetLength(1); j++) { Button newButton = new Button(); newButton.Text = buttons[i, j].Content.ToString(); newButton.Font = new Font("segoe UI", 16); ButtonStruct bs = buttons[i, j]; if (bs.isBold) { newButton.Font = new Font(newButton.Font, FontStyle.Bold); } //newButton.Text = buttons[i,j].ToString(); newButton.Width = buttonWidth; newButton.Height = buttonHeight; newButton.Left = posX; newButton.Top = posY; newButton.Tag = bs; newButton.Click += Button_click; posX += buttonWidth; this.Controls.Add(newButton); } posY += buttonHeight; } }
private void MakeButtons(ButtonStruct[,] buttons) { int buttonWidth = 82; int buttonHeight = 60; int posX = 0; int posY = 101; for (int i = 0; i < buttons.GetLength(0); i++) { for (int j = 0; j < buttons.GetLength(1); j++) { Button newButton = new Button(); newButton.Font = new Font("Segoe UI", 16); // newButton.Text = buttons[i, j].ToString(); ButtonStruct bs = buttons[i, j]; newButton.Text = bs.ToString(); if (bs.IsBold) { newButton.Font = new Font(newButton.Font, FontStyle.Bold); } newButton.Width = buttonWidth; newButton.Height = buttonHeight; newButton.Left = posX; newButton.Top = posY; this.Controls.Add(newButton); posX += buttonWidth; } posX = 0; posY += buttonHeight; } }
private void MakeButtons(ButtonStruct[,] buttons) { int buttonWidth = 82, buttonHeight = 60; int posX = 0, posY = 101; for (int i = 0; i < buttons.GetLength(0); i++) { for (int j = 0; j < buttons.GetLength(1); j++) { Button newButton = new Button(); newButton.Font = new Font("Segoe UI", 16); //override: newButton.Text = buttons[i,j].ToString(); ButtonStruct bs = buttons[i, j]; newButton.Text = bs.Content.ToString(); if (bs.IsBold) { newButton.Font = new Font(newButton.Font, FontStyle.Bold); } newButton.Width = buttonWidth; newButton.Height = buttonHeight; newButton.Left = posX; newButton.Top = posY; newButton.Tag = bs;//proprietà tag serve per taggare pulsante newButton.Click += Button_Click; this.Controls.Add(newButton); posX += buttonWidth; } posX = 0; posY += buttonHeight; } }
private void manageOperators(ButtonStruct bs) { if (lastOperator == ASCIIZERO) { operand1 = double.Parse(resultBox.Text); lastOperator = bs.Content; } else { if (lastButtonClicked.IsOperator && !lastButtonClicked.IsEqualSign) { lastOperator = bs.Content; } else { if (!lastButtonClicked.IsEqualSign) { operand2 = double.Parse(resultBox.Text); } switch (lastOperator) { case '+': result = operand1 + operand2; break; case '-': result = operand1 - operand2; break; case 'x': result = operand1 * operand2; break; case '/': result = operand1 / operand2; break; default: break; } operand1 = result; if (!bs.IsEqualSign) { lastOperator = bs.Content; operand2 = 0; } resultBox.Text = getFormattedNumber(result); } } }
public static void Draw() { //options menu box BoxStruct Box1 = new BoxStruct(); Box1.left = (Screen.width/2)-((Screen.width/6)/2); Box1.top = (Screen.height/2)-((Screen.height/2)/2); Box1.width = (Screen.width/6); Box1.height = (Screen.height/2); Box1.text = GetLanguageValueXml.GetValue("optionsmenu", "title"); GUI.Box(new Rect(Box1.left, Box1.top, Box1.width, Box1.height), Box1.text); //options menu language selector //QuitButton.left has to be calculated after QuitButton.width! ButtonStruct LanguageButton = new ButtonStruct(); LanguageButton.top = Box1.top + ((Box1.height/4)); LanguageButton.width = (Box1.width - (Box1.width/8)); LanguageButton.left = Box1.left + ((Box1.width - LanguageButton.width)/2); LanguageButton.height = 20; LanguageButton.text = GetLanguageValueXml.GetValue("optionsmenu", "language"); GUIContent[] listContent = new GUIContent[3]; listContent[0] = new GUIContent(""); listContent[1] = new GUIContent(GetLanguageValueXml.GetValue("optionsmenu", "english")); listContent[2] = new GUIContent(GetLanguageValueXml.GetValue("optionsmenu", "german")); GUIStyle listStyle = new GUIStyle(); //listStyle.padding.left = listStyle.padding.right = listStyle.padding.top = listStyle.padding.bottom = 10; listStyle.normal.textColor = Color.white; Texture2D tex = new Texture2D(1, 1); listStyle.hover.background = tex; listStyle.onHover.background = tex; //listStyle.hover.textColor = Color.grey; //listStyle.onHover.textColor = Color.grey; listStyle.padding.left = listStyle.padding.right = listStyle.padding.top = listStyle.padding.bottom = 4; Popup.List(new Rect(LanguageButton.left, LanguageButton.top, LanguageButton.width, LanguageButton.height), ref showList, ref selectedItem, new GUIContent(LanguageButton.text), listContent, listStyle); //options menu cancel button //CancelButton.left has to be calculated after CancelButton.width! ButtonStruct CancelButton = new ButtonStruct(); CancelButton.top = LanguageButton.top + (LanguageButton.height * (listContent.Length + 1)) + (LanguageButton.height/2); CancelButton.width = (Box1.width - (Box1.width/8)); CancelButton.left = Box1.left + ((Box1.width - CancelButton.width)/2); CancelButton.height = 20; CancelButton.text = GetLanguageValueXml.GetValue("escmenu", "cancel"); if (GUI.Button (new Rect(CancelButton.left, CancelButton.top, CancelButton.width, CancelButton.height), CancelButton.text)) { Deactivate(); } }
public static void Draw() { //escape menu box BoxStruct Box1 = new BoxStruct(); Box1.left = (Screen.width/2)-((Screen.width/6)/2); Box1.top = (Screen.height/2)-((Screen.height/2)/2); Box1.width = (Screen.width/6); Box1.height = (Screen.height/2); Box1.text = GetLanguageValueXml.GetValue("escmenu", "title"); GUI.Box(new Rect(Box1.left, Box1.top, Box1.width, Box1.height), Box1.text); //escape menu options button //QuitButton.left has to be calculated after QuitButton.width! ButtonStruct OptionsButton = new ButtonStruct(); OptionsButton.top = Box1.top + ((Box1.height/4)); OptionsButton.width = (Box1.width - (Box1.width/8)); OptionsButton.left = Box1.left + ((Box1.width - OptionsButton.width)/2); OptionsButton.height = 20; OptionsButton.text = GetLanguageValueXml.GetValue("escmenu", "options"); if (GUI.Button (new Rect(OptionsButton.left, OptionsButton.top, OptionsButton.width, OptionsButton.height), OptionsButton.text)) { EscMenuActive = false; OptionsMenu.Call(); } //escape menu quit button //OptionsButton.left has to be calculated after CancelButton.width! ButtonStruct QuitButton = new ButtonStruct(); QuitButton.top = OptionsButton.top + OptionsButton.height + (OptionsButton.height/2); QuitButton.width = (Box1.width - (Box1.width/8)); QuitButton.left = Box1.left + ((Box1.width - OptionsButton.width)/2); QuitButton.height = 20; QuitButton.text = GetLanguageValueXml.GetValue("escmenu", "quit"); if (GUI.Button (new Rect(QuitButton.left, QuitButton.top, QuitButton.width, QuitButton.height), QuitButton.text)) { Application.Quit(); } //escape menu cancel button //CancelButton.left has to be calculated after CancelButton.width! ButtonStruct CancelButton = new ButtonStruct(); CancelButton.top = QuitButton.top + QuitButton.height + (QuitButton.height/2); CancelButton.width = (Box1.width - (Box1.width/8)); CancelButton.left = Box1.left + ((Box1.width - CancelButton.width)/2); CancelButton.height = 20; CancelButton.text = GetLanguageValueXml.GetValue("escmenu", "cancel"); if (GUI.Button (new Rect(CancelButton.left, CancelButton.top, CancelButton.width, CancelButton.height), CancelButton.text)) { Deactivate(); } }
private void Button_Click(object sender, EventArgs e) { Button clickedButton = (Button)sender; ButtonStruct bs = (ButtonStruct)clickedButton.Tag; /*MessageBox.Show("Button "+ clickedButton.Text);*/ if (bs.IsNumber) { if (resultBox.Text == "0") { resultBox.Text = ""; } resultBox.Text += clickedButton.Text; } else { if (bs.IsDeciamlSeparator) { if (!resultBox.Text.Contains(bs.Content)) { resultBox.Text += clickedButton.Text; } } if (bs.IsPlusMinus) { if (!resultBox.Text.Contains("-")) { resultBox.Text += " - "; } else { resultBox.Text = resultBox.Text.Replace("-", ""); } } } }
private void Button_Click(object sender, EventArgs e) { Button clickedButton = (Button)sender; ButtonStruct bs = (ButtonStruct)clickedButton.Tag; if (bs.IsNumber) { if (lastButtonClicked.IsEqualSign) { clearAll(); } if (resultBox.Text == "0" || lastButtonClicked.IsOperator) { resultBox.Text = ""; } if (resultBox.Text.Length < 20) { resultBox.Text += clickedButton.Text; } } else { if (bs.IsDecimalSeparator) { if (!resultBox.Text.Contains(bs.Content)) { if (resultBox.Text.Length < 20) { resultBox.Text += clickedButton.Text; } } } if (bs.IsPlusMinusSign) { if (!resultBox.Text.Contains("-")) { resultBox.Text = "-" + resultBox.Text; } else { resultBox.Text = resultBox.Text.Replace("-", ""); } } else { switch (bs.Content) { case 'C': clearAll(); break; case '<': resultBox.Text = resultBox.Text.Remove(resultBox.Text.Length - 1); if (resultBox.Text.Length == 0 || resultBox.Text == "-0" || resultBox.Text == "-") { resultBox.Text = "0"; } break; default: if (bs.IsOperator) { manageOperators(bs); } break; } } } lastButtonClicked = bs; }
private void manageOperators(ButtonStruct bs) { //mettere prima di switch per ordine operazioni if (bs.IsSpecialOperator) { switch (bs.Content) { case '¼': if (lastOperator != ASCIIZERO) { operand2 = 1 / (Convert.ToDouble(resultBox.Text)); resultBox.Text = getFormattedNumber(operand2); } else { result = 1 / (Convert.ToDouble(resultBox.Text)); resultBox.Text = getFormattedNumber(result); } break; case '√': if (lastOperator != ASCIIZERO) { operand2 = Math.Sqrt(Convert.ToDouble(resultBox.Text)); resultBox.Text = getFormattedNumber(operand2); } else { result = Math.Sqrt(Convert.ToDouble(resultBox.Text)); resultBox.Text = getFormattedNumber(result); } break; case '²': if (lastOperator != ASCIIZERO) { operand2 = Math.Pow(Convert.ToDouble(resultBox.Text), 2); resultBox.Text = getFormattedNumber(operand2); } else { result = Math.Pow(Convert.ToDouble(resultBox.Text), 2); resultBox.Text = getFormattedNumber(result); } break; } } if (lastOperator == ASCIIZERO)//valore di default { operand1 = double.Parse(resultBox.Text); lastOperator = bs.Content; } else { if (lastButtonClicked.IsOperator && !lastButtonClicked.IsEqualSign) { lastOperator = bs.Content; } else { //if (!lastButtonClicked.IsEqualSign&&bs.Content!= '¼'&&bs.Content!= '√') if (!lastButtonClicked.IsEqualSign && bs.Content != '¼' && !bs.IsSpecialOperator) { operand2 = double.Parse(resultBox.Text); } switch (lastOperator) { case '+': result = operand1 + operand2; break; case '-': result = operand1 - operand2; break; case 'x': result = operand1 * operand2; break; case '/': result = operand1 / operand2; break; /*case '¼': * result = 1 / result;//TODO: gestire casi particolar * break;*/ default: break; } operand1 = result; if (!bs.IsEqualSign) { lastOperator = bs.Content; operand2 = 0; } else { lastOperator = ASCIIZERO; } resultBox.Text = getFormattedNumber(result); } } }
private void Button_Click(object sender, EventArgs e) { //qualsiasi controllo del netframework deriva sempre da object //se voglio utilizzare proprietà specifiche dei bottoni devo effettuare casting Button clickedButton = (Button)sender; //effettuo casting del sender, per definirlo button dal più generico object //MessageBox.Show("Button: "+clickedButton.Text); ButtonStruct bs = (ButtonStruct)clickedButton.Tag; //casting perchè oggetto if (bs.IsNumber) { if (lastButtonClicked.IsEqualSign) { clearAll(); } if (resultBox.Text == "0" || lastButtonClicked.IsOperator) { resultBox.Text = ""; } if (resultBox.Text.Length < 20) { resultBox.Text += clickedButton.Text; } } else { if (bs.IsDecimalSeparator) { if (!resultBox.Text.Contains(bs.Content.ToString())) { resultBox.Text += clickedButton.Text; } } if (bs.IsPlusMinusSign && resultBox.Text != "0")//-0 non esiste { if (!resultBox.Text.Contains("-")) { resultBox.Text = "-" + resultBox.Text; } else { resultBox.Text = resultBox.Text.Substring(1); } } else { switch (bs.Content) { case 'C': clearAll(); break; case '<': if (resultBox.Text.Length != 0) { resultBox.Text = resultBox.Text.Remove(resultBox.Text.Length - 1); } if (resultBox.Text.Length == 0 || resultBox.Text == "-0" || resultBox.Text == "-") { resultBox.Text = "0"; } break; default: if (bs.IsOperator) { manageOperators(bs); } break; } } } lastButtonClicked = bs; }