Можно же кнопку сделать и в ней прописать чтобы с двух строк данные считывались, в третью результат выводился
Да, чтобы результат сразу высвечивался в третьей строке, так надо --- Сообщение объединено с предыдущим 20 ноя 2019 Без неё нужно
Tea_Master, int a = Convet.ToInt32(TextBox1.Text); int aa = Convet.ToInt32(TextBox1.Text); string rez = (a+aa).ToString()TextBox3.Text = rez;
ты думаешь он знает про try catch? он не знает как суммировать читая с текстбокса, и кажись про эвенты тоже не знает --- Сообщение объединено с предыдущим 20 ноя 2019 А эвенты тогда зачем?
Как-то так: using System; using System.Text.RegularExpressions; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } void Sum(int? tb1, int? tb2) { textBox3.Text = (tb1 + tb2).ToString(); } private void textBox1_TextChanged(object sender, EventArgs e) { if (Regex.IsMatch(textBox1.Text, "[^0-9]")) { textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1); textBox1.SelectionStart = textBox1.TextLength; } else { Sum(Convert.ToInt32((textBox1.Text != "" ? textBox1.Text : "0")), Convert.ToInt32((textBox2.Text != "" ? textBox2.Text : "0"))); } } private void textBox2_TextChanged(object sender, EventArgs e) { if (Regex.IsMatch(textBox2.Text, "[^0-9]")) { textBox2.Text = textBox2.Text.Remove(textBox2.Text.Length - 1); textBox2.SelectionStart = textBox2.TextLength; } else { Sum(Convert.ToInt32((textBox1.Text != "" ? textBox1.Text : "0")), Convert.ToInt32((textBox2.Text != "" ? textBox2.Text : "0"))); } } } } Код using System; using System.Text.RegularExpressions; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } void Sum(int? tb1, int? tb2) { textBox3.Text = (tb1 + tb2).ToString(); } private void textBox1_TextChanged(object sender, EventArgs e) { if (Regex.IsMatch(textBox1.Text, "[^0-9]")) { textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1); textBox1.SelectionStart = textBox1.TextLength; } else { Sum(Convert.ToInt32((textBox1.Text != "" ? textBox1.Text : "0")), Convert.ToInt32((textBox2.Text != "" ? textBox2.Text : "0"))); } } private void textBox2_TextChanged(object sender, EventArgs e) { if (Regex.IsMatch(textBox2.Text, "[^0-9]")) { textBox2.Text = textBox2.Text.Remove(textBox2.Text.Length - 1); textBox2.SelectionStart = textBox2.TextLength; } else { Sum(Convert.ToInt32((textBox1.Text != "" ? textBox1.Text : "0")), Convert.ToInt32((textBox2.Text != "" ? textBox2.Text : "0"))); } } } }
Ты мог в Sum написать void Sum() { if (int.TryParse(textBox1.Text, out int a) && int.TryParse(textBox2.Text, out int b)) { textBox3.Text = (a+b).ToString() }} Код void Sum() { if (int.TryParse(textBox1.Text, out int a) && int.TryParse(textBox2.Text, out int b)) { textBox3.Text = (a+b).ToString() }} и в TextChanged вызывать Sum, и не нужны все эти регулярки и тд. Но попытка хорошая)
Ныа: 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; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void textBox1_TextChanged(object sender, EventArgs e) { if (textBox2.Text.Trim() != "") { try { int a = Convert.ToInt32(textBox1.Text); int b = Convert.ToInt32(textBox2.Text); int sum = a + b; // Сама операция textBox3.Text = sum.ToString(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } private void textBox2_TextChanged(object sender, EventArgs e) { if (textBox1.Text.Trim() != "") { try { int a = Convert.ToInt32(textBox1.Text); int b = Convert.ToInt32(textBox2.Text); int sum = a + b; // Сама операция textBox3.Text = sum.ToString(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } } } Код 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; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void textBox1_TextChanged(object sender, EventArgs e) { if (textBox2.Text.Trim() != "") { try { int a = Convert.ToInt32(textBox1.Text); int b = Convert.ToInt32(textBox2.Text); int sum = a + b; // Сама операция textBox3.Text = sum.ToString(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } private void textBox2_TextChanged(object sender, EventArgs e) { if (textBox1.Text.Trim() != "") { try { int a = Convert.ToInt32(textBox1.Text); int b = Convert.ToInt32(textBox2.Text); int sum = a + b; // Сама операция textBox3.Text = sum.ToString(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } } } --- Сообщение объединено с предыдущим 3 дек 2019 за 5 сек тупа натаскал кода --- Сообщение объединено с предыдущим 3 дек 2019 делал такие калькуляторы еще год назад))) --- Сообщение объединено с предыдущим 3 дек 2019 Советую убрать MessageBox в обработчике ошибок, ибо при стирании операторов будет выскакивать MessageBox, что не очень приятно. --- Сообщение объединено с предыдущим 3 дек 2019 Вот новый код: 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; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void textBox1_TextChanged(object sender, EventArgs e) { if (textBox2.Text.Trim() != "") { try { int a = Convert.ToInt32(textBox1.Text); int b = Convert.ToInt32(textBox2.Text); int sum = a + b; // Сама операция textBox3.Text = sum.ToString(); } catch (Exception) { textBox3.Text = ""; } } } private void textBox2_TextChanged(object sender, EventArgs e) { if (textBox1.Text.Trim() != "") { try { int a = Convert.ToInt32(textBox1.Text); int b = Convert.ToInt32(textBox2.Text); int sum = a + b; // Сама операция textBox3.Text = sum.ToString(); } catch (Exception) { textBox3.Text = ""; } } } } } Код 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; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void textBox1_TextChanged(object sender, EventArgs e) { if (textBox2.Text.Trim() != "") { try { int a = Convert.ToInt32(textBox1.Text); int b = Convert.ToInt32(textBox2.Text); int sum = a + b; // Сама операция textBox3.Text = sum.ToString(); } catch (Exception) { textBox3.Text = ""; } } } private void textBox2_TextChanged(object sender, EventArgs e) { if (textBox1.Text.Trim() != "") { try { int a = Convert.ToInt32(textBox1.Text); int b = Convert.ToInt32(textBox2.Text); int sum = a + b; // Сама операция textBox3.Text = sum.ToString(); } catch (Exception) { textBox3.Text = ""; } } } } }
FormatC, зачем плодить 2 одинаковых кода? не проще тогда у второго textbox подписаться на уже существующий метод а не создавать новый и туда пихать тот же код?