I have a pictureBox on my form along with two buttons (Back and Forward) but I can not find a viable method of doing what I wish to do: Scrolling through images in a folder like the default Windows Picture Viewer does with Arrow Keys.
Is there an efficient way to do this?
I'm using Visual Basic .NET with Visual Studio 2010, if that matters.
You'll need to load the pictures using DirectoryInfo, then browse through them with an index. Here is an example:
Public Class Form1
Private files As List(Of FileInfo)
Private currentFileIndex As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
RefreshFolder("c:\path\to\your\pictures")
End Sub
Private Sub backButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles backButton.Click
Advance(-1)
ShowCurrentFile()
End Sub
Private Sub forwardButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles forwardButton.Click
Advance(1)
ShowCurrentFile()
End Sub
Private Sub Advance(ByVal delta As Integer)
currentFileIndex = ((currentFileIndex + files.Count) + delta) Mod files.Count
End Sub
Private Sub RefreshFolder(ByRef path As String)
Dim di As DirectoryInfo = New DirectoryInfo(path)
files = (From c In di.GetFiles()
Where IsFileSupported(c)
Select c).ToList()
If files.Count > 0 Then
currentFileIndex = 0
End If
ShowCurrentFile()
End Sub
Private Sub ShowCurrentFile()
If currentFileIndex <> -1 Then
Try
PictureBox1.Image = Image.FromFile(files(currentFileIndex).FullName)
Catch ex As Exception
' TODO: handle exceptions gracefully
Debug.WriteLine(ex.ToString)
End Try
End If
End Sub
Private Function IsFileSupported(ByRef file As FileInfo) As Boolean
Return file.Extension = ".jpg" Or file.Extension = ".png" ' etc
End Function
End Class
you should be more specific.
if it will help you you have to create two subrotines that assign the next and pervious image to the picture box and triggier these subrotines on the key down events and the bottons clicks.
Related
I'm trying to use eventwathcer but its not working with 11 characters process name (AbcdEfghIII.exe). If I write 10 characters process name (AbcdEfgIII.exe) its working. Is there character limit or am I doing something wrong? Here is my code:
Imports System.Management
Public Class Form1
Dim WithEvents StopWatch As New ManagementEventWatcher(New WqlEventQuery("SELECT * FROM Win32_ProcessStopTrace"))
Private Sub StopWatch_EventArrived(ByVal sender As Object, ByVal e As System.Management.EventArrivedEventArgs)
If e.NewEvent.Properties("ProcessName").Value.ToString = "AbcdEfghIII.exe" Then
MsgBox("Closed")
End If
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
StopWatch.Stop()
RemoveHandler StopWatch.EventArrived, AddressOf StopWatch_EventArrived
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
AddHandler StopWatch.EventArrived, AddressOf StopWatch_EventArrived
StopWatch.Start()
Catch g As Exception
MsgBox("Please, run as admin.", MsgBoxStyle.Critical, "Error")
Me.Close()
End Try
End Sub
End Class
As there doesn't seem to be a way to make this work properly, I suppose you'll just have to use a hacky workaround: Checking if the process name starts with Customization.
Doing so will of course make it react to processes like CustomizationWHATEVER.exe, but since the code you're using for some reason doesn't return the full name there is no other way if you want it to work.
Using your initial code:
If check.StartsWith("Customization", StringComparison.OrdinalIgnoreCase) Then
MessageBox.Show("Closed")
End If
PREVIOUS ANSWER:
Here is a workaround:
Get the ProcessID field instead of ProcessName, and use that together with Process.GetProcessById() to get a .NET friendly Process class instance (from which you can extract the name).
Private Sub StopWatch_EventArrived(ByVal sender As Object, ByVal e As System.Management.EventArrivedEventArgs)
Dim PID As Integer = CType(CType(e.NewEvent.Properties("ProcessID").Value, UInteger) And Integer.MaxValue, Integer)
Dim p As Process = Process.GetProcessById(PID)
'NOTE: The ".exe" part is excluded in Process.ProcessName.
If String.Equals(p.ProcessName, "Customization", StringComparison.OrdinalIgnoreCase) Then
MessageBox.Show("Closed")
End If
End Sub
I need to write this small program in visual basic, but i face a problem which is i can't change the color or add list form text file during runtime.
this is picture of what i wont to do
http://i62.tinypic.com/30tghh0.png
And this is the code i wrote so far,,
Public Class Form1
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
Dim listBox1 As New System.Windows.Forms.ListBox
listBox1.Text = "New List"
listBox1.Location = New Point(25, 25)
listBox1.Size = New Size(380, 280)
Me.Controls.Add(listBox1)
OpenToolStripMenuItem.Enabled = True
SaveToolStripMenuItem.Enabled = True
SaveAsToolStripMenuItem.Enabled = True
CloseToolStripMenuItem.Enabled = True
EditToolStripMenuItem.Enabled = True
End Sub
Private Sub ExirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExirToolStripMenuItem.Click
End
End Sub
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
If (OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
Dim myfile As String = OpenFileDialog1.FileName
Dim allLines As String() = File.ReadAllLines(myfile)
For Each line As String In allLines
ListBox1.Items.Add(line)
Next
End If
End Sub
End Class
The problem as you see is in OpenToolStripMenuItem sub with line ListBox1.Items.Add(line)
is there is no listbox1 because is not created yet.the same with color, save and the rest.
so, please help me to solve it.
Move the declaration of ListBox1 out to the Class level:
Public Class Form1
Private ListBox1 As System.Windows.Forms.ListBox
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
ListBox1 = New System.Windows.Forms.ListBox
...
End Sub
...
End Class
*But why create it at run-time like this? You could add it at design-time and set the Visible() property to False. When the New button is clicked, you change Visible() to True. Unless you need a new ListBox to be created each time so you have more than one? If so, how will they be laid out?...
I was trying to code a download manager but get this error:
'save' is not declared. It maybe inaccessible due to its protection level
Here is my code:
Imports System.Net
Public Class Form1
Private WithEvents httpclient As webClient
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ListBox1.Items.Add(TextBox1.Text)
TextBox1.Clear()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SaveFileDialog1.ShowDialog()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Try
httpclient = New WebClient
Dim total As Integer = ListBox1.Items.Count
Dim current As Integer = -1
While current < total
ListBox1.SelectedIndex = current + 1
Dim download As String = ListBox1.SelectedItem
httpclient.DownloadFileAsync(New Uri(download), save)
Label3.Text = "Current Status: Downloading:..."
Do While httpclient.IsBusy
Application.DoEvents()
Loop
current = current + 1
End While
Catch ex As Exception
ListBox1.Items.Clear()
End Try
End Sub
End Class
DownloadFileAsync takes 2 parameters. The first is the URL of the file to download and the second is the local location to save the file. The tutorial you were following likely set the variable Save somewhere in the code.
To get this to work, you can replace it with something like "C:\Test.txt" to download it to that location or define the variable Save and set it's value to the location you want to save the file.
With your existing code, to allow a user to select the save location, you need to make the following changes. Declare the Save variable private to the form, under the Public Class Form1 line:
Private Save as string
Then in your Button1_Click event, it should look more like this:
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
Save = SaveFileDialog1.FileName
End If
Dim download As String = ListBox1.SelectedItem
httpclient.DownloadFileAsync(New Uri(download), "save")
I successfully use webbrowser control from VB.NET to show and print documents but I can't get zoom functionality which I would like to apply.
For example you can create a new project, add a form with "Webbrowser1" control and two buttons "btn_Plus" and "btn_Minus".
Don't forget to add COM reference to "Microsoft Internet Controls".
Option Strict Off 'because of late binding MyWeb.ExecWB
Imports SHDocVw
Public Class Form1
Dim zoomvalue As Integer = 50
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strHtml As String = "<HTML><h1>Doc...<h1></br><h4>My html here</br>acts like web page.<h4></HTML>"
Me.WebBrowser1.Navigate("about:" + strHtml)
End Sub
Private Sub zoom()
Try
Dim MyWeb As Object = Me.WebBrowser1.ActiveXInstance
MyWeb = Me.WebBrowser1.ActiveXInstance
MyWeb.ExecWB(OLECMDID.OLECMDID_OPTICAL_ZOOM, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, zoomvalue, IntPtr.Zero)
MyWeb = Nothing
Catch ex As Exception
'MessageBox.Show("Error:" & ex.Message)
End Try
End Sub
Private Sub btn_Plus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Plus.Click
zoomvalue += 5
zoom()
End Sub
Private Sub btn_Minus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Minus.Click
zoomvalue -= 5
zoom()
End Sub
End Class
1) Why this won't work and I think it should work?
2) How to get this to work?
3) How to get current zoom value from actual webbrowser?
Try MyWeb.ExecWB(OLECMDID.OLECMDID_OPTICAL_ZOOM, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, CObj(zoomvalue), CObj(IntPtr.Zero))
I'm using vb.net and was wondering how to show what file is being extracted when extracting an archive. I've already have a workaround but it's "cheaty" and doesn't really display the current file that is extracting.
Public Class Form1
Private listItemIndex As Integer = 0
Public Sub ext_Extracting(ByVal sender As Object, ByVal e As SevenZip.ProgressEventArgs)
Dim ext As SevenZipExtractor = New SevenZipExtractor("C:\Test.7z")
If listItemIndex < ext.ArchiveFileNames.Count Then
Label1.Text = ext.ArchiveFileNames.Item(listItemIndex).ToString()
listItemIndex += 1
End If
End Sub
End Class
This answer was written by the OP but incorrectly edited into the question:
Ok I got it. I had to add "FileInfo.FileName" into "SevenZip.FileInfoEventArgs" then make a reference to it when extracting.
Public Sub FileExtractionStarted(ByVal sender As Object, ByVal e As SevenZip.FileInfoEventArgs)
Label1.Text = e.FileInfo.FileName
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SevenZipExtractor.SetLibraryPath("C:\7z.dll")
Dim ext As SevenZipExtractor = New SevenZipExtractor("C:\test.7z")
ext.BeginExtractArchive("C:\Test")
AddHandler ext.ExtractionFinished, AddressOf ext_ExtractionFinished
AddHandler ext.Extracting, AddressOf ext_Extracting
AddHandler ext.FileExtractionStarted, AddressOf FileExtractionStarted '<---
End Sub