[英]How to create a KeyEventArgs object in WPF ( related to a SO answer )
[英]How to Call a KeyEventArgs Methods in wpf
在列表框內,如果雙擊列表框項目,列表框項目的值分配給列表框內的動態文本框。(我在列表框內創建了一個動態文本框)。然后我需要修改文本框值。然后點擊回車鍵, 文本框值添加到列表框項目然后動態文本框刪除。當單擊 esc 鍵時,初始值添加到列表框項目。
我在 MouseEventArg 方法內部遇到問題,如何調用 keyeventArgs 方法。
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);
}
}
}
你能不能不只是做
System.Windows.Forms.KeyEventArgs ee = new System.Windows.Forms.KeyEventArgs();
ee.KeyCode = Keys.Enter;
EnterClicked(sender,ee);
嘗試這個 :
訂閱您的動態文本框鍵事件。 把它放在構造函數中。 例如 :
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.