Issue Creating Text File Via Visual Basic (Not writing text) - vb.net

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

Related

how to a selection of data from a csv based on dates within the csv

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 :)

Update one form based on selections from another form

I apologise if the title is a bit vague, i've only been on here a day.
So my problem is I have a menu form in which I input the options from the comboboxes. And then I go to the next form which shows the relevant imported text file info.
However when I click the 'back' button to return to the menu and input different information in the comboboxes, it doesn't take me to the correct text file info, it just shows the info from the previous selection.
here is the student menu pic
here is the text file form
below is the code for the student menu next button:
If OptionBox.Text = "Introduction" Then
Introduction.Show()
Else
If OptionBox.Text = "Explanation" Then
Explanation.Show()
End If
End If
End Sub
below is the code for the text file form load page and the back button
Private Sub Introduction_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Student_Menu.Hide()
Dim font As New System.Drawing.Font("Calibri", 11)
If Student_Menu.TopicSelect.Text = "Computer Systems" Then
Dim strFile As String = "C:\Users\Sales\Documents\Visual Studio 2010\Projects\gcsecomputingtask\textfiles\Introduction\ComputerSystems.txt"
Dim sr As New IO.StreamReader(strFile)
IntroductionLabel.Text = sr.ReadToEnd()
sr.Close()
Else
If Student_Menu.TopicSelect.Text = "Hardware" Then
Dim strFile As String = "C:\Users\Sales\Documents\Visual Studio 2010\Projects\gcsecomputingtask\textfiles\Introduction\Hardware.txt"
Dim sr As New IO.StreamReader(strFile)
IntroductionLabel.Text = sr.ReadToEnd()
sr.Close()
Else
If Student_Menu.TopicSelect.Text = "Software" Then
Dim strFile As String = "C:\Users\Sales\Documents\Visual Studio 2010\Projects\gcsecomputingtask\textfiles\Introduction\Software.txt"
Dim sr As New IO.StreamReader(strFile)
IntroductionLabel.Text = sr.ReadToEnd()
Else
If Student_Menu.TopicSelect.Text = "Representation of Data" Then
Dim strFile As String = "C:\Users\Sales\Documents\Visual Studio 2010\Projects\gcsecomputingtask\textfiles\Introduction\RepresentationOfData.txt"
Dim sr As New IO.StreamReader(strFile)
IntroductionLabel.Text = sr.ReadToEnd()
Else
If Student_Menu.TopicSelect.Text = "Databases" Then
Dim strFile As String = "C:\Users\Sales\Documents\Visual Studio 2010\Projects\gcsecomputingtask\textfiles\Introduction\Databases.txt"
Dim sr As New IO.StreamReader(strFile)
IntroductionLabel.Text = sr.ReadToEnd()
Else
If Student_Menu.TopicSelect.Text = "Communications & Networks" Then
Dim strFile As String = "C:\Users\Sales\Documents\Visual Studio 2010\Projects\gcsecomputingtask\textfiles\Introduction\Hardware.txt"
Dim sr As New IO.StreamReader(strFile)
IntroductionLabel.Text = sr.ReadToEnd()
End If
End If
End If
End If
End If
End If
IntroductionLabel.Font = font
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
Me.Hide()
Student_Menu.Show()
Student_Menu.TopicSelect.ResetText()
Student_Menu.OptionBox.ResetText()
End Sub
what do i need to do in order to update this information so that the program doesn't skip going through the form again.
There is a lot of repeated code there. Here is a way to reduce it (see DRY) and expose a method to change the topic. In a Module:
Public Enum Topics
ComputerSystems
Hardware
Software
Data
Database
Networks
End Enum
Then in the form that shows the text:
Friend Sub DisplayTopic(topic As Topics)
Dim text As String
Dim filname As String = ""
Select Case topic
Case Topics.ComputerSystems
filname = "ComputerSystems.txt"
Case Topics.Database
filname = "Databases.txt"
'... etc
End Select
' attach path
filname = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
"gcsecomputingtask", filname)
text = File.ReadAllText(filname)
IntroductionLabel.Text = text
End Sub
By the way, VB does has an ElseIf which can avoid the "arrow" anti pattern you can see in your code. At the very least, the excessive indentation is annoying.
If topic = Topics.ComputerSystems Then
'...
ElseIf topic = Topics.Software Then
'...
End If
Show that form normally using an instance of the form class:
Public Class MenuForm ' a form is just a class
' declare an object variable to use
Private info As Form2 ' whatever its name is (Explanation???)
....
Private Sub MenuForm_Load(...)
' create an instance to be used later:
info = New Form2
Then invoke the method to tell it which topic to display. Displaying topic info is a separate method from loading a form first because the form load event happens only once. A specialized method to do what we want makes more sense since they really have nothing to do with one another, and makes it easier to see how the code works:
info.DisplayTopic(Topics.ComputerSystems)
info.Show
This way, you dont have one form fiddling with the controls on another, but still have a clear way of communicating which topic to display.
Note that the location of the topics file(s) is a bit different. You'd want a "gcsecomputingtask" folder in MyDocuments for the files. The VS project folder is not a good place for it, the folder location could change depending on which machine you are running on (yours or computer lab etc). They could also be stored as a resource to skip that part too.

How to save images file in vb.net?

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()

VB.NET Set Default Text Editor

I have created my own text editor in Visual Basic 2013. I want to open text files with it from outside the application: to open them from the Desktop with double click or right click and open with.
I tried to use right click and open with but it doesn't work, it just opens up my application.
How I make my text editor the one that I open text files with?
You would have to use something like the Environment.GetCommandLineArgs method.
Put this in your form load event:
Dim CommandLineArguments() As String = Environment.GetCommandLineArgs()
If CommandLineArguments.Length >= 2 AndAlso String.IsNullOrEmpty(CommandLineArguments(1)) = False AndAlso IO.File.Exists(CommandLineArguments(1)) Then
Me.TextBox1.Text = IO.File.ReadAllText(CommandLineArguments(1))
End If
This will get the command line arguments sent to your application (which is the path to the file you are trying to open with your app) and check if the argument is an existing file. If so, it will read all the file's text into your TextBox.
Write this code in the form load event.
Private Sub form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim fname As String = Command$()
If Not fname = "" Then
fname = Replace(fname, Chr(34), "")
Dim obj As New System.IO.StreamReader(fname.ToString)
RichTextBox1.Rtf = obj.ReadToEnd
obj.Close()
Me.Text = "Your Application Name " & fname
End If
End Sub

Folder name to combobox in VB.NET

I wanted my combo box to fill in runtime by checking all directory name in a directory.
Here is my code :
Private Sub EDITFORM_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call CLEAR()
End Sub
Private Sub CLEAR()
qtytb.Enabled = False
parttb.Clear()
qtytb.Clear()
DTCB.Items.Clear()
MTHCB.SelectedIndex = ""
YRCB.SelectedIndex = ""
RadioButton2.Checked = True
RadioButton1.Checked = True
TextBox1.Clear()
TextBox2.Clear()
>> Dim di As New DirectoryInfo("D:\DATABASE\" & Pick_Item.deptlbl.Text)
If di.Exists = True Then
For Each subDirectory As DirectoryInfo In di.GetDirectories()
YRCB.Items.Add(CInt(subDirectory.Name.ToString))
Next
End If<<
End Sub
That is my complete code to the combobox and load form
But when i debug, it just appear a messagebox with error so i dont know where exactly is my error in the code
"Conversion from "" string to integer is not valid"
Some thing like this. So, how can i add the folder name to my combobox? Exactly my Directories name are using Integer (Cause name it by year), if i add Directories by name(Not integer) it can. Any help would be appreciated
Your problem is in the TOP line
Dim folders() As String = IO.Directory.GetDirectories("D:\DATABASE\")
For Each folder As String In folders
Combobox1.Items.Add(folder)
Next
Update
Is there any other code which is present in the subroutine/event but is not included above? I tried it and it works fine even if the folder names are numbers
I think the problem is in these lines
MTHCB.SelectedIndex = ""
YRCB.SelectedIndex = ""
change them to
MTHCB.SelectedIndex = -1
YRCB.SelectedIndex = -1