Question How to allow scrolling in a panel and at the same time allow text changing in a TB

kfirba

Well-known member
Joined
Dec 29, 2012
Messages
77
Programming Experience
1-3
Hello!

I got a program for movies rental.

In the program, when you start typing something in the search box, I used a Panel to hold the SQL results of the movies name. Everything works perfect.
There is one slightly problem.

If I want to allow the user to scroll with the mouse wheel in the panel that shows the results, I have to set the focus to that panel. But when I'm setting the focus to that panel, I lose my TextBox writing ability and I can't continue writing in the text box.

Let's say I have the following movies:
  1. Dad
  2. Doodle
  3. Daddie

When I type "D" in the TextBox, It's gonna show me all of the 3 movies. If i want to let the user scroll with the mouse wheel, I will have to set the focus to the Search Result Panel, and then, I can't keep writing in the textbox.

How can I let the user use the mouse wheel to scroll and at the same time, keep him in the TextBox so he can keep writing?

Thanks!
 

IanRyder

Well-known member
Joined
Sep 9, 2012
Messages
1,130
Location
Healing, NE Lincs, UK
Programming Experience
10+
Hi,

This can be achieved by setting the Form's KeyPreview property to True. You can then set the Focus to the Panel and then code the KeyDown and KeyPress events of the Form to capture your keyboard input. This input can then be assigned to your TextBox which, in turn, fires your TextChanged event to return your SQL results. Have a look at this example:-

VB.NET:
Public Class Form1
  Dim ValidChar As Boolean
 
  Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    If e.KeyCode = Keys.Back Then
      TextBox1.Text = TextBox1.Text.Substring(0, TextBox1.Text.Length - 1)
      ValidChar = False
    Else
      ValidChar = True
    End If
  End Sub
 
  Private Sub Form1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
    If ValidChar Then
      TextBox1.Text = TextBox1.Text & e.KeyChar.ToString
    End If
  End Sub
 
  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Panel1.Select()
  End Sub
End Class

Hope that helps.

Cheers,

Ian

NB, I do not have a Mouse Wheel so have not tested this but, in theory, this should work.
 

kfirba

Well-known member
Joined
Dec 29, 2012
Messages
77
Programming Experience
1-3
Thanks!
There is a slightly problem. It seems like when I press a key it double add it to the text box. I will find a way to solve that. I just needed an idea, and you gave me the idea.
Thanks!
 

kfirba

Well-known member
Joined
Dec 29, 2012
Messages
77
Programming Experience
1-3
Oh, I see.
But, what I'm doing is that whenever the user enter the search box, the focus set to the panel. I will find a way to make it work ;)
Thanks a lot!
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,583
Location
Norway
Programming Experience
10+
ActiveControl property may help you with this.
 
Top Bottom