Click here to Skip to main content
15,886,077 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I'm trying to make a Form that will contain a TextBox into which I can write a value. All of this would be fine, but I have a problem with entering a value in the TextBox that does not appear in the List, the previous value would return to the TextBox.

What I have tried:

C#
  private void elementTextBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var elementString = elementTextBox.Text;
            elementTextBox_PreviewKeyDown(sender, e, elementString);//here is error Argument 2: cannot convert from 'System.Windows.Input.MouseButtonEventArgs' to 'System.Windows.Input.KeyEventArgs'	

        }
private async void elementTextBox_PreviewKeyDown(object sender, KeyEventArgs e,string element)
        {
            var myListOids = selectedOIDs.ToList();
            var myListOfElement = listOfElementNumber.ToList();
            DictionaryObjectId(myListOids, myListOfElement);
            var scaleString = scaleTextBox.Text;
            var scale = Int32.Parse(scaleString);
            var elementString = elementTextBox.Text;
            if (e.Key == Key.Return)
            {
                if (myListOfElement.Contains(elementString))
                {
                    var oidByElement = dictionaryOid.FirstOrDefault(pair => pair.Key == elementString).Value;
                    objectIdTextBox.Text = oidByElement.ToString();
                    await ScaleTask(scale, oidByElement);
                }
                else
                {
                    MessageBox.Show("Inadequate value!");
                    elementTextBox.Text = element;
                }
            }
        }


I have no idea how to pass that value, I thought (as it is in the code) that it could work when clicking into the field that it would take the value, but I don't know how to pass other values ​​to the method elementTextBox_PreviewKeyDown (object sender, KeyEventArgs e, string element )

I will be happy for any advice or idea.

David
Posted
Updated 7-Apr-22 20:03pm

1 solution

Use the KeyDown event instead of PreviewKeyDown, and if you don't want the box to change, set the KeyEventArgs.Handled Property[^] to true.
 
Share this answer
 
Comments
CPallini 8-Apr-22 3:00am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900