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: -
Public Class Form1
-
Dim x As Integer
-
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"}
-
Dim rules() As String = {"The Basic Rules Of Firearm Safety.", _
-
"Handle all firearms as if they were loaded.", _
-
"Always keep the firearm pointed in a safe direction.", _
-
"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.", _
-
"Always be certain that your target and the surrounding area are safe before firing.", _
-
"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.", _
-
"Thoroughly read the instruction manual supplied with your firearm.", _
-
"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.", _
-
"Only use ammunition recommended by the firearm manufacturer, and always be certain that the ammunition matches the caliber of your gun.", _
-
"Quality ear and eye protection should always be worn when shooting or observing.", _
-
"Never use firearms while under the influence of drugs or alcohol.", _
-
"All firearms should be stored unloaded and secured in a safe storage case, inaccessible to children and untrained adults.", _
-
"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.", _
-
"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."}
-
-
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
-
-
ListBox1.Items.Add("Firearm Rules")
-
ListBox1.Items.Add("Rule 1")
-
ListBox1.Items.Add("Rule 2")
-
ListBox1.Items.Add("Rule 3")
-
ListBox1.Items.Add("Rule 4")
-
ListBox1.Items.Add("Rule 5")
-
ListBox1.Items.Add("Rule 6")
-
ListBox1.Items.Add("Rule 7")
-
ListBox1.Items.Add("Rule 8")
-
ListBox1.Items.Add("Rule 9")
-
ListBox1.Items.Add("Rule 10")
-
ListBox1.Items.Add("Rule 11")
-
ListBox1.Items.Add("Rule 12")
-
ListBox1.Items.Add("The End")
-
ListBox1.SelectedIndex = 0
-
-
End Sub
-
-
Private Sub ListBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Click
-
-
If ListBox1.SelectedIndex = 0 Then
-
PictureBox1.Image = Image.FromFile(files(0))
-
ruleLabel.Text = rules(0).ToString
-
ElseIf ListBox1.SelectedIndex = 1 Then
-
PictureBox1.Image = Image.FromFile(files(1))
-
ruleLabel.Text = rules(1).ToString
-
ElseIf ListBox1.SelectedIndex = 2 Then
-
PictureBox1.Image = Image.FromFile(files(2))
-
ruleLabel.Text = rules(2).ToString
-
ElseIf ListBox1.SelectedIndex = 3 Then
-
PictureBox1.Image = Image.FromFile(files(3))
-
ruleLabel.Text = rules(3).ToString
-
ElseIf ListBox1.SelectedIndex = 4 Then
-
PictureBox1.Image = Image.FromFile(files(4))
-
ruleLabel.Text = rules(4).ToString
-
ElseIf ListBox1.SelectedIndex = 5 Then
-
PictureBox1.Image = Image.FromFile(files(5))
-
ruleLabel.Text = rules(5).ToString
-
ElseIf ListBox1.SelectedIndex = 6 Then
-
PictureBox1.Image = Image.FromFile(files(6))
-
ruleLabel.Text = rules(6).ToString
-
ElseIf ListBox1.SelectedIndex = 7 Then
-
PictureBox1.Image = Image.FromFile(files(7))
-
ruleLabel.Text = rules(7).ToString
-
ElseIf ListBox1.SelectedIndex = 8 Then
-
PictureBox1.Image = Image.FromFile(files(8))
-
ruleLabel.Text = rules(8).ToString
-
ElseIf ListBox1.SelectedIndex = 9 Then
-
PictureBox1.Image = Image.FromFile(files(9))
-
ruleLabel.Text = rules(9).ToString
-
ElseIf ListBox1.SelectedIndex = 10 Then
-
PictureBox1.Image = Image.FromFile(files(10))
-
ruleLabel.Text = rules(10).ToString
-
ElseIf ListBox1.SelectedIndex = 11 Then
-
PictureBox1.Image = Image.FromFile(files(11))
-
ruleLabel.Text = rules(11).ToString
-
ElseIf ListBox1.SelectedIndex = 12 Then
-
PictureBox1.Image = Image.FromFile(files(12))
-
ruleLabel.Text = rules(12).ToString
-
ElseIf ListBox1.SelectedIndex = 13 Then
-
PictureBox1.Image = Image.FromFile(files(13))
-
ruleLabel.Text = rules(13).ToString
-
End If
-
-
End Sub
-
-
Private Sub fwdButton_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles fwdButton.Click
-
x = ListBox1.SelectedIndex + 1
-
PictureBox1.Image = Image.FromFile(files(x))
-
ruleLabel.Text = rules(x)
-
End Sub
-
-
Private Sub backButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles backButton.Click
-
x = ListBox1.SelectedIndex - 1
-
PictureBox1.Image = Image.FromFile(files(x))
-
ruleLabel.Text = rules(x)
-
End Sub
-
-
End Class
2 4409
question moved to .net forum.
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. -
int x = ListBox1.SelectedIndex; // C# for make a int named x containing SelectedIndex. You will have to phrase for VB
-
-
If x >= 0 Then
-
PictureBox1.Image = Image.FromFile(files(x))
-
ruleLabel.Text = rules(x).ToString
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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.
|
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...
|
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...
|
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.
|
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...
| |
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
|
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.
|
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...
|
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....
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |