I have a program I created in VisualBasic that is very similar to a slideshow image viewer. On the left side the user is able to select an image from a location and display it. On the right side I would like them to be able to use the combo box to select a category which would only display images in that category.
I am currently able to get an image list loaded in the right side and the user can cycle through that list, but I am having trouble figuring out how to connect the combobox and imagelist. Maybe there is another route I should try?
Public Class Form1
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If OpenFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
End If
End Sub
Private Sub Btnnext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
btnPrev.Enabled = True
Static i As Integer
Dim incp As String
incp = +1
i += 1
PictureBox2.Image = ImageList1.Images(i)
If i = ImageList1.Images.Count - 1 Then
i = -1
End If
End Sub
Private Sub Btnprevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
Static i As Integer
Dim incp As String
incp = +1
i += 1
PictureBox2.Image = ImageList1.Images(i)
If i = ImageList1.Images.Count - 1 Then
i = -incp
End If
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
End Sub
End Class
So you're saying that you have multiple ImageList objects and you want the user to select one using the ComboBox? If so then you do it like you would for selecting from any other list of objects using a ComboBox, e.g.
category1ImageList.Tag = "Category 1"
category2ImageList.Tag = "Category 2"
categoriesComboBox.DisplayMember = "Tag"
categoriesComboBox.DataSource = {category1ImageList, category2ImageList}
The user selects from the Tag values and then the SelectedItem of the ComboBox is the selected ImageList.
Related
So I have two string arrays, one for my friends, the other for their numbers. I go to my combo box (displays list of names) I click on it and it displays their number on a label. My problem is I want the user to a enter a new name and number in 2 textboxes, then transfer the name on the combo box. Once their name is in the box, I click on it and it display's their number on label. Is there a way to transfer the new items onto my arrays?
Option Explicit On
Module MainModule
Public strPeople() As String = {"Kyle", "John", "Jake", "Donna", "Carly", "Ty", "Mavis"}
Public strPhoneNumbers() As String = {"945-1232", "804-2329", "290-7321", "928-4569", "205-9893", "320-0195", "305-4520"}
Public tempList As New List(Of String)
End Module
Here Is My Main Form
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.Items.AddRange(strPeople)
End Sub
Private Sub AboutToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles AboutToolStripMenuItem1.Click
AboutBox1.ShowDialog()
End Sub
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
Application.Exit()
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim strPhoneNums As String = strPhoneNumbers(ComboBox1.SelectedIndex)
Label3.Text = "Phone Number: " & strPhoneNums
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
//Add Contact Button
If TextBox1.Text <> "" Then
ReDim Preserve strPeople(7)
strPeople(7) = TextBox1.Text
ComboBox1.Items.Add(strPeople(7))
End If
If TextBox2.Text <> "" Then
ReDim Preserve strPhoneNumbers(7)
strPhoneNumbers(7) = TextBox2.Text
End If
TextBox1.Clear()
TextBox2.Clear()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Application.Exit()
End Sub
I'm very frustrated trying to get my code to work.
I'm trying to have a selected item in a listbox removed also in the textbox.
Getting ready to remove text;
Removed the text;
But it's still in the textbox.
Here is my code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Add(TextBox1.Text)
TextBox2.Text += TextBox1.Text & vbNewLine
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
'
'//HOW TO REMOVE THE SELECTED TEXT IN THE LISTBOX ALSO REMOVED IN THE TEXTBOX2??
'
'
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Dim filenames As String = "C:\log\log.txt"
My.Computer.FileSystem.WriteAllText(filenames, TextBox2.Text, False)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim filenames As String = "C:\log\log.txt"
If My.Computer.FileSystem.FileExists(filenames) Then
TextBox2.Text = My.Computer.FileSystem.ReadAllText(filenames)
Dim items()
items = TextBox2.Lines()
For Each item In items
ListBox1.Items.Add(item)
Next
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Clipboard.SetText(ListBox1.SelectedItem)
End Sub
End Class
The worst part is that every time I try to look it up online, there are no errors until I click the button that says 'Value Cannot Be Null'
It happened every single time.
Please, before you mash the -1 button, at least tell me why. I'm new to this.
This should work for you
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = TextBox1.Text.Replace(ListBox1.Items(ListBox1.SelectedIndex), Nothing)
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
End Sub
End Class
Below I have an array and in my design I have a check list box with 10 options. For example, if boxes 1 and 2 were checked, I would only want to print Indexes 0 and 1 ONLY. I have a button that prints all of the array members (included below) and that is what I want to make print only selected items. I have tried using a switch but that file had gotten corrupted and I am lost. Thank you. (Language is VB)
Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btn1.Click
Dim strDecimal(9) As String
strDecimal(0) = FormatPercent(0.0146175)
strDecimal(1) = FormatPercent(0.0345324585)
strDecimal(2) = FormatPercent(0.09324543575)
strDecimal(3) = FormatPercent(0.07346475)
strDecimal(4) = FormatPercent(0.0772346615)
strDecimal(5) = FormatPercent(0.42234234654)
strDecimal(6) = FormatPercent(0.6246264664)
strDecimal(7) = FormatPercent(0.4524642234)
strDecimal(8) = FormatPercent(0.6876543534)
strDecimal(9) = FormatPercent(0.6876543534)
For num As Integer = 0 To strDecimal.Length - 1
listArrays.Items.Add(strDecimal(num))
Next
End Sub
Private Sub clearList()
listArrays.Items.Clear()
End Sub
Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
clearList()
End Sub
Assuming you're using a CheckedListBox and want to know which items are Checked:
Private Sub btn3_Click(sender As Object, e As EventArgs) Handles btn3.Click
For Each itm As String In listArrays.CheckedItems
Debug.Print(itm)
Next
End Sub
How do I capture the name of a button so I can use it in another form to query a database. I am new to vb and still at the very early learning stage so any help would be greatfully appreciated.
frmMain
Private Sub btnA_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnA.MouseDown
If (e.Button = Windows.Forms.MouseButtons.Right) Then
frmRacks.Show()
ElseIf (e.Button = Windows.Forms.MouseButtons.Left) Then
MessageBox.Show("Left clicked")
End If
End Sub
frmRacks
Here is where I need to capture name to query database
Private Sub Button1_MouseDown(sender As Object, e As MouseEventArgs) Handles Button1.MouseDown
Dim name As String = DirectCast(sender, Button).Name
End Sub
There are many ways to do this, one is to declare public field/variable in frmRack, another is using ShowDialog instead of Show:
I'll go with the first one (public) first:
Public buttonName as String
And in the button click from your frmMain you pass the value like:
Private Sub btnA_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnA.MouseDown
If (e.Button = Windows.Forms.MouseButtons.Right) Then
frmRacks.buttonName = "btnA" ' Or you could use DirectCast as proposed by dbasnett
frmRacks.Show()
ElseIf (e.Button = Windows.Forms.MouseButtons.Left) Then
MessageBox.Show("Left clicked")
End If
End Sub
And then in Loading your frmRacks you have now the option of assigning button Name, like:
Private Sub frmRacks_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim query as String = "SELECT * FROM " + buttonName 'This is only an example you could make your own here
End Sub
It should be MouseButtons.Left instead of Windows.Forms.MouseButtons.Left
If e.Button = MouseButtons.Left Then
MsgBox("Left Button Clicked") 'OR WHATEVER YOU WANT IT TO DO?!
End If
This question has been answered.
Do not want to leave it exposed.
Private Sub btnTTL_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles findzipButton.Click
Dim zipCode As String
'forgot
If (ListBox1.FindString(findzipButton.Text) >= 0) Then
ttlTextBox.Text = "$15"
ElseIf (ListBox2.FindString(findzipButton.Text) >= 0) Then
ttlTextBox.Text = "$20"
Else
MessageBox.Show("The zipcode was not found!")
End If
End Sub
End Class
So what I think your trying to do is match the input a user puts into a text box with a selection in the either ListBoxA or ListBoxB. I just tried this in VS 2012, and it seems to work in the way the problem above describes, but I'm only trying to find and display the shipping cost:
Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub ListBox1_Load(sender As Object, e As EventArgs) Handles Me.Load
ListBox1.Items.Add("60611")
ListBox1.Items.Add("60234")
ListBox1.Items.Add("56789")
ListBox1.Items.Add("23467")
ListBox1.Items.Add("60543")
ListBox1.Items.Add("60561")
ListBox1.Items.Add("55905")
ListBox1.Items.Add("89567")
ListBox2.Items.Add("50978")
ListBox2.Items.Add("78432")
ListBox2.Items.Add("98432")
ListBox2.Items.Add("97654")
ListBox2.Items.Add("20245")
End Sub
Private Sub btnFind_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFind.Click
Dim zipCode As String = txtZipCode.Text
If (ListBox1.FindString(zipCode) >= 0) Then
txtShipping.Text = "$15"
ElseIf (ListBox2.FindString(zipCode) >= 0) Then
txtShipping.Text = "$20"
Else
MessageBox.Show("The zipcode was not found!")
End If
End Sub
You were on the right track. What you needed to do was compare what the user actually input to the textbox to what was available in the Listbox. The FindItem() method will then result a Long. If it did find your search string it will build the shipping text box.