简体   繁体   English

如何在 wpf 中调用 KeyEventArgs 方法

[英]How to Call a KeyEventArgs Methods in wpf

Inside the listbox,if double click the listbox items, the values of the listbox items assign to the dynamic textbox within the listbox.(I created a dynamictextbox withint the listbox).Then I need to modify the textbox values.After that click enter key, the textbox values Added to the listbox items then dynamictextbox removed.when click the esc key the initial values added to listbox item.在列表框内,如果双击列表框项目,列表框项目的值分配给列表框内的动态文本框。(我在列表框内创建了一个动态文本框)。然后我需要修改文本框值。然后点击回车键, 文本框值添加到列表框项目然后动态文本框删除。当单击 esc 键时,初始值添加到列表框项目。

I have problem in inside the MouseEventArg method how can call the keyeventArgs method.我在 MouseEventArg 方法内部遇到问题,如何调用 keyeventArgs 方法。

C# C#

  System.Windows.Controls.TextBox dynamicTextBox = new System.Windows.Controls.TextBox();

    string previousvalue;

    private void items_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        //Get the index value
        var index = lstbxindex.Items.IndexOf(lstbxindex.SelectedItem);


        //set the textbox height and width property 
        dynamicTextBox.Width = 230;
        dynamicTextBox.Height = 50;

        //Add a textbox to the listbox 
        this.lstbxindex.Items.Add(dynamicTextBox);

        //To assign the selectedITem values to textbox
        dynamicTextBox.Text = lstbxindex.SelectedItem.ToString();

        //Get the textbox values before editing
        previousvalue = dynamicTextBox.Text;

        //Remove the values from the listbox item
        lstbxindex.Items.RemoveAt(index);


        dynamicTextBox.AcceptsReturn = true;

    }




private void checkenterclicked(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            EnterClicked(sender, e);
            //dynamicTextBox.PreviewKeyDown += EnterClicked;
        }
    }

    private void EnterClicked(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            using (DatabaseConnector databaseConnection = new DatabaseConnector(ApplicationConstants.ConnectionString))
            using (ProjectsTable projectsTable = new ProjectsTable(databaseConnection))
            {


                //Here Filter the Name from project Table which DbActive state is zero
                projectsTable.AddFilter(new Filter<ProjectsColumnTypes>(ProjectsColumnTypes.DbActive, CompareOperator.Equals, true));
                projectsTable.Read();

                projectsTable.AddFilter(new Filter<ProjectsColumnTypes>(ProjectsColumnTypes.Name, CompareOperator.Equals, dynamicTextBox.Text));

                projectsTable.Read();

                foreach (DtoProjectsRow row in projectsTable.Rows)
                {
                    //Guid DbId = row.DbId;
                    Guid DbId = row.DbId;

                    var UpdateRow = projectsTable.NewRow();


                    UpdateRow.Name = dynamicTextBox.Text;

                    UpdateRow.DbId = DbId;


                    UpdateRow.DbActive = true;

                    // Alter the row to the table.
                    projectsTable.AlterRow(UpdateRow);

                    // Write the new row to the database.
                    projectsTable.Post();

                    //Add the items in comboBox
                    lstbxindex.Items.Add(dynamicTextBox.Text);
                }
                // dynamicTextBox = e.Source as System.Windows.Controls.TextBox;

            }
        }
        else
        {
            if (e.KeyCode == Keys.Escape)
            {
                lstbxindex.Items.Add(previousvalue);
                lstbxindex.Items.Remove(dynamicTextBox);
            }
        }
    }

Can you not just do你能不能不只是做

System.Windows.Forms.KeyEventArgs ee = new System.Windows.Forms.KeyEventArgs();
ee.KeyCode = Keys.Enter;
EnterClicked(sender,ee);

Try this :尝试这个 :

Subscribe to your dynamic textbox keyevent.订阅您的动态文本框键事件。 Put that in the constructor.把它放在构造函数中。 For example :例如 :

System.Windows.Controls.TextBox dynamicTextBox = new System.Windows.Controls.TextBox();

string previousvalue;

    public MainWindows()
    {
        InitializeComponent();

        //subscribe to previewKeyDown, KeyDown will not work for enter key
        dynamicTextBox.PreviewKeyDown += dynamicTextBox_KeyDown;
    }

    // this will hit if any key is pressed
    void dynamicTextBox_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
             using (DatabaseConnector databaseConnection = new DatabaseConnector(ApplicationConstants.ConnectionString))
             using (ProjectsTable projectsTable = new ProjectsTable(databaseConnection))
            {


            //Here Filter the Name from project Table which DbActive state is zero
            projectsTable.AddFilter(new Filter<ProjectsColumnTypes>(ProjectsColumnTypes.DbActive, CompareOperator.Equals, true));
            projectsTable.Read();

            projectsTable.AddFilter(new Filter<ProjectsColumnTypes>(ProjectsColumnTypes.Name, CompareOperator.Equals, dynamicTextBox.Text));

            projectsTable.Read();

            foreach (DtoProjectsRow row in projectsTable.Rows)
            {
                //Guid DbId = row.DbId;
                Guid DbId = row.DbId;

                var UpdateRow = projectsTable.NewRow();


                UpdateRow.Name = dynamicTextBox.Text;

                UpdateRow.DbId = DbId;


                UpdateRow.DbActive = true;

                // Alter the row to the table.
                projectsTable.AlterRow(UpdateRow);

                // Write the new row to the database.
                projectsTable.Post();

                //Add the items in comboBox
                lstbxindex.Items.Add(dynamicTextBox.Text);
            }
            // dynamicTextBox = e.Source as System.Windows.Controls.TextBox;

        }
    }

private void items_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    //Get the index value
    var index = lstbxindex.Items.IndexOf(lstbxindex.SelectedItem);


    //set the textbox height and width property 
    dynamicTextBox.Width = 230;
    dynamicTextBox.Height = 50;

    //Add a textbox to the listbox 
    this.lstbxindex.Items.Add(dynamicTextBox);

    //To assign the selectedITem values to textbox
    dynamicTextBox.Text = lstbxindex.SelectedItem.ToString();

    //Get the textbox values before editing
    previousvalue = dynamicTextBox.Text;

    //Remove the values from the listbox item
    lstbxindex.Items.RemoveAt(index);


    dynamicTextBox.AcceptsReturn = true;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM