private void textBox_KeyDown(object sender, KeyEventArgs e) { if ((e.Control || e.Shift) && e.KeyCode == Keys.Return) { double d = 0; //現在のカーソル位置のテキストを計算する int count = 0; int selectionLine = 0; for (int i = 0; i < textBox.Lines.Length; i++) { count += textBox.Lines[i].Length + 2; if (count > textBox.SelectionStart) { selectionLine = i; break; } } string[] formula = new string[selectionLine + 1]; Array.Copy(textBox.Lines, formula, selectionLine + 1); d = NumericalFormula.GetNumetricValue(formula); if (!double.IsNaN(d)) { skipTextChangeEvent = true; this.numericalValue = d; if (textBox.Multiline) { if (textBox.Text.IndexOf("\r\n", textBox.SelectionStart) >= 0) { textBox.Text = textBox.Text.Remove(textBox.Text.IndexOf("\r\n", textBox.SelectionStart)); } textBox.Text += "\r\n" + GetString(); } else { textBox.Text = GetString(); } this.numericalValue = d; if (ValueChanged != null) { ValueChanged(this, e); } skipTextChangeEvent = false; textBox.SelectionStart = textBox.Text.Length; } } }
private bool renewValue = true; //イベントはキャンセルしないが、numericalValueは変更しない private void textBox_TextChanged(object sender, EventArgs e) { if (skipTextChangeEvent) { return; } try { int count = 0; int selectionLine = 0; for (int i = 0; i < textBox.Lines.Length; i++) { count += textBox.Lines[i].Length + 2; if (count > textBox.SelectionStart) { selectionLine = i; break; } } string[] formula = new string[selectionLine + 1]; Array.Copy(textBox.Lines, formula, selectionLine + 1); double d = NumericalFormula.GetNumetricValue(formula); if (!double.IsNaN(d)) { if (d != this.numericalValue) { if (renewValue) { this.numericalValue = d;//NumericalValueを外部から変更された時は値を変更しない } if (ValueChanged != null) { ValueChanged(this, e); } } } } catch { } }