473,721 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

click a button to move to the next item in a listbox

1 New Member
Hi all,
I am new to VB, and I am having some troubles. I have a listbox that when you click on the item it displays a picture in a picture box and a message in a label. It works fine when an item is manually selected, but I want forward and back buttons that will do this. It works once but it doesn't highlight the new item in the listbox, so it gets stuck. I can't use a loop either.

Any help is greatly appreciated. See code below:
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Dim x As Integer
  3.     Dim files() As Object = {"home.jpg", "1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg", "6.jpg", "7.jpg", "8.jpg", "9.jpg", "10.jpg", "11.jpg", "12.jpg", "end.jpg"}
  4.     Dim rules() As String = {"The Basic Rules Of Firearm Safety.", _
  5.     "Handle all firearms as if they were loaded.", _
  6.     "Always keep the firearm pointed in a safe direction.", _
  7.     "Keep your finger out of the gun's trigger guard and off the trigger until you have aligned the gun's sights on a safe target and you have made the decision to fire.", _
  8.     "Always be certain that your target and the surrounding area are safe before firing.", _
  9.     "Whenever you handle a firearm, the first thing you should do (while keeping it pointed in a safe direction with your finger outside the trigger guard) is to open the action to determine whether or not the firearm is loaded.", _
  10.     "Thoroughly read the instruction manual supplied with your firearm.", _
  11.     "Before firing your weapon, you should routinely make sure that your firearm is in good working order and the barrel is clear of dirt and obstructions.", _
  12.     "Only use ammunition recommended by the firearm manufacturer, and always be certain that the ammunition matches the caliber of your gun.", _
  13.     "Quality ear and eye protection should always be worn when shooting or observing.", _
  14.     "Never use firearms while under the influence of drugs or alcohol.", _
  15.     "All firearms should be stored unloaded and secured in a safe storage case, inaccessible to children and untrained adults.", _
  16.     "The transportation of firearms is regulated by Federal, State, and local laws.  Always transport your firearm in a safe, unloaded condition and in accordance with applicable laws.", _
  17.     "Remember-no set of rules can cover all possible situations.  The safe and rational use of a firearm depends on the common sense and proper training of the user."}
  18.  
  19.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  20.  
  21.         ListBox1.Items.Add("Firearm Rules")
  22.         ListBox1.Items.Add("Rule 1")
  23.         ListBox1.Items.Add("Rule 2")
  24.         ListBox1.Items.Add("Rule 3")
  25.         ListBox1.Items.Add("Rule 4")
  26.         ListBox1.Items.Add("Rule 5")
  27.         ListBox1.Items.Add("Rule 6")
  28.         ListBox1.Items.Add("Rule 7")
  29.         ListBox1.Items.Add("Rule 8")
  30.         ListBox1.Items.Add("Rule 9")
  31.         ListBox1.Items.Add("Rule 10")
  32.         ListBox1.Items.Add("Rule 11")
  33.         ListBox1.Items.Add("Rule 12")
  34.         ListBox1.Items.Add("The End")
  35.         ListBox1.SelectedIndex = 0
  36.  
  37.     End Sub
  38.  
  39.     Private Sub ListBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Click
  40.  
  41.         If ListBox1.SelectedIndex = 0 Then
  42.             PictureBox1.Image = Image.FromFile(files(0))
  43.             ruleLabel.Text = rules(0).ToString
  44.         ElseIf ListBox1.SelectedIndex = 1 Then
  45.             PictureBox1.Image = Image.FromFile(files(1))
  46.             ruleLabel.Text = rules(1).ToString
  47.         ElseIf ListBox1.SelectedIndex = 2 Then
  48.             PictureBox1.Image = Image.FromFile(files(2))
  49.             ruleLabel.Text = rules(2).ToString
  50.         ElseIf ListBox1.SelectedIndex = 3 Then
  51.             PictureBox1.Image = Image.FromFile(files(3))
  52.             ruleLabel.Text = rules(3).ToString
  53.         ElseIf ListBox1.SelectedIndex = 4 Then
  54.             PictureBox1.Image = Image.FromFile(files(4))
  55.             ruleLabel.Text = rules(4).ToString
  56.         ElseIf ListBox1.SelectedIndex = 5 Then
  57.             PictureBox1.Image = Image.FromFile(files(5))
  58.             ruleLabel.Text = rules(5).ToString
  59.         ElseIf ListBox1.SelectedIndex = 6 Then
  60.             PictureBox1.Image = Image.FromFile(files(6))
  61.             ruleLabel.Text = rules(6).ToString
  62.         ElseIf ListBox1.SelectedIndex = 7 Then
  63.             PictureBox1.Image = Image.FromFile(files(7))
  64.             ruleLabel.Text = rules(7).ToString
  65.         ElseIf ListBox1.SelectedIndex = 8 Then
  66.             PictureBox1.Image = Image.FromFile(files(8))
  67.             ruleLabel.Text = rules(8).ToString
  68.         ElseIf ListBox1.SelectedIndex = 9 Then
  69.             PictureBox1.Image = Image.FromFile(files(9))
  70.             ruleLabel.Text = rules(9).ToString
  71.         ElseIf ListBox1.SelectedIndex = 10 Then
  72.             PictureBox1.Image = Image.FromFile(files(10))
  73.             ruleLabel.Text = rules(10).ToString
  74.         ElseIf ListBox1.SelectedIndex = 11 Then
  75.             PictureBox1.Image = Image.FromFile(files(11))
  76.             ruleLabel.Text = rules(11).ToString
  77.         ElseIf ListBox1.SelectedIndex = 12 Then
  78.             PictureBox1.Image = Image.FromFile(files(12))
  79.             ruleLabel.Text = rules(12).ToString
  80.         ElseIf ListBox1.SelectedIndex = 13 Then
  81.             PictureBox1.Image = Image.FromFile(files(13))
  82.             ruleLabel.Text = rules(13).ToString
  83.         End If
  84.  
  85.     End Sub
  86.  
  87.     Private Sub fwdButton_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles fwdButton.Click
  88.         x = ListBox1.SelectedIndex + 1
  89.         PictureBox1.Image = Image.FromFile(files(x))
  90.         ruleLabel.Text = rules(x)
  91.     End Sub
  92.  
  93.     Private Sub backButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles backButton.Click
  94.         x = ListBox1.SelectedIndex - 1
  95.         PictureBox1.Image = Image.FromFile(files(x))
  96.         ruleLabel.Text = rules(x)
  97.     End Sub
  98.  
  99. End Class
Feb 28 '09 #1
2 4409
debasisdas
8,127 Recognized Expert Expert
question moved to .net forum.
Mar 1 '09 #2
tlhintoq
3,525 Recognized Expert Specialist
That if...else construct is killer.
Suggestion: Get the SelectedIndex then just display that indexed image and label text.

Expand|Select|Wrap|Line Numbers
  1. int x = ListBox1.SelectedIndex; // C# for make a int named x containing SelectedIndex.  You will have to phrase for VB
  2.  
  3. If x >= 0 Then 
  4.             PictureBox1.Image = Image.FromFile(files(x)) 
  5.             ruleLabel.Text = rules(x).ToString 
  6.  
Next suggestion: Qualify the math of your next and back buttons. You can't just keep adding and subtracting 1 from them and expect them to work. If someone keeps mashing on the next button they will drive it up to 1000 when you only have 15 items. Now they have to mash it 1000 times down before changes will take place.

If (x+1) < ListBox.Items.C ount then add one to the index

Last suggestion: Don't use the next and back buttons to directly change the items in the picture box and label. Use these buttons to change the selected index of the ListBox. This will fire the event of ListBox.Selecte dIndexChanged which causes your picture and label to change because you've already written this. So why write it again, and again in the next and back button behaviors?

This also makes your UI match your program's thinking because the listbox will show what is selected.

Most importantly the change behavior takes place in one method only. That is really important. Otherwise you will quickly have a dozen methods that all seem to do the same thing and it will be a monster to update. Any behavior should take place in one method only. Other methods can call it all they like.
Mar 1 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1564
by: Andy Warner | last post by:
Hello all, I have a listbox with 2 'fields' and a choice of 100 or so items that can be selected. User can select multiple items (i'm using the simple multiitem model) as normal. however, I want several buttons that will select a set of these items if pressed.
6
30018
by: Dan Bass | last post by:
If you look at explorer, right clicking on a file, first selects the file, then throws up the context menu relating to that selection. With a Windows ListBox control and a simple context menu, the default behaviour seems to display the context menu on the listbox, but there is no selection. Now I know that explorer's using a listview, and that the context menu acts differently if you click on open space (no selection), but can I...
0
2597
by: AK | last post by:
Hello, Listbox mulitselect property is set to true (extended). I am trying to implement drag and drop feature for a listbox. The drag & drop is done in the mouse move event (DoDragdrop function is called in mouse move event). I am facing two problems: 1. First is that when shift key is pressed and left mouse button is down. The listbox does not give the correct count of selected items. It gives only one item as selected...
6
6182
by: Steve | last post by:
Hi, I open up a webform (vb.net) and populate a listbox control on the Page load event. If I click on (select) and item from the listbox I want to write the value of the selected item to a label control. Nothing happens. But when I click a button (which contains same code to write to label) I get the value of the selected item in the Label.
3
3215
by: Rosanne | last post by:
Is there a way to clear the SelectedIndex of a listbox when the browser's back button is clicked? I have a page that contains a server side list box with AutoPostBack = True. When the user clicks an item in the listbox, they are redirected to an information page based on their selection. On this information page, if the user clicks the browser's back button they are taken back to the first page with the item in the listbox still...
1
4105
by: Firewalker | last post by:
I am attempting to change the backColor property on the previously instantiated buttons FROM a listbox_doubleClick event. I thought it would be something like this: If Me.Controls.Item(iSeatNumber).BackColor.Equals(Color.White) Then Me.Controls.Item(iSeatNumber).BackColor.Equals(Color.CornflowerBlue) 'End If
3
4726
by: Art | last post by:
Hi, I've got a bunch of ListBoxes. I want to have a context menu that will include, for example, an edit routine. The problem I have is the following. Suppose you select item 1. Then you move your mouse to item 9 and right click. Item 1 is still selected. Is there any way to have the right click select item 9 and then display the context menu? As usual... I'd really appreciate any suggestions.
5
6760
by: Amoril | last post by:
I've read quite a few different message on various boards and for some reason I'm still having trouble wrapping my head around this viewstate maintenance and trying to get these dynamically created link buttons to stay wired up to their click events. I have what is basically a simply survey question generation page. The page first displays a few static fields and a dropdownlist of various options for the user to select. When the user...
2
4433
by: vinod allapu | last post by:
hi all, i am trying to generate a report, iam using sql server 2000 and Asp.net 2.0. First to select the columns iam using the list box... as we can order by the query result from the selected columns only, i want to use another list box which contains only selected columns.. 1. Iam filling the columns names required into the listbox it is selecting the columns in the select statement to generate report. 2....
0
8840
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8730
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9367
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6669
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4484
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4753
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3189
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2130
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.