I'm making a File Explorer in VB.NET. Everything is going fine except one thing. When you click the dynamically created label to "open" a folder, I need to get the value of the label (so I can set a variable to it.) I can't get the value because the label was dynamically create. Therefore the object doesn't exist. Here's my code:
Imports System.IO
Public Class Form1
Dim Path As String
Dim FolderCount = 0
Dim FolderWidth = 128
Dim FolderHeight = 128
Dim WidthAndPadding = FolderWidth + 10
Dim HeightAndPadding = FolderHeight + 10
Dim FolderTitle As String
Dim CombinedWidth
Dim FolderRows = 0
Dim OnNewLine = False
Dim FolderTop = 0
Dim FolderLeft = 0
Dim FullPath
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.AutoScroll = True
Path = "C:\Program Files\"
For Each Dir As String In Directory.GetDirectories(Path)
CreateFolders(Dir)
Next
End Sub
Public Sub CreateFolders(ByVal StrDirectory)
FolderTitle = StrDirectory.Substring(Path.Length)
Dim Folder As New Label
FolderLeft = WidthAndPadding * FolderCount
Folder.Left = FolderLeft
Folder.Top = FolderTop
Folder.Width = FolderWidth
Folder.Height = FolderHeight
Folder.Image = My.Resources.Folder
Folder.TextAlign = ContentAlignment.BottomCenter
If FolderTitle.Length > 15 Then
FolderTitle = FolderTitle.Substring(0, 15) + "..."
End If
Folder.Text = FolderTitle
Folder.Font = New Font("Arial", 9.5)
FolderCount += 1
CombinedWidth = FolderCount * WidthAndPadding
If CombinedWidth >= Me.Width Then
OnNewLine = False
If OnNewLine = False Then
FolderRows = FolderRows + 1
FolderCount = 0
CombinedWidth = 0
Folder.Left = FolderCount * FolderWidth
FolderTop = FolderRows * FolderHeight
Folder.Top = FolderTop
OnNewLine = True
'FolderCount += 1
End If
End If
Me.Controls.Add(Folder)
AddHandler Folder.DoubleClick, AddressOf Folder_DoubleClick
FullPath = Path + FolderTitle
End Sub
Private Sub Folder_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
'MsgBox(Folder.Text)
End Sub
End Class
The event handler's sender parameter contains the label that generated the event.
You can cast it back to Label by writing CType(sender, Label).
Related
how do i hide specific name folder within the treelist ? i have tree list like this
and i want to hide folder name New folder and pdf. i make this from Load Data Dir. i have tweak a bit in the code like this by putting if
Dim rootfolder As String = "C:\\FFOutput"
Private Sub TreeList1_VirtualTreeGetCellValue(ByVal sender As Object, ByVal e As DevExpress.XtraTreeList.VirtualTreeGetCellValueInfo) Handles TreeList1.VirtualTreeGetCellValue
Dim di As New DirectoryInfo(CStr(e.Node))
If di.Name <> "New folder" Then
If e.Column Is TreeListColumn1 Then
e.CellData = di.Name
End If
If e.Column Is TreeListColumn2 Then
e.CellData = e.Node.ToString
End If
If e.Column Is TreeListColumn3 Then
If IsFile(di) Then
e.CellData = New FileInfo(CStr(e.Node)).Extension
Else
e.CellData = Nothing
End If
End If
End If
End Sub
Private Sub TreeList1_VirtualTreeGetChildNodes(ByVal sender As Object,
ByVal e As DevExpress.XtraTreeList.VirtualTreeGetChildNodesInfo) _
Handles TreeList1.VirtualTreeGetChildNodes
If loadDrives = False Then
Dim root As String() = Directory.GetDirectories(rootfolder)
e.Children = root
loadDrives = True
Else
Try
Dim di As New DirectoryInfo(CStr(e.Node))
If di.Name <> "New folder" Then
Dim path As String = CStr(e.Node)
If Directory.Exists(path) Then
Dim dirs As String() = Directory.GetDirectories(path)
Dim files As String() = Directory.GetFiles(path)
Dim arr(dirs.Length + files.Length) As String
dirs.CopyTo(arr, 0)
files.CopyTo(arr, dirs.Length)
e.Children = arr
Else
e.Children = New Object() {}
End If
End If
Catch
End Try
End If
End Sub
and it become like this
I got asked a couple of days ago how to save number of clicks you have done to each button in a program to a small file, to keep track of what is most used. Since it was ages ago i dabbled with visual basic i said i would raise the question here, so here goes.
There are 5 buttons labels Button1-Button5. When a button is clicked it should look for the correct value in a file.txt and add +1 to the value. The file is structured like this.
Button1 = 0
Button2 = 0
Button3 = 0
Button4 = 0
Button5 = 0
So when button1 is clicked it should open file.txt, look for the line containing Button1 and add +1 to the value and close the file.
I have tried looking for some kind of tutorial on this but have not found it so i'm asking the collective of brains here on Stackoverflow for some guidance.
Thanks in advance.
delete file first
new ver ..
Imports System.IO.File
Imports System.IO.Path
Imports System.IO
Imports System.Text
Public Class Form1
Dim line() As String
Dim strbild As StringBuilder
Dim arr() As String
Dim i As Integer
Dim pathAndFile As String
Dim addLine As System.IO.StreamWriter
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim fileName As String = "myfile.txt"
Dim appPath As String = My.Application.Info.DirectoryPath
pathAndFile = Path.Combine(appPath, fileName)
If System.IO.File.Exists(pathAndFile) Then
'line = File.ReadAllLines(pathAndFile)
Else
Dim addLineToFile = System.IO.File.CreateText(pathAndFile) 'Create an empty txt file
Dim countButtons As Integer = 5 'add lines with your desired number of buttons
For i = 1 To countButtons
addLineToFile.Write("Button" & i & " = 0" & vbNewLine)
Next
addLineToFile.Dispose() ' and close file
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
writeToFile(1)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
writeToFile(2)
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
writeToFile(3)
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
writeToFile(4)
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
writeToFile(5)
End Sub
Public Sub writeToFile(buttonId As Integer)
Dim tmpButton As String = "Button" & buttonId
strbild = New StringBuilder
line = File.ReadAllLines(pathAndFile)
Dim f As Integer = 0
For Each lineToedit As String In line
If InStr(1, lineToedit, tmpButton) > 0 Then
Dim lineSplited = lineToedit.Split
Dim cnt As Integer = lineSplited.Count
Dim newVal = addCount(CInt(lineSplited.Last()))
lineSplited(cnt - 1) = newVal
lineToedit = String.Join(" ", lineSplited)
line(f) = lineToedit
End If
f = f + 1
Next
strbild = New StringBuilder
'putting together all the reversed word
For j = 0 To line.GetUpperBound(0)
strbild.Append(line(j))
strbild.Append(vbNewLine)
Next
' writing to original file
File.WriteAllText(pathAndFile, strbild.ToString())
Me.Refresh()
End Sub
' Reverse Function
Public Function ReverseString(ByRef strToReverse As String) As String
Dim result As String = ""
For i As Integer = 0 To strToReverse.Length - 1
result += strToReverse(strToReverse.Length - 1 - i)
Next
Return result
End Function
Public Function addCount(ByVal arr As Integer) As Integer
Return arr + 1
End Function
End Class
Output in myfile.txt
Button1 = 9
Button2 = 2
Button3 = 4
Button4 = 0
Button5 = 20
Create a vb winform Application
Drag on your form 5 buttons (Button1, Button2, ... etc)
copy that :
Imports System.IO.File
Imports System.IO.Path
Imports System.IO
Imports System.Text
Public Class Form1
Dim line() As String
Dim strbild As StringBuilder
Dim arr() As String
Dim i As Integer
Dim pathAndFile As String
Dim addLine As System.IO.StreamWriter
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim fileName As String = "myfile.txt"
Dim appPath As String = My.Application.Info.DirectoryPath
pathAndFile = Path.Combine(appPath, fileName)
If System.IO.File.Exists(pathAndFile) Then
line = File.ReadAllLines(pathAndFile)
Else
Dim addLineToFile = System.IO.File.CreateText(pathAndFile) 'Create an empty txt file
Dim countButtons As Integer = 5 'add lines with your desired number of buttons
For i = 1 To countButtons
addLineToFile.Write("Button" & i & " = 0" & vbNewLine)
Next
addLineToFile.Dispose() ' and close file
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
writeToFile(1)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
writeToFile(2)
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
writeToFile(3)
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
writeToFile(4)
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
writeToFile(5)
End Sub
Public Sub writeToFile(buttonId As Integer)
Dim tmpButton As String = "Button" & buttonId
strbild = New StringBuilder
line = File.ReadAllLines(pathAndFile)
i = 0
For Each lineToedit As String In line
If lineToedit.Contains(tmpButton) Then
Dim lineSplited = lineToedit.Split
Dim newVal = addCount(CInt(lineSplited.Last()))
'lineToedit.Replace(lineSplited.Last, newVal)
lineToedit = lineToedit.Replace(lineToedit.Last, newVal)
line(i) = lineToedit
End If
i = i + 1
Next
strbild = New StringBuilder
'putting together all the reversed word
For j = 0 To line.GetUpperBound(0)
strbild.Append(line(j))
strbild.Append(vbNewLine)
Next
' writing to original file
File.WriteAllText(pathAndFile, strbild.ToString())
End Sub
' Reverse Function
Public Function ReverseString(ByRef strToReverse As String) As String
Dim result As String = ""
For i As Integer = 0 To strToReverse.Length - 1
result += strToReverse(strToReverse.Length - 1 - i)
Next
Return result
End Function
Public Function addCount(ByVal arr As Integer) As Integer
Return arr + 1
End Function
End Class
Have fun ! :)CristiC777
try this on vs2013
change If confition
line = File.ReadAllLines(pathAndFile)
i = 0
For Each lineToedit As String In line
If InStr(1, lineToedit, tmpButton) > 0 Then
'If lineToedit.Contains(tmpButton) = True Then
Dim lineSplited = lineToedit.Split
Dim newVal = addCount(CInt(lineSplited.Last()))
lineToedit = lineToedit.Replace(lineToedit.Last, newVal)
line(i) = lineToedit
End If
i = i + 1
Next
This is my first time on this site and I would like help very much.
I made a program in VB 2010 Express to try and read a .txt file into an DataGridView. I got it to read in and I have a function to search through surnames and search through date of birth. The way I want it to search is so that if I type in the month (02) for February, then it will list all the people with the DOB month as February. The surnames work fine but the DOB doesn't. Whenever I try to search the month February (02) The DataGridView becomes blank. http://puu.sh/8asUD.png (picture of addressgrid/datagridview going blank) The contents of the .txt file are exactly:
Jackson,Samantha,2 Heather Row,Basingstoke,RG21 3SD,01256 135434,23/04/1973,sam.jackson#hotmail.com,Vickers,Jonathan,18 Saville Gardens,Reading,RG3 5FH,01196 678254,04/02/1965,the_man#btinternet.com,Morris,Sally,The Old Lodge, Hook,RG23 5RD,01256 728443,19/02/1975,smorris#fgh.co.uk,Cobbly,Harry,345 The High Street,Guildford,GU2 4KJ,01458 288763,30/03/1960,harry.cobbly#somewhere.org.uk,Khan,Jasmine,36 Hever Avenue,Edenbridge,TN34 4FG,01569 276524,28/02/1980,jas.khan#hotmail.com,Vickers,Harriet,45 Sage Gardens,Brighton,BN3 2FG,01675 662554,04/04/1968,harriet.vickers#btinternet.com
Here's the code for THE ENTIRE FORM:
Public Class AddressBook
Structure Address
Public Forename As String
Public Surname As String
Public Address1 As String
Public Address2 As String
Public Postcode As String
Public Phonenumber As String
Public DOB As String
Public Email As String
End Structure
Public Addressbook(40) As Address
Public recordcounter As Integer = 0
Public readfile As Boolean = False
Private Sub btt_read_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btt_read.Click
'Declare Textfile
If System.IO.File.Exists("addressbook.txt") = False Then
MsgBox("This text file doesn't exist")
End If
Dim Addressfile As New System.IO.StreamReader("C:\Users\DirtyMike\Desktop\123\Controlled Assessment\addressbook.txt", True)
Dim loopcounter As Integer
Dim filecontent As String
Dim temparray() As String
If readfile = False Then
filecontent = Addressfile.ReadLine
temparray = filecontent.Split(",")
readfile = True
'Display the data
For loopcounter = 0 To (temparray.Length - 7) Step 8
Addressbook(recordcounter).Surname = temparray(loopcounter + 0)
Addressbook(recordcounter).Forename = temparray(loopcounter + 1)
Addressbook(recordcounter).Address1 = temparray(loopcounter + 2)
Addressbook(recordcounter).Address2 = temparray(loopcounter + 3)
Addressbook(recordcounter).Postcode = temparray(loopcounter + 4)
Addressbook(recordcounter).Phonenumber = temparray(loopcounter + 5)
Addressbook(recordcounter).DOB = temparray(loopcounter + 6)
Addressbook(recordcounter).Email = temparray(loopcounter + 7)
recordcounter = recordcounter + 1
Next
Addressfile.Close()
REM Display the Data
For loopcounter = 0 To recordcounter - 1
AddressGrid.Rows.Add()
AddressGrid.Rows.Item(loopcounter).Cells(0).Value = Addressbook(loopcounter).Surname
AddressGrid.Rows.Item(loopcounter).Cells(1).Value = Addressbook(loopcounter).Forename
AddressGrid.Rows.Item(loopcounter).Cells(2).Value = Addressbook(loopcounter).Address1
AddressGrid.Rows.Item(loopcounter).Cells(3).Value = Addressbook(loopcounter).Address2
AddressGrid.Rows.Item(loopcounter).Cells(6).Value = Addressbook(loopcounter).DOB
AddressGrid.Rows.Item(loopcounter).Cells(7).Value = Addressbook(loopcounter).Email
AddressGrid.Rows.Item(loopcounter).Cells(5).Value = Addressbook(loopcounter).Phonenumber
AddressGrid.Rows.Item(loopcounter).Cells(4).Value = Addressbook(loopcounter).Postcode
Next
'Display the data from the file.
End If
End Sub
Private Sub bttsurname_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btt_searchsur.Click
AddressGrid.Rows.Clear()
Dim surname As String
Dim loopcounter As Integer
Dim positioncounter As Integer
surname = txt_surname.Text
For loopcounter = 0 To recordcounter
If Addressbook(loopcounter).Surname = surname Then
Me.AddressGrid.Rows.Add()
Me.AddressGrid.Rows.Item(positioncounter).Cells(0).Value = Me.Addressbook(loopcounter).Surname
Me.AddressGrid.Rows.Item(positioncounter).Cells(1).Value = Me.Addressbook(loopcounter).Forename
Me.AddressGrid.Rows.Item(positioncounter).Cells(2).Value = Me.Addressbook(loopcounter).Address1
Me.AddressGrid.Rows.Item(positioncounter).Cells(3).Value = Me.Addressbook(loopcounter).Address2
Me.AddressGrid.Rows.Item(positioncounter).Cells(4).Value = Me.Addressbook(loopcounter).Postcode
Me.AddressGrid.Rows.Item(positioncounter).Cells(5).Value = Me.Addressbook(loopcounter).Phonenumber
Me.AddressGrid.Rows.Item(positioncounter).Cells(6).Value = Me.Addressbook(loopcounter).DOB
Me.AddressGrid.Rows.Item(positioncounter).Cells(7).Value = Me.Addressbook(loopcounter).Email
positioncounter = positioncounter + 1
End If
Next
End Sub
Private Sub btt_menu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btt_menu.Click
Menu1.Show()
Me.Close()
End Sub
Private Sub btt_searchdob_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btt_searchdob.Click
AddressGrid.Rows.Clear()
Dim DOB As String
Dim loopcounter, positioncounter As Integer
Dim MonthA As String
DOB = txt_DOB.Text
MonthA = Mid(DOB, 4, 2)
positioncounter = 0
For loopcounter = 0 To recordcounter
If MonthA = Mid(Addressbook(loopcounter).DOB, 4, 2) Then
Me.AddressGrid.Rows.Add()
Me.AddressGrid.Rows.Item(positioncounter).Cells(0).Value = Me.Addressbook(loopcounter).Surname
Me.AddressGrid.Rows.Item(positioncounter).Cells(1).Value = Me.Addressbook(loopcounter).Forename
Me.AddressGrid.Rows.Item(positioncounter).Cells(2).Value = Me.Addressbook(loopcounter).Address1
Me.AddressGrid.Rows.Item(positioncounter).Cells(3).Value = Me.Addressbook(loopcounter).Address2
Me.AddressGrid.Rows.Item(positioncounter).Cells(4).Value = Me.Addressbook(loopcounter).Postcode
Me.AddressGrid.Rows.Item(positioncounter).Cells(5).Value = Me.Addressbook(loopcounter).Phonenumber
Me.AddressGrid.Rows.Item(positioncounter).Cells(6).Value = Me.Addressbook(loopcounter).DOB
Me.AddressGrid.Rows.Item(positioncounter).Cells(7).Value = Me.Addressbook(loopcounter).Email
positioncounter = positioncounter + 1
End If
Next
End Sub
Private Sub btt_clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btt_clear.Click
AddressGrid.Rows.Clear()
For loopcounter = 0 To recordcounter - 1
AddressGrid.Rows.Add()
AddressGrid.Rows.Item(loopcounter).Cells(0).Value = Addressbook(loopcounter).Surname
AddressGrid.Rows.Item(loopcounter).Cells(1).Value = Addressbook(loopcounter).Forename
AddressGrid.Rows.Item(loopcounter).Cells(2).Value = Addressbook(loopcounter).Address1
AddressGrid.Rows.Item(loopcounter).Cells(3).Value = Addressbook(loopcounter).Address2
AddressGrid.Rows.Item(loopcounter).Cells(4).Value = Addressbook(loopcounter).Postcode
AddressGrid.Rows.Item(loopcounter).Cells(5).Value = Addressbook(loopcounter).Phonenumber
AddressGrid.Rows.Item(loopcounter).Cells(6).Value = Addressbook(loopcounter).DOB
AddressGrid.Rows.Item(loopcounter).Cells(7).Value = Addressbook(loopcounter).Email
Next
'Display the data from the file.
End Sub
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Any help would be greatly appreciated. Thanks in advance! (P.S. Apologies for the poor tags, they're probably wrong.)
If you can add some Symbol into the text this process will be easy to fetch Like:
TextFileData = %Your Name,Age,Address%Other Name, Other Age, Other Address,
You can use split ("%") to get the users information
Users = TextFileData.Split("%")
Then use split(",") to extract the information
Information = Users(0),Split(",")
I have written a WinForm project which displays a ListBox containing a list of file names. When the user clicks a submit button, the application dynamically loads and displays one PictureBox control for each file and then waits while they are processed. As PDF files are generated for each one, the matching PictureBox for that file needs to be updated to display an image.
Here's what I have so far:
Private Sub ButtonSubmit_Click(sender As System.Object, e As System.EventArgs) Handles ButtonSubmit.Click
Dim x As Integer = 790
Dim y As Integer = 91
For i As Integer = 0 To ListBox1.Items.Count - 1
Dim key As String = ListBox1.Items(i).ToString()
'adds picturebox for as many listbox items added
Dim MyPictureBox As New PictureBox()
MyPictureBox.Name = "pic" + key
MyPictureBox.Location = New Point(x, y)
MyPictureBox.Size = New Size(12, 12)
MyPictureBox.SizeMode = PictureBoxSizeMode.StretchImage
Me.Controls.Add(MyPictureBox)
MyPictureBox.Image = My.Resources.Warning1
ToolTipSpooling.SetToolTip(MyPictureBox, "Creating PDF...")
x += 0
y += 13
Next i
Call CheckPDFs()
End Sub
Public Sub CheckPDFs()
Dim ListboxTicketIDs = (From i In ListBox1.Items).ToArray()
For Each Item In ListboxTicketIDs
Dim ID = Item.ToString
Dim Watcher As New FileSystemWatcher()
Watcher.Path = "C:\Temp\"
Watcher.NotifyFilter = (NotifyFilters.Attributes)
Watcher.Filter = ID + ".pdf"
AddHandler Watcher.Changed, AddressOf OnChanged
Watcher.EnableRaisingEvents = True
Next
End Sub
Private Sub OnChanged(source As Object, e As FileSystemEventArgs)
Dim p As PictureBox = CType(Me.Controls("pic" + ListBox1.Items.ToString()), PictureBox)
p.Image = My.Resources.Ok1
End Sub
I'm having trouble changing the PictureBox to a different picture once the item(s) listed in the listbox are present, based on the FileSystemWatcher. For instance, the files are not always created in the same order as they exist in the ListBox.
EDIT
Working code below.
Public Class Form1
Private WithEvents Watcher As FileSystemWatcher
Public Sub CheckPDFs()
For i As Integer = 0 To ListBox1.Items.Count - 1
Watcher = New FileSystemWatcher()
Watcher.SynchronizingObject = Me
Watcher.Path = "C:\Temp\"
Watcher.NotifyFilter = NotifyFilters.Attributes
Watcher.Filter = "*.pdf"
Watcher.EnableRaisingEvents = True
Next
End Sub
Private Sub Watcher_Changed(ByVal sender As Object, ByVal e As FileSystemEventArgs) Handles Watcher.Changed
Dim key As String = Path.GetFileNameWithoutExtension(e.Name)
Dim p As PictureBox = CType(Me.Controls("pic" + key), PictureBox)
p.Image = My.Resources.Ok
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Add(TextBox1.Text)
TextBox1.Text = ""
Dim x As Integer = 5
Dim y As Integer = 5
For i As Integer = 0 To ListBox1.Items.Count - 1
Dim key As String = ListBox1.Items(i).ToString()
'adds picturebox for as many listbox items added
Dim MyPictureBox As New PictureBox()
MyPictureBox.Name = "pic" + key
MyPictureBox.Location = New Point(x, y)
MyPictureBox.Size = New Size(15, 15)
MyPictureBox.SizeMode = PictureBoxSizeMode.StretchImage
Me.Controls.Add(MyPictureBox)
MyPictureBox.Image = My.Resources.Info
x += 0
y += 18
Next i
Call CheckPDFs()
End Sub
First of all, you don't need to create multiple file watchers. You just need a single file watcher to watch for any changes to the folder. I would recommend declaring it as a private field at the top of your form using the WithEvents keyword so you don't have to worry about adding and removing event handlers.
Next, when the watcher raises the changed event, you can get the file name of the file that changed by looking at the properties of the event args object. You need to get the name of the file that changed and then use the file name as the key to finding the matching picture box control.
Public Class Form1
Private WithEvents Watcher As FileSystemWatcher
Public Sub CheckPDFs()
Watcher = New FileSystemWatcher()
Watcher.Path = "C:\Temp\"
Watcher.NotifyFilter = NotifyFilters.Attributes
Watcher.Filter = "*.pdf"
End Sub
Private Sub Watcher_Changed(ByVal sender As Object, ByVal e As FileSystemEventArgs) Handles Watcher.Changed
Dim key As String = Path.GetFileNameWithoutExtension(e.Name)
Dim p As PictureBox = CType(Me.Controls("pic" + key), PictureBox)
p.Image = My.Resources.Ok1
End Sub
End Class
However, since you say in a comment below that the file name will not be the same as the text in the listbox, but that it will merely start with that text, you could do something like this, instead:
Private Sub Watcher_Changed(ByVal sender As Object, ByVal e As FileSystemEventArgs) Handles Watcher.Changed
Dim p As PictureBox = Nothing
For Each item As Object In ListBox1.Items
If e.Name.StartsWith(item.ToString()) Then
p = CType(Me.Controls("pic" + item.ToString()), PictureBox)
Exit For
End If
Next
If p IsNot Nothing Then
p.Image = My.Resources.Ok1
End If
End Sub
I'm trying to create a program that checks if someone is going to Happy Hour. If not, it lists those who aren't and puts their picture next to their name.
I'm able to achieve all but get the images locally and store them in an array (which would be added to pictureArray(i)).
(You can see the commented out sections are where I've tried to get the images...)
Any ideas?
Public Class Form1
Dim ITLPList() As String = {"Name 1", "Name 2", "Name 3", "Name 4", "Name 5", "Name 6"}
' Dim imageList As New ImageList
' Dim fileSteam As New System.IO.FileStream(sFileName, System.IO.FileMode.Open)
' Dim img As Image
' Dim sFileName As String = "C:\Users\turcotd\Desktop\ITLPers\itlp1.jpg"
Dim itlpTally() As String
Dim labelArray(5) As Label
Dim pictureArray(5) As PictureBox
Dim intTally As Integer
Dim i As Integer = 0
Public itlpIndex As Integer = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
itlpName.Text = ITLPList(0)
labelArray(0) = lblPerson0
labelArray(1) = lblPerson1
labelArray(2) = lblPerson2
labelArray(3) = lblPerson3
labelArray(4) = lblPerson4
pictureArray(0) = picITLP0
pictureArray(1) = picITLP1
pictureArray(2) = picITLP2
pictureArray(3) = picITLP3
pictureArray(4) = picITLP4
End Sub
Private Sub btnYes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnYes.Click
If (i < 6) Then
itlpName.Text = ITLPList(i)
i = i + 1
End If
End Sub
Private Sub btnNo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNo.Click
If (i < 6) Then
'Names
itlpName.Text = ITLPList(i)
intTally = intTally + 1
lblTally.Text = intTally
labelArray(i).Text = ITLPList(i)
'Images
' img = Image.FromStream(fileSteam)
' fileSteam.Close()
' imageList.Images.Add(img)
' pictureArray(i).Image = imageList.Images.Item(0)
' img.Dispose()
' img = Image.FromFile(sFileName)
i = i + 1
End If
itlpName.Text = ITLPList(i)
End Sub
End Class
You could get all jpg's in a directory as FileInfo in the following way:
Dim dir = New IO.DirectoryInfo("C:\Users\turcotd\Desktop\ITLPers")
Dim images = dir.GetFiles("*.jpg", IO.SearchOption.AllDirectories).ToList
Loading the file into the PictureBox from a file is a very straightforward operation:
picITLP0.Image = Image.FromFile("C:\Users\turcotd\Desktop\ITLPers\itlp1.jpg")