Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a textbox and a datagridview with some results. I am looking for a way in which if the user is typing something in the textbox and in between, he uses his mouse and clicks on the datagridview, the focus should not leave the textbox means it should stay in the textbox. Now, the evil part of the problem is datagridview also has a cellcontentdoubleclickevent which I want to behave normally. This means that the cursor should be in textbox and if the user double clicks on the cell of datagridview, it can execute.

What I have tried:

C#
private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
      {
          if (dataGridView1.Rows.Count > 0) // datagridview must
                                               // have anything
          {
              dataGridView1.Visible = false;
              textBox4.Text =
              dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
              dataView.RowFilter = null; // resets the filter to
                                         // its starting state
              sale_purchase_datagridview1.Focus();
          }
      }
Posted
Updated 6-Nov-23 4:03am
v2
Comments
PIEBALDconsult 25-Oct-23 10:05am    
Maybe re-think your life choices.
Ankit Goel 25-Oct-23 10:08am    
That's exactly what i was waiting for . A nonsense comment. thanks
PIEBALDconsult 25-Oct-23 14:49pm    
Glad to be of service.
Member 15627495 25-Oct-23 11:49am    
you have to catch the event click and the tab option of your componants.

because those two are access to interact and get focus on your componants.

on a first time, you will filter { is the click or focus is on textbox, or datagrid }
then re-focus to the texbox, or keep the focus on the datagrid.

look close at Form event for click, you have a global case dedicated.
[no name] 25-Oct-23 14:51pm    
That's a "mode"; and users have to be able to "escape" a mode ... without smashing the keyboard.

1 solution

Quote:
That's exactly what i was waiting for . A nonsense comment. thanks
Not really nonsense though is it?

As Maciej says in his comment, it's "unfriendly to the UI" and Gerry says "without smashing the keyboard". At the very least it's counter-intuitive behaviours from your UI.

Try to see it from a Users perspective - I'm typing away but will suddenly double-click something else (i.e. remove my hand from the keyboard to grasp my mouse), then what? Carry on typing in the original text box?

What if I started typing in the textbox but it's now the EOD and I want to leave the program entirely. Can't because I'm still stuck in that text box.

It sounds like you need to do something in the Validate event for the text box, but watch out for that last scenario - not insurmountable see Allow Form to close when invalid data is present[^]
 
Share this answer
 
Comments
Maciej Los 8-Nov-23 8:44am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900