ListBox and Keyboard Control

Problem with SelectedIndex & Keyboard Relationship

Page 1 of 1

3 Replies - 3438 Views - Last Post: 29 March 2009 - 06:51 AM Rate Topic: -----

#1 FerdT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-March 09

ListBox and Keyboard Control

Posted 27 March 2009 - 02:11 AM

Hi

I have a ListBox Control containing 5 items. This control displays the current condition and allows the condition to change. The ListBox.SelectedIndex is calculated from the current condition (normally item 2 in the list) and displayed. If the user requires to change this condition the Keyboard Up/Down would be used for navigation (The App is very keyboard intensive), however if the ListBox.SelectedIndex is set at 2 and the keyboard down arrow is pressed the selected item displayed changes to 0 not 3 as required. Is there a way to synchronise the keyboard control with the current selected index?

Not sure if it makes a difference but the App is WPF

Thanks in Advance

Is This A Good Question/Topic? 0
  • +

Replies To: ListBox and Keyboard Control

#2 krum110487  Icon User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 291
  • Joined: 07-February 09

Re: ListBox and Keyboard Control

Posted 27 March 2009 - 06:07 AM

if we have some code, we may be able to help you with the error!

please paste some code within [code] Code here! [/code] brackets

I am not sure what methods you are using to do the keyboard control, but I would have something like...

on key_down
	 ListBox.SelectedIndex = Listbox.SelectedIndex + 1



(you would of course have to check if you are at the end of the list etc..) this is just to give an idea.

This post has been edited by krum110487: 27 March 2009 - 06:12 AM

Was This Post Helpful? 1
  • +
  • -

#3 FerdT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-March 09

Re: ListBox and Keyboard Control

Posted 27 March 2009 - 04:46 PM

Many thanks for pointing me to the obvious. This is really a case of "not seeing the wood for the trees" and my stupidity. The reason no code was pasted was that the only code I had was the ListBox.Selectedndex. Having tested the app and discovered that the keyboard responded, although incorrectly, I had assumed that my problem was deep and buried in either .Net or the OS. It had not occurred to me that the keyboard still needed handling. Here is the code I have arrived at and the control now functions as expected:
Private Sub lstFps_PreviewKeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles lstFps.PreviewKeyDown
		Dim intN As Integer
		Dim intCount As Integer = lstFps.Items.Count

		Select Case e.Key
			Case Key.Down
				intN = lstFps.SelectedIndex + 1
				If intN = intCount Then
					lstFps.SelectedIndex = 0
				Else
					lstFps.SelectedIndex = intN
				End If
				e.Handled = True
			Case Key.Up
				intN = lstFps.SelectedIndex - 1
				If intN < 0 Then
					lstFps.SelectedIndex = intCount - 1
				Else
					lstFps.SelectedIndex = intN
				End If
				e.Handled = True
			Case Key.Enter
				txtTcInput.Focus()
		End Select
	End Sub



Again many thanks!
Was This Post Helpful? 0
  • +
  • -

#4 Bu Khaled  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 29-March 09

Re: ListBox and Keyboard Control

Posted 29 March 2009 - 06:51 AM

Hi,

Good afternoon,


I want to ask how to write a code to sort the Items I add to the listbox from the Input box in vb.net 2005

Please help me I have an exam and I want to know how to write it?



thanks,

Bu Khaled
Was This Post Helpful? 1

Page 1 of 1