Salve a tutti.
Sto giocando un pò con le WF di C# 2005 EE e mi son messo a fare un esercizio molto semplice.
La Form ha due TextBox (textBox1, textBox2) , una ComboBox (comboBox1) ed una Label (label1).
Il funzionamento è questo:
si scrive un numero su textBox1, uno su textBox2 ed in automatico, o selezionando l'operazione dalla comboBx, si effettua l'operazione.
Tutto sembra funzionare ma ho questi dubbi:
1 - come posso evitare la selezione e/o modifica delle items della ComboBox ?
2 - perchè l'evento textBox2_TextChanged non viene bruciato come il primo ?
3 - la comboBox1 l'ho popolata bene o ci sono pratiche migliori per aggiungere valori ? (collections, object list ... altro ?)
Graziecodice:using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form { private String[] comboData1 = new String[4]; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { comboData1[0] = "addizione"; comboData1[1] = "divisione"; comboData1[2] = "moltiplicazione"; comboData1[3] = "sottrazione"; comboBox1.DataSource = comboData1; } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e, String input1, String input2) { Double t1, t2; input1 = normalInput(input1); input2 = normalInput(input2); if (input1.Length > 0 && input2.Length > 0) { t1 = Convert.ToDouble(input1); t2 = Convert.ToDouble(input2); switch (comboBox1.SelectedValue.ToString()) { case "addizione": t1 = t1 + t2; break; case "divisione": t1 = t1 / t2; break; case "moltiplicazione": t1 = t1 * t2; break; case "sottrazione": t1 = t1 - t2; break; }; label1.Text = t1.ToString(); } else label1.Text = ""; } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { comboBox1_SelectedIndexChanged(sender, e, textBox1.Text, textBox2.Text); } private void textBox1_TextChanged(object sender, EventArgs e) { comboBox1_SelectedIndexChanged(sender, e, textBox1.Text, textBox2.Text); } private void textBox2_TextChanged(object sender, EventArgs e) { comboBox1_SelectedIndexChanged(sender, e, textBox1.Text, textBox2.Text); } private String normalInput(String input) { return System.Text.RegularExpressions.Regex.Replace(input, @"[^\d,]+", ""); } } }![]()


Rispondi quotando
