TextBox receiving focus when clicking on ListView - c#

I'm struggling to make a custom autocomplete style control for my application using a list view. In one form, the custom control is working as expected, however in another, when clicking on the ListView, rather than the ListViewItem being selected, the first TextBox in the form is selected instead.
Rather than clog up the question with a wall of code, here is the code for the AutoCompleteListView.
Here is an image of the form. Items are redacted due to data protection:
Also, here is a .gif showing the behaviour of the form. What I'm doing in the aformentioned gif is attempting to click on the first item within the ListView, however, as I do that, focus changes to the first text box in the form. This same behaviour occurs when clicking on any item, in any of the 3 text boxes.
I have attempted to trace the events using RuntimeFlow to watch as events fire, but it has not provided any clarity as to what is actually happening. If anyone could point me in the right direction, that'd be greatly appreciated.

My first guess is in ItemClick you are setting visibility to false, which means something else in the form needs focus, ie the textbox. So do a textbox.focus() in ItemClick to the desired textbox. And given that you will not have access to the parent form in this class, I recommend you make an OnClose event in the AutoCompleteListView and subscribe to it in the parent form, to set the focus to somewhere.

Related

Listbox text disappears when form loses focus, comes back when we clicked on again

I am sorry in advance that I am providing no code. The reason is simply the fact that I don't know what part of code to put here to be helpful. But here is the problem:
I have a Listbox on my form. When the form starts, the listbox will be populated by new items, until the process is finished. There aren't many, say, 10 lines.
All is fine, except when the users click somewhere else on their desktop and the form loses focus. That is when the text inside the listbox vanishes. And it comes back once we click on the form again! I have not manipulated the drawitem event of the listbox. Any ideas why this happens?
Update: I realized this also happens when I click on the listbox itself! I would also like to add that I have a label over my listbox control that shows the percentage.

WPF: How do I handle mouse click on a Listview inside of a Button?

I have a listview for displaying a table inside of a button. I have a click event assigned to the button, but when the user clicks on the list view, a row is selected on the list view and the mouse click is never bubbled up to the button.
I'm stuck at this point, and need a way to solve this. All the examples I've seen online are for placing a button inside the listview. How do I make this work?
Set the IsHitTestVisible property to false on your ListView. Since it is a control that normally processes click events, doing this will have it ignore them.
If you don't want the items to be selectable in the first place, you could make the List View unable to receive Focus. Just set the Focusable property to false.
With the List View unable to receive focus, then I would expect the mouse event to bubble up to the button.

Getting handle of Autocomplete dropdown box of textbox in winforms

I wanted to adjust the width of the Autocomplete dropdown box of a textbox. I dont want to adjust the width of that textbox, but only Autocomplete dropdown. I know that there is no way I can increase the width of the Autocomplete dropdown by using properties provided with textbox.
Hence I wanted to know whether there is any way to get the handle of that Autocomplete box and then increase the width of that drop downlist without changing the textbox width?
If this is not possible then I would like to create my custom textbox with autocomplete, in this case how to use the existing autocomplete functionality provided by microsoft? Is there any way to do it. Are there any libraries available for this?
I don't think you can use Microsoft's implementation of autocomplete, which does not have an option to adjust dropdown width.
Create a background thread to not get into the way of typing, and hook up the text change event of a combobox box or a textbox to update the candidate list (assuming autosuggest mode since you mention a dropdown). You can probably add/remove the combobox items on the fly if you have a combobox. But for dropdown list and textbox items you need a popup window
It is easy to get a popup to show, but you need to not use a fixed position so it won't go off the screen when the textbox is close to the edge of the screen. And the focus logic is a little bit complex. you need to keep focus on the textbox unless the user press the arrow keys to make a selection.
so
when focus is on textbox:
arrow keys move the focus to the popup
other keys goes to the textbox, if not handled by the dialog itself, except for the delete key when mouse is over the popup.
when focus is on popup:
arrow keys move the focus to the sibling candidate item or the textbox
other keys goes to the textbox, if not handled by the dialog itself, except for the delete key
mouse clicks:
dismiss the popup outside of the popup or the popup.
update the value of textbox if a candidate item in the popup is clicked on
It takes a lot of effort to get the focus/threading right. If you can afford some form space, you can just add a fixed width listbox to the form instead, like Visual Studio help viewer's index pane.
After going through lot of blog posts and different articles, I came to a consensus that it is next to impossible to get a solution to my problem in the way I wanted. So I've decided either to come up with a custom solution or as Sheng Jiang said I need to implement my own autocomplete object.
I've come up with a solution which fits my requirement by increasing the width of the textbox as per the largest string in the autocomplete string list while I'm adding the autocomplete custom source. As I said I cant increase the width of the text box because of the size constraint on the form, so I decided to keep this textbox in a panel and increase the size of the textbox inside that. Panel will not grow with the textbox so that solved my problem.
I know this is not perfect solution but it fits my requirement.

C# PropertyGrid DoubleClick event capture

For a project I am working on, I have some values in a PropertyGrid that are many lines long. I want the user to be able to double-click a value in the grid, which will bring up my custom UI window. I already have it set up so that the UI works when the [...] button is clicked, but I also need it to show up directly if any grid item is double-clicked.
On a side note, looping through the controls of the PropertyGrid and assigning Click/Mouse events to them allows this to happen when the property name is clicked, but still does not capture the click when the property value is clicked. Any suggestions?

SplitContainer.Panel1 Selection to Changes SplitContainer.Panel2 selection

The MSDN documentation for the SplitContainer controls says:
With the SplitContainer control, you can create complex user interfaces;
often, a selection in one panel
determines what objects are shown in
the other panel.
So, I have a SplitContainer that is split horizontally having top and bottom panels.
What I would like to do is -- when a control within Panel1 (on top) receives focus, I need a control within Panel2 (bottom) to be focused. And then the focus must go back to the control in Panel1 again.
The problem is, the control in Panel2 returns false on CanFocus. I understand that for some kind of controls this can happen, and that every ancestor of the control must have focus for it to receive focus. I tried that too, but to no avail. The CanFocus still returns false.
Is there any alternative solution to this? Has anyone tried this. What I am trying to do is essentially the same as what Outlook does. When you select a message in your Inbox. The message body shows up in the bottom panel.
What you're describing doesn't mimic Outlook. If I select a message in the message list in Outlook, focus remains with the message list. Yes, the message body does appear below, but the focus doesn't move.
There shouldn't be anything preventing you from changing either the control or the content that is displayed in either panel. You shouldn't have to move focus to accomplish that.
Do you really need to set focus to Panel2?
I'm guessing your Panel2 contains something like a RichTextBox object, say called RTB1 for simplicity.
Say someone selects something in Panel1. For simplicity, let's say you have a ListView control called LV1.
Then, on the LV1.SelectedIndexChanged event, read that event and fill your data to RTB1.
You don't really need to give Panel2 focus, unless there is something I'm missing.

Categories