Posted 12 April 2023, 6:15 am EST
Hi,
how can I enabled that the user can cut the text from the ComboBox?
Regards
Forums Home / ComponentOne / WPF Edition
Posted by: patrick.muellner on 12 April 2023, 6:15 am EST
Posted 12 April 2023, 6:15 am EST
Hi,
how can I enabled that the user can cut the text from the ComboBox?
Regards
Posted 12 April 2023, 4:28 pm EST - Updated 12 April 2023, 4:33 pm EST
Hi Patrick,
C1ComboBox has TextBox in its template and we can easily cut the text from it. Please refer the attached gif for the same:
Please refer the sample too: ComboBoxSample.zip
If you have a different requirement or we understood it wrong, then please explain in detail for your requirement. We’ll be happy to resolve your issue.
Regards,
Nitin
Posted 12 April 2023, 8:38 pm EST
Hi,
sorry - but if I click the right mouse button I can cut the whole text.
Regards
Posted 13 April 2023, 5:12 pm EST
Hi Patrick,
Are you able to achieve your requirement? If not then, please describe your requirement in detail. So, that we can assist you accordingly.
Or if you have another issue related to this thread, then you can ask here. Or you can generate a new forum thread.
Regards,
Nitin
Posted 13 April 2023, 5:48 pm EST
HI,
I include you a video.
On the ComboBox IsEditable is set to “False”. But if I do a mouse right click I can cut the text. But this shouldn’t be able.
Regards
Posted 13 April 2023, 11:07 pm EST
Hi Patrick,
Apologize for any inconvenience.
JFYI, We have TextBox in our C1ComboBox template. And the ContextMenu appears after right-click is the default context menu for that TextBox, which is not linked with the C1ComboBox i.e., we didn’t added this context-menu to the ComboBox this is the default one.
You can also check with only TextBox. The same ContextMenu will appear after right-click.
To achieve your requirement, you can create a custom ContextMenu for TextBox under C1ComboBox by handling Loaded event of C1ComboBox as:
private void ComboBox_Loaded(object sender, RoutedEventArgs e)
{
var comboHeader = cmb.Template.FindName("ComboHeader", cmb) as C1TextEditableContentControl;
var textBox = comboHeader.Template.FindName("EditControl", comboHeader) as C1TextBoxBase;
textBox.ContextMenu = CustomContextMenu(textBox);
}
private ContextMenu CustomContextMenu(C1TextBoxBase textBox)
{
var contextMenu = new ContextMenu();
//Cut
var cutMenu = new MenuItem() { Header = "Cut" };
cutMenu.Click += (s1, e1) =>
{
Clipboard.SetText(textBox.SelectedText);
textBox.SelectedText = "";
};
contextMenu.Items.Add(cutMenu);
//Copy
var copyMenu = new MenuItem() { Header = "Copy" };
copyMenu.Click += (s1, e1) =>
{
Clipboard.SetText(textBox.SelectedText);
};
contextMenu.Items.Add(copyMenu);
//Paste
var pasteMenu = new MenuItem() { Header = "Paste" };
pasteMenu.Click += (s1, e1) =>
{
textBox.SelectedText = Clipboard.GetText();
};
contextMenu.Items.Add(pasteMenu);
//Disabled Menu
contextMenu.Opened += (s1, e1) =>
{
if (cmb.IsEditable == false | textBox.SelectedText.Length == 0)
cutMenu.IsEnabled = false;
};
return contextMenu;
}
Please refer the attached sample for the same: ComboBoxSample_Mod.zip
Best regards,
Nitin
Posted 16 April 2023, 5:09 pm EST
Hi,
thanks for the code.
One problem still exists. If I want to cut with the keyboard it still works.
How can I solve this problem?
Regards
Posted 16 April 2023, 6:32 pm EST
Hi Patrick,
Thanks for reaching out to us again.
You can disable Cut from keyboard if IsEditable=false, by handling PreviewKeyDown as:
cmb.PreviewKeyDown += (s, e) =>
{
if(!cmb.IsEditable)
{
if (e.Key == Key.Back)
e.Handled = true;
if (e.KeyboardDevice.Modifiers == ModifierKeys.Control & e.Key == Key.X)
e.Handled = true;
}
};
Please refer the attached modified sample: ComboBoxSample_Mod2.zip
Best Regards,
Nitin