[英]How to allow the user to only edit the current line and not the previous lines above in the textBox
Here is a picture of when I run the program.这是我运行程序时的图片。 I want the user to be able to only type on the current line.我希望用户只能在当前行上键入。 They shouldn't be able to edit the lines above.他们不应该能够编辑上面的行。
Here is the link to the github if you want to downlaod it to use it for yourself.如果您想下载它以供自己使用,这里是 github 的链接。 https://github.com/TeddyRoche/Calculator https://github.com/TeddyRoche/计算器
Here is the code that is controlling the whole program.这是控制整个程序的代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Calculator
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
float number_Initial = 0;
float prev_Answer = 0;
List<float> number_List = new List<float>();
List<string> equations = new List<string>();
bool valid = true;
int key_Press = 0;
int maxLines = 0;
string lastLine = "";
public MainWindow()
{
InitializeComponent();
}
private void NumPressedButton(int i)
{
if (valid == false)
{
MessageBox.Show("Please enter only numbers.");
displayText.Text = displayText.Text.Remove(displayText.Text.Length - 1);
}
else
{
if (key_Press < 12)
{
this.displayText.Text += i;
number_Initial = number_Initial * 10 + i;
key_Press++;
}
}
valid = true;
}
private void NumPressedKeyboard(int i)
{
if (valid == false)
{
MessageBox.Show("Please enter only numbers.");
displayText.Text = displayText.Text.Remove(displayText.Text.Length - 1);
}
else
{
if (key_Press < 12)
{
number_Initial = number_Initial * 10 + i;
key_Press++;
}
}
valid = true;
}
private void OutputSymbol(string x)
{
switch (x)
{
case "+":
equations.Add("add");
break;
case "-":
equations.Add("sub");
break;
case "*":
equations.Add("mul");
break;
case "/":
equations.Add("div");
break;
}
}
private void SymbolPressedButton(String x)
{
if (lastLine == "")
{
this.displayText.AppendText("Ans");
number_List.Add(prev_Answer);
this.displayText.AppendText(x);
OutputSymbol(x);
key_Press = key_Press + 4;
}
else
{
if (number_Initial == 0)
{
this.displayText.AppendText(x);
OutputSymbol(x);
key_Press++;
}
else
{
number_List.Add(number_Initial);
this.displayText.AppendText(x);
OutputSymbol(x);
number_Initial = 0;
key_Press++;
}
}
}
private void SymbolPressedKeyboard(String x)
{
if (lastLine == "")
{
this.displayText.AppendText("Ans");
number_List.Add(prev_Answer);
OutputSymbol(x);
key_Press = key_Press + 4;
}
else
{
if (number_Initial == 0)
{
OutputSymbol(x);
key_Press++;
}
else
{
number_List.Add(number_Initial);
OutputSymbol(x);
number_Initial = 0;
key_Press++;
}
}
}
//Display____________________________________________________________________________________________________________________________________________________
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
maxLines = displayText.LineCount;
if(maxLines > 0)
{
lastLine = displayText.GetLineText(maxLines - 1);
}
}
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
//check to see if what the user presses is a number or one of the appropriate symbols allowed
//if not sets valid to false
valid = true;
if(e.Key < Key.D0 || e.Key > Key.D9)
{
if(e.Key < Key.NumPad0 || e.Key > Key.NumPad9)
{
valid = false;
if (e.Key == Key.Add || e.Key == Key.Subtract || e.Key == Key.Multiply || e.Key == Key.Divide || e.Key == Key.Enter)
{
valid = true;
}
}
}
if(valid == true)
{
//Performing functions when a certain key is pressed
switch (e.Key)
{
case Key.NumPad0:
case Key.D0:
NumPressedKeyboard(0);
break;
case Key.NumPad1:
case Key.D1:
NumPressedKeyboard(1);
break;
case Key.NumPad2:
case Key.D2:
NumPressedKeyboard(2);
break;
case Key.NumPad3:
case Key.D3:
NumPressedKeyboard(3);
break;
case Key.NumPad4:
case Key.D4:
NumPressedKeyboard(4);
break;
case Key.NumPad5:
case Key.D5:
NumPressedKeyboard(5);
break;
case Key.NumPad6:
case Key.D6:
NumPressedKeyboard(6);
break;
case Key.NumPad7:
case Key.D7:
NumPressedKeyboard(7);
break;
case Key.NumPad8:
case Key.D8:
NumPressedKeyboard(8);
break;
case Key.NumPad9:
case Key.D9:
NumPressedKeyboard(9);
break;
case Key.Add:
SymbolPressedKeyboard("+");
break;
case Key.Subtract:
SymbolPressedKeyboard("-");
break;
case Key.Divide:
SymbolPressedKeyboard("/");
break;
case Key.Multiply:
SymbolPressedKeyboard("*");
break;
case Key.Enter:
Equals_Equation();
break;
}
}
else if(valid == false)
{
MessageBox.Show("Please enter only numbers.");
//displayText.Text = displayText.Text.Remove(displayText.Text.Length - 1);
}
}
//Display____________________________________________________________________________________________________________________________________________________
//Numbers ___________________________________________________________________________________________________________________________________________________
private void _0_Click(object sender, RoutedEventArgs e)
{
NumPressedButton(0);
}
private void _1_Click(object sender, RoutedEventArgs e)
{
NumPressedButton(1);
}
private void _2_Click(object sender, RoutedEventArgs e)
{
NumPressedButton(2);
}
private void _3_Click(object sender, RoutedEventArgs e)
{
NumPressedButton(3);
}
private void _4_Click(object sender, RoutedEventArgs e)
{
NumPressedButton(4);
}
private void _5_Click(object sender, RoutedEventArgs e)
{
NumPressedButton(5);
}
private void _6_Click(object sender, RoutedEventArgs e)
{
NumPressedButton(6);
}
private void _7_Click(object sender, RoutedEventArgs e)
{
NumPressedButton(7);
}
private void _8_Click(object sender, RoutedEventArgs e)
{
NumPressedButton(8);
}
private void _9_Click(object sender, RoutedEventArgs e)
{
NumPressedButton(9);
}
//Numbers____________________________________________________________________________________________________________________________________________________
//Equations__________________________________________________________________________________________________________________________________________________
private void Divide_Click(object sender, RoutedEventArgs e)
{
SymbolPressedButton("/");
}
private void Multiply_Click(object sender, RoutedEventArgs e)
{
SymbolPressedButton("*");
}
private void Subtract_Click(object sender, RoutedEventArgs e)
{
SymbolPressedButton("-");
}
private void Add__Click(object sender, RoutedEventArgs e)
{
SymbolPressedButton("+");
}
protected void Equals_Equation()
{
if(equations.Count != 0)
{
if(this.displayText.Text.StartsWith("1") || this.displayText.Text.StartsWith("2") || this.displayText.Text.StartsWith("3") || this.displayText.Text.StartsWith("4") || this.displayText.Text.StartsWith("5") || this.displayText.Text.StartsWith("6") || this.displayText.Text.StartsWith("7") || this.displayText.Text.StartsWith("8") || this.displayText.Text.StartsWith("9") || this.displayText.Text.StartsWith("0"))
{
number_List.Add(number_Initial);
this.displayText.AppendText("\n");
//loop that goes through the equations list and does the appropriate calculations
//Does Multiplication and Division first
for (int s = 0; s < equations.Count(); s++)
{
if (equations[s] == "mul")
{
number_List[s] = number_List[s] * number_List[s + 1];
}
else if (equations[s] == "div")
{
number_List[s] = number_List[s] / number_List[s + 1];
}
}
//Then does Addition and Subtraction next
for (int s = 0; s < equations.Count(); s++)
{
if (equations[s] == "add")
{
number_List[0] = number_List[0] + number_List[s + 1];
}
else if (equations[s] == "sub")
{
number_List[0] = number_List[0] - number_List[s + 1];
}
}
//changes the display to show the answer and creates a new line for the user to continue
this.displayText.Text += number_List[0];
number_Initial = number_List[0];
number_List.Clear();
prev_Answer = number_Initial;
//number_List.Add(number_Initial);
number_Initial = 0;
equations.Clear();
this.displayText.AppendText("\n");
this.displayText.PageDown();
displayText.Select(displayText.Text.Length, 0);
}
else if (this.displayText.Text.StartsWith("A"))
{
number_List.Insert(0, prev_Answer);
number_List.Add(number_Initial);
this.displayText.AppendText("\n");
//loop that goes through the equations list and does the appropriate calculations
//Does Multiplication and Division first
for (int s = 0; s < equations.Count(); s++)
{
if (equations[s] == "mul")
{
number_List[s] = number_List[s] * number_List[s + 1];
}
else if (equations[s] == "div")
{
number_List[s] = number_List[s] / number_List[s + 1];
}
}
//Then does Addition and Subtraction next
for (int s = 0; s < equations.Count(); s++)
{
if (equations[s] == "add")
{
number_List[0] = number_List[0] + number_List[s + 1];
}
else if (equations[s] == "sub")
{
number_List[0] = number_List[0] - number_List[s + 1];
}
}
//changes the display to show the answer and creates a new line for the user to continue
this.displayText.Text += number_List[0];
number_Initial = number_List[0];
number_List.Clear();
prev_Answer = number_Initial;
//number_List.Add(number_Initial);
number_Initial = 0;
equations.Clear();
this.displayText.AppendText("\n");
this.displayText.PageDown();
displayText.Select(displayText.Text.Length, 0);
}
}
else
{
}
key_Press = 0;
}
private void Equals_Click(object sender, RoutedEventArgs e)
{
Equals_Equation();
}
//Clears all stored data so the user can start from scratch
private void Clear_Click(object sender, RoutedEventArgs e)
{
number_List.Clear();
number_Initial = 0;
equations.Clear();
this.displayText.Clear();
key_Press = 0;
}
//Equations__________________________________________________________________________________________________________________________________________________
}
} }
I havent found much that has helped with only allowing the user to edit the current line.我还没有发现很多只允许用户编辑当前行的帮助。 I see a lot with not allowing the user to edit the whole textBox.我看到很多不允许用户编辑整个文本框的情况。
I want the user to only be able to edit the current line.我希望用户只能编辑当前行。 Right know I can use the arrow keys or click with my mouse on the previous lines and can edit them and I dont want them to be allowed to do this.正确知道我可以使用箭头键或用鼠标单击前面的行并可以编辑它们,但我不希望他们被允许这样做。
Here is a quick example.这是一个简单的例子。 Hopefully this gives you enough info.希望这能为您提供足够的信息。
XAML XAML
<TextBox
AcceptsReturn="True"
TextWrapping="Wrap"
VerticalScrollBarVisibility="Auto"
PreviewTextInput="TextBox_PreviewTextInput"/>
Code Behind代码隐藏
private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (sender is not TextBox box) //semi-redundant safety check
return;
//make sure there is text, also make sure there is a newline in the box
if (string.IsNullOrEmpty(box.Text) || (!box.Text.Contains('\n') && !box.Text.Contains('\r')))
return;
//through some testing I found the \r would be there without a \n so I include both for completeness
var lastReturn = Math.Max(box.Text.LastIndexOf('\r'), box.Text.LastIndexOf('\n'));
if (box.CaretIndex <= lastReturn)
e.Handled = true;
}
This solution can be expanded on, but it mainly prevents the Text from changing whenever Text is entered on any line but the last.该解决方案可以扩展,但它主要是防止在除最后一行之外的任何一行上输入文本时更改文本。 I like it as you can also move the Text Caret around to get standard functionality still (highlighting, selection, etc.)我喜欢它,因为您还可以四处移动文本插入符以获得标准功能(突出显示、选择等)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.