Label move the position on its own - vb.net

This is maybe stupid, but I seriously don't know why it does this:
As you can see in the gif, the first proxy is normally placed, but all other proxies went to new line.
I am importing proxies like this:
Dim ofd As New OpenFileDialog With {.Filter = "Text Files (.txt)|*.txt"}
If ofd.ShowDialog = vbOK Then
Dim sr As IO.StreamReader = New IO.StreamReader(ofd.FileName)
proxies = sr.ReadToEnd
list = proxies.Split(Environment.NewLine)
End If
In timer I have this:
Label6.Text = list(ProxyIndex)
UseProxy(list(ProxyIndex))
'here is where I am navigating to the website'
ProxyIndex += 1

Try this (Fixed):
Label6.Text = list(ProxyIndex).Trim
If that does not works, then try this else:
Label6.Text = list(ProxyIndex.replace(Environment.NewLine, string.empty))

Related

Open Explorer and redirect to specific path after SaveFileDialog

I create a simple function to download a file from the URL and then save it to some folder using SaveFileDialog and then the program will open Windows Explorer to the path of the saved file.
but I don't know how to get the last path of the SaveFileDialog
here is my code :
Dim path = "myURL"
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.FileName = dgvAttachmentName & dgvFileExtensi
saveFileDialog1.RestoreDirectory = True
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
Using client As New WebClient()
client.DownloadFile(path, saveFileDialog1.FileName)
Process.Start("explorer.exe", "/root," & saveFileDialog1.FileName)
End Using
End If
If I use SaveFileDialog1.FileName, I get the full path of the File but with a FileName, but I also can't use replace to remove the file name from the path because User can change the file name every time they want to save a file.
How to get the path only from the SaveFileDialog then open explorer to that path ?
Try this:
Dim fi As New System.IO.FileInfo(saveFileDialog1.FileName)
Dim Path = fi.DirectoryName
all together:
Dim path = "myURL"
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.FileName = dgvAttachmentName & dgvFileExtensi
saveFileDialog1.RestoreDirectory = True
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
Using client As New WebClient()
client.DownloadFile(path, saveFileDialog1.FileName)
Dim fi As New System.IO.FileInfo(saveFileDialog1.FileName)
Dim Path = fi.DirectoryName
Process.Start("explorer.exe", "/root," & Path )
End Using
End If

Skipping a line of code if an image isn't selected

If a user selects an image it will save the image as per the below code:
PictureBox1.Image.Save(PicFile, System.Drawing.Imaging.ImageFormat.Jpeg)
Now if the user decides not to select an image, is there a way to skip this code? I currently get the following error:
Additional information: Object reference not set to an instance of an object.
This is what I've tried:
Dim opf As New OpenFileDialog
opf.Filter = "Choose Image(.jpg;.png;*.gif)|*.jpg;*.png;*.gif"
If opf.ShowDialog = DialogResult.OK Then
PictureBox1.Image = Image.FromFile(opf.FileName)
End If
Try
Dim ms As New MemoryStream
PictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim img As Byte()
img = ms.ToArray()
DataGridView1.Rows.Add(img)
Catch ex As Exception
MessageBox.Show(ex.Message.ToString())
End Try
Now if the user decides not to select an image, is there a way to skip this code?
If by this you mean the user selects Cancel, then yes. Simply place the Try[...]Catch block into your If statement and it will only attempt the code if the user selects Open.
Something like this:
Dim opf As New OpenFileDialog
opf.Filter = "Choose Image(.jpg;.png;*.gif)|*.jpg;*.png;*.gif"
If opf.ShowDialog = DialogResult.OK Then
PictureBox1.Image = Image.FromFile(opf.FileName)
Try
Dim ms As New MemoryStream
PictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim img As Byte()
img = ms.ToArray()
DataGridView1.Rows.Add(img)
Catch ex As Exception
MessageBox.Show(ex.Message.ToString())
End Try
End If
If you only wanted to skip certain bits of code you could do a check on opf.FileName. As an example:
If opf.FileName <> "" Then
PictureBox1.Image = Image.FromFile(opf.FileName)
End If

FTP Client uploads only 0KB files

I am having a small problem with my FTP client.
Choosing a file works, renaming that file with 4 variables works.
It's the upload that is causing me trouble.
Whenever a file is uploaded to the FTP server it says it is 0KB.
I am thinking of 2 possible problems:
Visual studio tells me that the variable file is used before it has been assigned a value, to make sure it isn't null i did the following.
Dim file As Byte()
If (Not file Is Nothing) Then
strz.Write(file, 0, file.Length)
strz.Close()
strz.Dispose()
FileSystem.Rename(Filename, originalFile)
End If
This Takes care of any possible Errors.
The second one is fName, same warning as with file, and I took care of it the same way.
another possibility is that my code just takes the 4 variables and makes that into a file and uploads it, hence the 0KB size....
Here's my code:
Dim Filename As String
Dim originalFile As String
Private Function enumerateCheckboxes(ByVal path As String)
originalFile = path
Dim fName As String
For Each Control In Me.Controls
If (TypeOf Control Is ComboBox AndAlso DirectCast(Control, ComboBox).SelectedIndex > -1) Then
fName += CStr(Control.SelectedItem.Key) + "_"
End If
Next
Try
fName = path + fName.Substring(0, fName.Length - 1) + ".jpg"
Catch ex As Exception
MsgBox(ex.Message)
MsgBox("Stack Trace: " & vbCrLf & ex.StackTrace)
End Try
Return fName
End Function
Public Function OpenDialog()
Dim FD As OpenFileDialog = New OpenFileDialog()
FD.Title = "Selecteer een bestand"
FD.InitialDirectory = "C:\"
FD.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"
FD.FilterIndex = 2
FD.RestoreDirectory = True
If FD.ShowDialog() = DialogResult.OK Then
Dim Filename As String = FD.FileName
Filename = StrReverse(Filename)
Filename = Mid(Filename, InStr(Filename, "\"), Len(Filename))
Filename = StrReverse(Filename)
MsgBox(enumerateCheckboxes(Filename))
End If
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim request As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ip" & enumerateCheckboxes(Filename)), System.Net.FtpWebRequest)
request.Credentials = New System.Net.NetworkCredential("username", "password")
request.Method = System.Net.WebRequestMethods.Ftp.UploadFile
Dim file() As Byte
Try
Filename = OpenDialog()
If (Not Filename Is Nothing) Then
System.IO.File.ReadAllBytes(Filename)
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
MessageBox.Show("Stack Trace: " & vbCrLf & ex.StackTrace)
End Try
If (Not Filename Is Nothing) Then
FileSystem.Rename(originalFile, Filename)
End If
Dim strz As System.IO.Stream = request.GetRequestStream()
If (Not file Is Nothing) Then
strz.Write(file, 0, file.Length)
strz.Close()
strz.Dispose()
FileSystem.Rename(Filename, originalFile)
End If
End Sub
End Class
I have looked at multiple threads with the same problem as me.
Threads like this
But i dont believe this applies to my problem.
If you would be so kind to explain what i did wrong and how i can fix and avoid this in the future, my debugging is still a bit rough...
Thank you in advance!
Visual Studio is giving you that warning because you never assign anything to the file array. I think that on the line where you have:
System.IO.File.ReadAllBytes(Filename)
You really meant to have:
file = System.IO.File.ReadAllBytes(Filename)

Change .SaveFile to save as keeping the file format

How can I turn this statement into a "save as" dialog box?
Me.TextBox4.SaveFile(System.Environment.GetFolderPath(Environment.SpecialFolder.MyComputer) + "\MyDocs\Test.xml", RichTextBoxStreamType.UnicodePlainText)
I need to preserve this format since it is the only one that worked properly when file is saved.
Thanks.
You can try something like this. Create a SaveFileDialog and pass it all the parameters for the default locations and file names. Create a new file stream based on your file (creating or overwriting) and passing that stream to the SaveFile method of the RichTextBox
Using sfd As New SaveFileDialog()
sfd.AddExtension = True
sfd.Filter = "*.xml|*.xml"
sfd.OverwritePrompt = True
sfd.DefaultExt = ".xml"
sfd.CreatePrompt = False
sfd.InitialDirectory = Path.Combine(Environment.SpecialFolder.MyComputer, "\MyDocs\")
sfd.FileName = "Test.xml"
If sfd.ShowDialog = Windows.Forms.DialogResult.OK AndAlso sfd.FileName <> String.Empty Then
Using sf As New FileStream(sfd.FileName, FileMode.Create)
TextBox4.SaveFile(sf, RichTextBoxStreamType.UnicodePlainText)
End Using
End If
End Using

cancel throws error in OpenFileDialog box

I'm trying to implement an OpenFileDialog box, its works fine except if I choose to click cancel then program throws an error, saying that file can't be found, which confuses me cause I didnt select a file.
The following is the code. how I can implement the cancel button?
OpenFileDialog1.InitialDirectory = "C:\"
OpenFileDialog1.FileName = "Select a Batch file..."
OpenFileDialog1.Filter = "Batch files (*.bat) | *.bat"
OpenFileDialog1.ShowDialog()
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.Cancel Then
OpenFileDialog1.Dispose()
End If
Dim R As New IO.StreamReader(OpenFileDialog1.FileName)
TextBox4.Text = R.ReadToEnd
R.Close()
Button4.Enabled = True
Button6.Enabled = True
You commented out the (inadequate) handling of cancelling the dialog. Put it back in:
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.Filter = "Batch files (*.bat)|*.bat|All files|*.*"
Dim result = openFileDialog1.ShowDialog()
If result = DialogResult.Cancel Then
Return ' Just leave the method
End If
' … rest of method
You should also think about proper variable names. OpenFileDialog1, TextBox3 and Button2 are never appropriate names. Good identifiers increase the readability of your code tremendously.
Dialog will dispose itself in both cases - you simply don't do anything if user cancels his intended action. This should do it:
OpenFileDialog1.InitialDirectory = "C:\"
OpenFileDialog1.FileName = "Select a Batch file..."
OpenFileDialog1.Filter = "Batch files (*.bat) | *.bat"
OpenFileDialog1.ShowDialog()
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim R As New IO.StreamReader(OpenFileDialog1.FileName)
TextBox4.Text = R.ReadToEnd
R.Close()
Button4.Enabled = True
Button6.Enabled = True
End If
Of course you will have to add some additional error handling but that is another story.
Dim result = OpenFileDialog1.ShowDialog()
If result = True Then
Dim R As New IO.StreamReader(OpenFileDialog1.FileName)
TextBox4.Text = R.ReadToEnd
R.Close()
Button4.Enabled = True
Button6.Enabled = True
else
' handle the error, e.g. msgbox (no vaild file chosen"
End If
This is what worked for me for my project.
Dim bResult As DialogResult = sfdReportFile.ShowDialog()
If bResult = DialogResult.OK Then
tbFilePathName.Text = sfdReportFile.FileName.ToString
End If
You will need to define the result as a DialogResult to check if it was OK and send the file path to whatever you needed it for.
On my project, I used the SaveFileDialog. If the user closed the dialog window, then there is no file name, and an error occurs. With my below code, my process wont run unless there is a file name to use.
If SaveFileDialog1.FileName = Nothing Then
Else
Code to run here when a file name is selected.
End If
The same thing can be run for the OpenFileDialog. Just add an if then to check if a filename has been saved, if not, don't run the code.