question

69159764 avatar image
0 Votes"
69159764 asked 69159764 commented

[WPF TextBox] When the context menu is displayed, the selected character string of TextBox is canceled.

I have a question about WPF TextBox.
Right-clicking with a character selected in the TextBox opens a standard context menu.
However, if you right-click outside the selected string, the string will be deselected.
I don't want to clear the selected string when I open the context menu like the address bar in Explorer.
(Right-click on the selected string to open the context menu as desired.)

The same result was achieved by opening my own context menu within the TextBox.
Is there a good solution?

windows-wpf
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I do not know what Explorer is. Do you mean Internet Explorer or Windows Explorer or Files Explorer or is Explorer the name of you application? I do not understand how Explorer is relevant, unless it is the name of you application.

0 Votes 0 ·

The "Explorer" I mentioned refers to the "File Explorer".
In my native Japanese, it was just "Explorer", so I'm sorry to confuse you.

0 Votes 0 ·
Castorix31 avatar image
0 Votes"
Castorix31 answered 69159764 commented

You can handle OnContextMenuOpening.
Something like :

 public partial class MyTextBox : TextBox
 {
     protected override void OnContextMenuOpening(ContextMenuEventArgs e)
     {
         int nSelStart = this.SelectionStart;
         int nSelLength = this.SelectionLength;
         base.OnContextMenuOpening(e);           
         this.Select(nSelStart, nSelLength);
     }
 }  

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Great solution.
Thanks to that, my worries have been resolved.
Thank you very much.

0 Votes 0 ·
HuiLiu-MSFT avatar image
0 Votes"
HuiLiu-MSFT answered 69159764 commented

You could try to add con.focusable = false; so that the string is still selected when you right-click to display the context menu.
The code of xaml:

 <Grid>
         <TextBox x:Name="myText" Height="38" Margin="0,72,6,0" VerticalAlignment="Top" HorizontalAlignment="Right" Width="135"></TextBox>
         <Label Height="30" Margin="12,80,164,0" Name="label1" VerticalAlignment="Top">Textbox with contextMenu property set</Label>
 </Grid>

The code of xaml.cs:

 public partial class MainWindow : Window
   {
     ContextMenu con = new ContextMenu();
     public MainWindow()
     { 
       InitializeComponent();
       MenuItem menuItem1 = new MenuItem();
       menuItem1.Header = "Menu1";
       MenuItem menuItem2 = new MenuItem();
       menuItem2.Header = "Menu2";
       con.Items.Add(menuItem1);
       con.Items.Add(menuItem2);
       con.Focusable = false;
       this.myText.ContextMenu = con;
     }
   }

The picture of result:
129461-2.gif


If the response is helpful, please click "Accept Answer" and upvote it.
 Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread. 


2.gif (57.1 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


To reproduce the problem, I think that you must select a part of the text and then click a non-selected part. For similar functionality, see Notepad, for example.

0 Votes 0 ·

I suspect that the problem is that the text appears to be not selected when the focus goes elsewhere but the text (I suspect) in fact remains selected.

I wish we had answers to the questions I asked in my comment. I would prefer getting clarification before speculating on an answer. I think the answer is to change the property that causes the selection to appear to be unselected. I forget what that property is but I will find it if it seems relevant.

0 Votes 0 ·

If you display the context menu from an unselected location instead of the selected location, as others will follow you, it will be deselected.
I am still looking for a solution.

0 Votes 0 ·