I am very new to Visual Basic so please be gentle. :P
I am creating a small application, for basic learning purposes, which will allow a user of the application to update a profile of sorts. This includes, uploading a profile picture, which is then stored in /bin/Debug/Resource and then displayed on their profile in a PictureBox.
I am using the following code, which seems to do just that. However, when I close the application and run it again, the image is not displayed on either PictureBox but is still stored in the designated folder.
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If (Not System.IO.Directory.Exists("Resource")) Then
System.IO.Directory.CreateDirectory("Resource")
End If
Dim OpenFileDialog1 As New OpenFileDialog
With OpenFileDialog1
.CheckFileExists = True
.ShowReadOnly = False
.Filter = "All Files|*.*|Bitmap Files (*)|*.bmp;*.gif;*.jpg"
.FilterIndex = 2
'
If .ShowDialog = DialogResult.OK Then
Dim FName() As String = OpenFileDialog1.FileName.Split("\\")
System.IO.File.Copy(OpenFileDialog1.FileName, "Resource\\" + FName(FName.Length - 1))
PictureBox1.Image = Image.FromFile(.FileName)
Profile.PictureBox1.Image = Image.FromFile(.FileName)
End If
End With
End Sub
Any help that you can provide is greatly appreciated.
Thank you.
This is what you need to do in order to display the picture.
This code will have to be added to the Load form method.
Dim StoredPath As String = "PathToImage"
IF File.Exists(StoredPath) Then
PictureBox1.Image = Image.FromFile(#StoredPath)
PictureBox1.Refresh()
End if
Related
im very new to vb.net. im making a piece of software and im very nearly finished (its my first piece of standalone software). i have a trackbar next to a button, which im trying to use to control how large a selection of this csv file should be, and then it can download it to a seperate file. my issue is im unsure how to parse the file itself and assign variables and do some variable maths and get the selection i need. ive written some pseudocode here to sort of show what im trying to do. any help or pointing in the right direction of what im looking for would be brilliant, thankyou.
Private Sub TrackBar4_Scroll(sender As Object, e As EventArgs) Handles TrackBar4.Scroll
Label44.Text = TrackBar4.Value
Dim MonthSelection As Integer = 3
MonthSelection = Label44.Text
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
If Label32.Text = "C:\" Then
MessageBox.Show("Please select Log from sign first")
Else
Dim fbd As FolderBrowserDialog = New FolderBrowserDialog()
If fbd.ShowDialog() = DialogResult.Cancel Then Exit Sub
Dim outputPath As String = IO.Path.Combine(fbd.SelectedPath, "selection log.txt")
'pseudocode
'parse file with date, time, direction, speed fields in each line
'dim SelectionRangeStart As String = LastLine.Date - MonthSelection(1,2,3,4,5,6)
'dim CSVSelectionRange As String = SelectionRangeStart to LastLine
IO.File.WriteAllText(outputPath, CSVSelectionRange)
MessageBox.Show("Success!")
End If
End Sub
ive searched for how to parse but its very complex and all forums and guides seem to be very specific and a bit difficult to understand. any help at all is greatly appreciated :)
I'm trying to make a YouTube downloader for fun. I saw some tutorials and I finished it, but when I download it and select the path if I choose "Desktop", it doesn't download but if I choose a folder on the desktop, it downloads it but not in the folder, in the desktop. I tried to fix it but nothing worked.
How can I resolve that?
Here's my code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles download.Click
If url.Text <> "" Then
If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
stato.Text = "Downloading"
Dim video = YouTube.Default.GetVideo(url.Text)
FolderBrowserDialog1.RootFolder = Environment.SpecialFolder.DesktopDirectory
File.WriteAllBytes(FolderBrowserDialog1.SelectedPath & video.FullName, video.GetBytes())
stato.Text = "Done!"
End If
Else
MsgBox("Enter an URL!")
End If
End Sub
If the SelectedPath doesn't have the file, go one level up because the file is most likely there. Here's your fix so it goes to the right folder:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles download.Click
If url.Text <> "" Then
FolderBrowserDialog1.RootFolder = Environment.SpecialFolder.DesktopDirectory
If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
stato.Text = "Downloading"
Dim video = YouTube.Default.GetVideo(url.Text)
File.WriteAllBytes(System.IO.Path.Combine(FolderBrowserDialog1.SelectedPath, video.FullName), video.GetBytes())
stato.Text = "Done!"
End If
Else
MsgBox("Enter an URL!")
End If
End Sub
Note that I'd recommend you use a backgroundworker component for the download instead. Also, ideally, you should be saving the bytes to the file in the background worker as you save, so the bytes don't all go into memory, but directly into your file instead. Those recommendations are outside the scope of your question though.
I'm create simple paint program in vb.net, when I'm trying to save the file, the program is freeze, and I can't do anything.
This is my code that I'm used
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
SaveFileDialog1.CreatePrompt = True
SaveFileDialog1.DefaultExt = "jpg"
SaveFileDialog1.Filter = "File Images (*.jpg;*.jpeg;) | *.jpg;*.jpeg; |PNG Images | *.png |GIF Images | *.GIF"
SaveFileDialog1.InitialDirectory = "F:"
If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
FrmCanvas.PictureBox1.Image.Save(SaveFileDialog1.FileName)
End If
End Sub
Did I'm missed something on my code? I'm sorry, I'm new at vb.net
You have missed the file format of your Image
FrmCanvas.PictureBox1.Image.Save(savefiledialog1.FileName,System.Drawing.Imaging.ImageFormat.Jpeg)
To save image on users selected format based on filter is something like this
If SaveFileDialog1.FileName <> "" Then
' Saves the Image in the appropriate ImageFormat based upon the
' file type selected in the dialog box.
' NOTE that the FilterIndex property is one-based.
Select Case SaveFileDialog1.FilterIndex
Case 1
FrmCanvas.PictureBox1.Image.Save(savefiledialog1.FileName,System.Drawing.Imaging.ImageFormat.Jpeg)
Case 2
FrmCanvas.PictureBox1.Image.Save(savefiledialog1.FileName,System.Drawing.Imaging.ImageFormat.Bmp)
Case 3
FrmCanvas.PictureBox1.Image.Save(savefiledialog1.FileName,System.Drawing.Imaging.ImageFormat.Gif)
End Select
End If
Hope that helps you get the idea.
Please visit this for more info.
Try This.
Dim SaveImage As New Bitmap(PictureBox1.Image)
SaveImage.Save(SaveImagePath + SaveImageName, Imaging.ImageFormat.Jpeg)
SaveImage.Dispose()
I'm currently teaching myself (with the help of SO & Google) VB.Net to create a launcher for a multiplayer mod and I need users upon first launch of my application to input where their folder is stored, so far I have;
Dim folderDlg As System.Windows.Forms.FolderBrowserDialog
folderDlg = New System.Windows.Forms.FolderBrowserDialog
folderDlg.Description = "Please select your multiplayer folder"
If My.Settings.isFirstRun Then
My.Settings.isFirstRun = False
My.Settings.Save()
folderDlg.ShowDialog()
Else
End If
The button to run the mod itself
Private Sub Launch_mp_Click(sender As Object, e As EventArgs) Handles Launch_mp.Click
If My.Computer.FileSystem.FileExists("launcher.exe") Then
Process.Start("launcher.exe")
Timer2.Interval = 1000
Timer2.Start()
End If
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
p = Process.GetProcessesByName("eurotrucks2")
If p.Count > 0 Then
Timer2.Stop()
Me.WindowState = FormWindowState.Minimized
Me.Visible = True
Else
End If
End Sub
I'm confused as to how I can store the users selected path and then recall it later on for the button without always asking for the dir.
You are almost there:
You have various options where to store the information: registry, old-style using ini-files or in the config file of your application. I would suggest using the config file since you already store the isFirstRun-varialbe in the config. In project explrorer look at the "My Project" folder and double click an item called "Settings". Add a setting of type string called "ModFolder". After that you will be able to access the value of that setting using My.Settings.ModFolder varialbe (see here).
Use the FolderBrowserDialog to store the folder (see here)
if folderDlg.ShowDialog() = DialogResult.Ok then
My.Settings.ModFoler = folderDlg.SelectedPath
My.Settings.Save
end if
When your application starts next time the ModFolder-variable will automaticall hold the value stored so instead of If My.Settings.isFirstRun Then I would check:
If File.Exists(Path.Combine(My.Settings.ModFolder, "AppToStart.Exe")) then
...
end if
If the file exists launch it, if not re-show the dialog to pick the folder.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim FILE_NAME As String = "C:\KVRequest.txt"
Dim aryText(4) As String
aryText(0) = "TextBox4.Text"
aryText(1) = "TextBox5.Text"
aryText(2) = "TextBox6.Text"
aryText(3) = "TextBox7.Text"
aryText(4) = "TextBox8.Text"
Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
objWriter.Close()
MsgBox("Text file created in your C drive, attach this file in an email to someone#gmail.com Please check that all of the details are correct before sending.")
End Sub
What I am trying to do is get the text from the text boxes (4 5 6 7 8) to write into a text file. The code I have creates the file, but does not write text into it, can anyone give me a tip on how to get this working?
Thanks!
Edit: Also while I am here, I was trying to get it so button_1.enabled was only true if all of the text boxes had been edited, but I could not think of a practical way to do this, if you could help me with this I would also be very grateful!
Given the code posted above, the reason nothing is being written to the file is because you're not telling it to write anything to the file. You would need to add something like this between the creation of your StreamWriter and where you close the close method on it:
objWriter.WriteLine(TextBox4.Text)
objWriter.WtiteLine(TextBox5.Text)
etc...
Also, the simplest option for only enabling the save button is to create a Control.TextChanged handler for each of your text boxes (or use the one Sub to do it for all of them by adding all their events to the one handler method) and have it do something similar to:
If TextBox4.Text <> "" And TextBox5.Text <> "" And TextBox6.Text <> "" Then
Button1.Enabled = True
Else
Button1.Enabled = False
End If