I am using a TextBox in a custom UserControl that I am creating. It seems that the default contextmenu doesn't show the shortcuts for Cut, Copy, Paste. This is fine, as long as they are just working.
But my Form using the UserControl has a MenuStrip that contains these default shortcuts as well. But the Cut, Copy, Paste commands are not working anymore, now that the shortcuts are assigned to the MenuStrip.
How can I use shortcuts at multiple positions in my forms? What is the best way to pass a global command like Cut and post it deeper into my UserControl? And is it possible to add the shortcuts to the default contextmenu of the textbox?
A MenuStrip item or ToolStrip item doesn't change the focus when it is clicked or its shortcut keystroke is pressed. Which is the ticket to implementing this functionality, the form's ActiveControl tells you which control has the focus. You just need to check if it is a TextBox. Like this:
private void copyToolStripMenuItem_Click(object sender, EventArgs e) {
var box = this.ActiveControl as TextBoxBase;
if (box != null) box.Copy();
}
Do the same for the Paste() and Cut() methods. You can further enhance the UI by selectively enabling these menu/toolbar items by subscribing to the Application.Idle event and checking if the ActiveControl is a text box and the text box or clipboard contains any text. Like this:
public Form1() {
InitializeComponent();
Application.Idle += Application_Idle;
}
protected override void OnFormClosed(FormClosedEventArgs e) {
Application.Idle -= Application_Idle;
base.OnFormClosed(e);
}
void Application_Idle(object sender, EventArgs e) {
var box = this.ActiveControl as TextBoxBase;
copyToolStripMenuItem.Enabled = box != null && box.Text.Length > 0;
cutToolStripMenuItem.Enabled = copyToolStripMenuItem.Enabled;
pasteToolStripMenuItem.Enabled = box != null && Clipboard.ContainsText();
}
You have to just enabled the property of textbox for the same and it will start responding.
Just verify that myTextBox.ShortcutsEnabled = TRUE;
Related
So I'm looking for a way to display some help when a key is pressed. I'm thinking the best option is ToolTip. But how can I get it so it shows instantly on KeyDown on a DataGridView? I have the ToolTip setup when KeyDownis pressed. However it doesn't show up for some reason. This is the code in my KeyDown event:
if (e.Control)
{
if(tt == null)
{
tt = new ToolTip();
tt.InitialDelay = 0;
tt.Active = true;
tt.Show("Help Test", dataGridView1.FindForm());
}
}
Yet nothing displays when I push down Ctrl.
You should set this.dataGridView1.ShowCellToolTips = false; using designer or using code, then you can show a manual ToolTip.
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if(e.Control)
toolTip1.Show("Some help", this.dataGridView1);
}
Note: You should dispose a ToolTip when the form disposes, so it's better to drop a ToolTip component from toolbox on form and use it. This way you don't need to dispose it manually yourself.
I have this piece of code for button in my Windows Phone 8.1 Store App project (not the Silverlight):
private void CursorRightButton_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(QueryTextBox.Text)) return;
QueryTextBox.Focus(FocusState.Keyboard); //also i tried FocusState.Pointer
QueryTextBox.Select((TextBox.SelectionStart + 1) % (TextBox.Text.Length + 1), 0);
}
As you can see, I am tring to move cursor to right in text programmatically and the problem is that it hides soft keyboard and then shows it again after tapping button. I need to have keyboard on while tapping this button.
I tried to tinker with Focus() methods for sender and TextBox objects but I couldn't find any possible solution.
So the question is, how do you force keyboard not to loose focus/not to hide while tapping on controls?
I found out with Sajeetharans help that I need to set IsTabStop value on controls to false. Then keyboard will stay there. I did it in constructor of my page like this
public MainPage()
{
InitializeComponent();
CursorLeftButton.IsTabStop = false;
CursorRightButton.IsTabStop = false;
}
and my button method
private void CursorRightButton_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(TextBox.Text)) return;
TextBox.Select((TextBox.SelectionStart + 1) % (TextBox.Text.Length + 1), 0);
}
Add a loaded event to your container say grid,
private void Grid_Loaded(object sender, RoutedEventArgs e)
{
this.IsTabStop = true;
set focus on the control , say a textblock
Txtblock1.Focus();
}
How To Programmatically Dismiss the SIP (keyboard)
I am developing a application using C#.
I have a window which has a label containing some text.
I want to copy as we copy something from anywhere.
But i cant copy of the label from the window.
How can i do that to copy the text of the label???
You will not be able to do this with a label.
You could try doing this with a textbox, to simulate the label and hightlight select.
TextBox.ReadOnly Property
Use the ReadOnly property to specify whether the contents of the
TextBox control can be changed. Setting this property to true will
prevent users from entering a value or changing the existing value.
and something like
TextBox1.Text = "Hello, Select Me";
TextBox1.ReadOnly = true;
TextBox1.BorderStyle = 0;
TextBox1.BackColor = this.BackColor;
TextBox1.TabStop = false;
Add a method to the label to make the label get focus if clicked:
private void label1_Click(object sender, EventArgs e)
{
label1.Focus();
}
Set the 'KeyPreview' property of the form to 'true' so it will process keys being pressed. I also added a method to handle the keydown event:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (label1.ContainsFocus && e.Control && e.KeyCode == Keys.C)
Clipboard.SetText(label1.Text);
}
This should work even if the "KeyPreview" property is false. This property is true if the form will receive all key events; false if the currently selected control on the form receives key events. The default is false
By default winforms label control doesn't support the feature of selecting text and copying. Instead you can add click event to the label and onclick give focus to the label. And in the form key press event check if label is focused and Ctrl+C is clicked then copy it to click board.
private void label1_Click(object sender, EventArgs e)
{
label1.Focus();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (label1.ContainsFocus && e.Control && e.KeyCode == Keys.C)
Clipboard.SetText(label1.Text);
}
I have an application with a Panel containing children Form objects. When I click one of the children Form it brings to front. I would like to know which one is in front now...
I've looked in event list but cant find proper event form my purpose :(
These methods doesn't work:
protected void OpenedFileForm_Enter(object sender, EventArgs e)
{
MessageBox.Show("enter");
}
protected void OpenedFileForm_Click(object sender, EventArgs e)
{
MessageBox.Show("click");
}
protected void OpenedFileForm_Activated(object sender, EventArgs e)
{
MessageBox.Show("activated");
}
protected void OpenedFileForm_MouseClick(object sender, MouseEventArgs e)
{
MessageBox.Show("mouse click");
}
protected void OpenedFileForm_Shown(object sender, EventArgs e)
{
MessageBox.Show("shown");
}
OpenFileDialog openFile1 = new OpenFileDialog();
openFile1.DefaultExt = "*.txt";
openFile1.Filter = "TXT Files|*.txt|RTF Files|*.rtf";
if (openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
openFile1.FileName.Length > 0)
{
switch (Path.GetExtension(openFile1.FileName))
{
case ".txt":
txtForm childTXT = new txtForm();
this.childForms.Add(childTXT);
childTXT.Parent = this.mainPanel;
childTXT.richTextBox1.LoadFile(openFile1.FileName, RichTextBoxStreamType.PlainText);
childTXT.Show();
break;
}
}
Have you tried the Form.Activated Event?
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.activated(v=vs.80).aspx
Edit:
If you are in an MDI application, you might need to use MdiChildActivate instead.
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.mdichildactivate.aspx
This code can only work when you set the Form.TopLevel property to false. Which makes it turn into a child control, almost indistinguishable from a UserControl.
This has many side-effects, for one there is no notion of "front" anymore. The Z-order of child controls is determined by their position in their parent's Controls collection. And it affects the events it fires, Activated and Deactivated will never fire. Furthermore, the Form class was designed to be a container control, it doesn't like taking the focus itself. Its child controls get the focus, the Form class doesn't have any use for focus. Which is why the Enter, Click and MouseClick events don't fire, they are events that require focus.
Long story short, what you are trying to do doesn't make a wholeheckofalot of sense. If it is strictly the Z-order you want to fix then write an event handler for the MouseDown event:
void OpenedFileForm_MouseDown(object sender, MouseEventArgs e) {
var frm = (Form)sender;
frm.BringToFront();
}
You could add frm.Select() to get the Enter event to fire, but only do that if the form doesn't contain any focusable controls itself. Do note that there is evidence that you don't assign the events correctly in your code. The Shown event does fire. It is also important that you set the FormBorderStyle to None, the title bar cannot indicate activation status anymore.
Ok, I got this! Thx for help everyone. You gave me a hint to think about equity of my strange MDI idea where Panel is parent for other Forms. I Removed SplitContainer containing Panel and just did standard MDI application, where Forms are MDIChildren of main Form.
childTXT.MdiParent = this;
I have a tree view on the left side. Selecting a node displays relevant information in a form on the right side.
Would I be able to keep the tree and any one control (textbox, combobox, checkbox) on the right in focus at the same time? This will enable a user to select a field, make a change, select another node, and without having to go back and select the same field again, just type and change the value of the same field.
Thanx.
EDIT
I suppose one could implement such behaviour manually:
private Control __cFocus;
private void {anyControl}_Focus(object sender, EventArgs e)
{
__cFocus = (Control)sender;
}
private void treeView1_AfterSelect(object sender, EventArgs e)
{
__cFocus.Focus();
}
I was just wondering if there exists an automatic / more elegant solution
EDIT 2
Ok, so it seems I'll have to implement it manually. Manual implementation it is then. However, now there seem to be another problem; not sure if I should ask this as a separate question.
When selecting a node the textbox gains focus as intended, but only when using the keyboard. It doesn't work when selecting a node with the mouse. First I thought that it might be a mouse event that's interfering, but stepping revealed that the MouseUp event fired first and then the AfterSelect event which sets the focus, so I don't think it's interfering. The textbox's Enter event is also fired, but for some reason it loses focus again to the tree.
Thanx
no, you cannot keep two controls in focus at the same time. But what you can do is set the focus to the target control in the treeview AfterSelect event
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
textBox1.Focus();
textBox1.SelectAll();
}
then in your textbox leave, save the changes, like so:
private void textBox1_Leave(object sender, EventArgs e)
{
//save changes here
}
this way, everytime you select an item in the treeview, check your textbox for change and save as needed, then you will refocus on the textbox for your next edit
There only can be one element having the focus!
But I have an idea for you that might solve your problem. Assuming you have a window with a TreeView and a TextBox. Set the HideSelection property of the TreeView to false and subscribe the AfterSelect event (like edeperson already answered) like this:
private void OnTreeViewAfterSelect(object sender, TreeViewEventArgs e)
{
textBox1.Text = e.Node.Text;
textBox1.Focus();
}
Then subscribe the KeyDown event of the TextBox and do following in the event method:
private void OnTextBoxKeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode == Keys.Up) || (e.KeyCode == Keys.Down))
{
treeView1.Focus();
SendKeys.Send(e.KeyCode == Keys.Up ? "{UP}" : "{DOWN}");
}
}
At last subscribe the Leave event of the TextBox and do following in the event method:
private void OnTextBoxLeave(object sender, EventArgs e)
{
if (treeView1.SelectedNode != null)
{
treeView1.SelectedNode.Text = textBox1.Text;
}
}
And, voilá it should work like you expected it...
If you want to focus on it , you can use usercontrol. you can put your textbox on usercontrol and set focus of this textbox on usercontrol using set properties on treeview select.
No you may not, only one control may be in focus at any given time.
See Moonlight's comment for one way to achieve the behavior that you seek.