Hi guys, I'm stucked at (as it seems to me) a very strange problem. I have a listview on my form to which a list of users is populated from mssql db. In the same table but different column I have user_id. I have a textbox and a textbox_keydown control to it.

When user enters a ID number in textbox, on keydown event in listview item is selected.

Listview MUST be locked because users must not change the selection-just view the selected item.

Now, on keydown event the item is 100% selected (msgbox gives me the selected item), but (here comes the problem) it is not visible/highlighted. At least not on any WinXP machine that I've tried. On several Win7 PCs works great, but on WinXP PC item is not visible.

this is parts of my code that I think is relavant:

Private Sub TextBox9_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox9.KeyDown

ListView1.Enabled = False

 'data reader reads from mssql

Me.ListView1.FindItemWithText(reader.Item("USER")).Selected = True
Me.ListView1.FindItemWithText(reader.Item("USER")).EnsureVisible()
ListView1.Select()

End Sub

On win7 machine, I get a grey background in listview (ofcourse-because is locked) and a nice blue color background for selected item. But in winXP the listview background is also gray, and selected item seems to be also because I can't seem to see it.

I'm would really appreciate some help guys.
tnx

do users ever need to change items in the listview?
If they never need to change anything in the listview then you could use something like this

    Private Sub ListView1_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.GotFocus
        some_other_object.focus()
    End Sub

this would prevent users from ever changing anything in the listview by changing focus to something else the moment the listview obtains focus.

Then there is no need to disable it to cause your problem.

Hi, I see what you mean, but the thing is that users must be able to change items in the listview in some cases. There are two cases here:

in 1st case users must select a person from listview and fill out the rest of the "document". This "document" contains also the listview selection (amongst other data) and is later saved in mssql and gets it's own ID number.

In other case users enter form ID in a textbox. Form loads the saved data for that "document" ID and also shows/selects the person which was selected in listview when creating this "document". And in this case user must not change this listview selection, he just must be able to see the selected item.

I hope I explained so you can understand what I'm trying to do.
tnx

There are two cases here:

in 1st case users must select a person from listview and fill out the rest of the "document". This "document" contains also the listview selection (amongst other data) and is later saved in mssql and gets it's own ID number.

In other case users enter form ID in a textbox. Form loads the saved data for that "document" ID and also shows/selects the person which was selected in listview when creating this "document". And in this case user must not change this listview selection, he just must be able to see the selected item.

Perhaps you can use an if-then-else or a select case statement to keep track of when listview may be edited and when it may not.

I'm not sure which would be easier for you, allowing listview editing in certain cases or disallowing it except for certain cases but you could try this.

    Private Sub ListView1_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.GotFocus

        '//the only time ListView may not be edited is when Form ID text box has content

        If Not form_ID_txtBox.text = "" Then '//if the Form ID text box is not blank
            some_other_object.focus() '//ListView1 cannot obtain focus
        End If
    End Sub

Hi Robert, for me is no difference - allowing listview editing in certain cases or disallowing it except for certain cases are the two solution but I think focusing from listview to another object is not a way to that solution. It is true if I focus somewhere else, on form load is ok. But user can stil simply just click listview item and fucuses it again. Listview must be disabled in such way users can't change the selection.

I really can't figure it out what's the trick on WinXP machines, like I said-on Win7 selected item is highlighted although listview is locked. I tried on several PCs, .net framework versions are the same...don't know where else to look...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.