wpf 如何允许用户只编辑textBox中的当前行,而不编辑上面的前几行

flmtquvp  于 2023-01-06  发布在  其他
关注(0)|答案(1)|浏览(184)

这是我运行程序时的一张图片。我希望用户只能在当前行输入。他们不应该能够编辑上面的行。

如果你想下载github来自己使用,这里有一个github的链接。https://github.com/TeddyRoche/Calculator
下面是控制整个程序的代码。

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__________________________________________________________________________________________________________________________________________________

}

}
我还没有发现只允许用户编辑当前行有什么帮助,但我看到了很多不允许用户编辑整个文本框的方法。
我希望用户只能编辑当前行。正确的知道我可以使用箭头键或用鼠标单击前面的行,可以编辑它们,我不希望他们被允许这样做。

68bkxrlz

68bkxrlz1#

这里有一个简单的例子,希望能给你足够的信息。
XAML语言

<TextBox
  AcceptsReturn="True"
  TextWrapping="Wrap"
  VerticalScrollBarVisibility="Auto" 
  PreviewTextInput="TextBox_PreviewTextInput"/>

代码隐藏

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;
}

这个解决方案可以扩展,但它主要是防止文本的变化,每当文本输入的任何一行,但最后一行。我喜欢它,因为你也可以移动文本插入符号左右,以获得标准的功能仍然(突出显示,选择等)

相关问题