Hi guys, I'm trying to get the focused textbox so I can insert a time stamp, the code below will put a timestamp into textBox1 but I dont want to define the textbox so i'm looking for something like "focused.text" instead of "textBox1.text".
Can anyone help with this
Thanks.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
switch (keyData)
{
case Keys.Control | Keys.T:
var insertText = DateTime.Now.ToString();
var selectionIndex = textBox1.SelectionStart;
textBox1.Text = textBox1.Text.Insert(selectionIndex, insertText);
textBox1.SelectionStart = selectionIndex + insertText.Length;
return true;
}
return false;
}
Loading
cory thompsonPosted Jan 10, 2012, 2:56 PM
Sam HobbsPosted Jan 10, 2012, 1:10 AM
I do not know the details of what you need to do. Overriding ProcessCmdKey is probably more complicated than you need to do. A more object-oriented solution is probably easier to understand and implement.
You could add a handler to each TextBox for a keyboared event such as KeyPress. Or you could handle the TextChanged event. If you do that for each TextBox then the handler will automatically know what TextBox it is for.
Or you could create a new class derived from TextBox in which you handle one of those events. The event handler will get a "sender" parameter that will identify what control that the event is for.
If you can be more specific about what you need to do then I can be more specific about how.