Add Each Generated Code To .txt File - VB.net - vb.net

I Have Button and TextBox. When i click on the button random code generated (working well).
I want each code will be save in the same .txt file. Like this:
my.txt:
RandomCode1
RandomCode2
Random3
I mean when i click on the button the generated code will added to the .txt file (same .txt file for all)
Thank's.
Or (if it's possible)
when i click on the button the program generate random code i need tons like this so maybe i'll click on time and then the program will generate around 100 codes in every single click or the program will not stop until i'll click on another button.
(if it's not easy just help me with the first option.)

If you want to make 100 codes per click you can do a loop like
Dim inc as Integer = 0
Do Until inc = 100
'your code for making code
inc = inc + 1
loop
As for text file saving
Dim outfile As StreamWriter
outfile = File.AppendText("C:\data\codes.txt")
outfile.Write(TextBox1.text)
outfile.Flush()
outfile.Close()

Related

Search text file and put matching results in listbox

I have a VB.NET project in which there is a form where there is a TextBox control, a ListBox control and an external text file that contains a list of outlook folder paths for client emails.
Essentially, the user enters into the text box the name of a client and/or their unique reference number, presses the search button (yes - I know I could make the results appear as they type, I want a button!) and it comes up with the matching results for the company name or serial number that are in the text file and puts them in the list box, with the full path of the outlook email folder.
For example:
If I put into the textbox: "06967759-274D-40B2-A3EB-D7F9E73727D7"
It would put the following result into the listbox:
"EIS Admin\Contacts{06967759-274D-40B2-A3EB-D7F9E73727D7}"
And the user can then go to that folder and find the email(s).
I have gone through several revisions both of my own code and code pasted from online with people having the same issue, only to have Visual Studio throw no errors, run the code and have no luck, with it doing nothing but clearing the list box, and not showing matching results of any kind.
I understand this may be a repeat question but I am extremely confused, can't get anything to work and need some help regarding my issue.
Here is the current code (from online - not mine):
lbx_OFL_Results.Items.Clear()
Dim i As Integer
For i = 0 To lbx_OFL_Results.Items.Count - 1
If i > lbx_OFL_Results.Items.Count - 1 Then Exit For
If Not lbx_OFL_Results.Items(i).Contains(tbx_FindText.Text) Then
lbx_OFL_Results.Items.Remove(lbx_OFL_Results.Items(i))
i -= 1
End If
Next
The list box is called "lbx_OFL_Results"
The textbox is called "tbx_FindText"
I start by clearing the list box of all items (when the form loads, it fills the list box will all lines of the text file, so I need to clear it).
Form Load Event Code:
Dim lines1() As String = IO.File.ReadAllLines("C:\ProgramData\WPSECHELPER\.data\Outlook Folder Wizard\outlookfolders.txt")
lbx_OFL_Results.Items.AddRange(lines1)
For the rest of the code it seems to be doing some form of a 'sort search' then removing any excess results.
If anyone can suggest edits to my code, or new code then that would be sublime.
Thanks.
Thanks to #Jimi for the answer.
Code:
listbox.Items.Clear()
listbox.BeginUpdate()
For i as Integer = 0 To lines1().Length - 1
If lines1(i).Contains(searchbox.Text) Then
listbox.Items.Add(lines1(i))
End If
Next
listbox.EndUpdate()
I have another question which solves how to make this search non case-sensitive. It can be found here.

Take a capture of form

Problem : Form is divided in two parts.
Left side for enter information's. Right side for preview those information's.
On top of form there is a button called Send.
When i click on that button it need to take a snapshot of the right side and send it to email address provided as a attachment.
Solution : Take a snapshot of right side of form and save it to Temp folder
- Send a email and attach the file from temp folder
- Delete the file ( for any case )
Is it possible to take a ss and convert it to pdf ? is that a better way ?
I found a very helpfull class on forum Class for Screen Capture
But is there any way to use method without entering position of the screen.
Example : Right side of form is at GroupBox2 .
Is there any way to take a snapshoot of the groupbox2 on form?
Is there any way to take a snapshoot of the groupbox2 on form?
and save it to Temp folder
As suggested by user3697824, use Control.DrawToBitmap() with Path.GetTempFileName:
Dim bmp As New Bitmap(GroupBox2.Width, GroupBox2.Height)
GroupBox2.DrawToBitmap(bmp, New Rectangle(New Point(0, 0), bmp.Size))
Dim FileName As String = System.IO.Path.GetTempFileName
bmp.Save(FileName) ' save it as a Bitmap
' < or >
bmp.Save(FileName, Imaging.ImageFormat.Jpeg) ' save it as a Jpeg
' *Either way, the file will have the .TMP file extension!
Debug.Print(FileName)

How to save changes of a saved text file in VB.NET

I have a text editor that can open text files .txtand puts the text in a textbox. The user can also save the text to a .txt file.
How can the user save the changes of that text file that was saved recently?
Also, If the user opened a txt file how can the text editor change the text file to what the user changed?
Hope you understood that. Thanks in advance.
Since I don't know what you've done so far I will take this from the beginning...
For this you will need these components:
A SaveFileDialog
An OpenFileDialog
Three buttons (or menuitems), labeled: "Save", "Save As", "Open"
And I actually recommed using a RichTextBox instead of a regular TextBox.
.
To start, we put a variable in the code to know what file was saved before:
Dim LatestFile As String
Then, go to the properties of your Save- and OpenFileDialog and put this in the Filter field:
Text files (*.txt)|*.txt
Then you double-click the SaveFileDialog which should write the SaveFileDialog_FileOk event in your code. There you put:
LatestFile = SaveFileDialog1.FileName
RichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.PlainText)
And then double-click the OpenFileDialog, and enter this code:
LatestFile = OpenFileDialog1.FileName
RichTextBox1.LoadFile(OpenFileDialog1.FileName, RichTextBoxStreamType.PlainText)
And then for the buttons:
The "Open" button:
OpenFileDialog1.ShowDialog()
The "Save" button:
If Not LatestFile = "" Then
RichTextBox1.SaveFile(LatestFile, RichTextBoxStreamType.PlainText)
Else
SaveFileDialog1.ShowDialog()
End If
And the "Save As" button:
SaveFileDialog1.ShowDialog()
.
Hope this helps!
If I understood your question: you want your text editor application to detect changes that happened to the source text file that it is currently viewing to the user and if it detects a change, it should update its text box to display the latest file for the user.. am I right?
If that is the case, one way to do that is to use a Timer. Every time the timer tick event is triggered, you should check the date of the last modification to the file. If it is greater than the date that you have checked while opening the file for the first time, then the file had gone through some changes. Reload the file contents into the text box.
Another way (without using Timers) is to only check for the modification date once the application window is activated. Since the user will have to change the focus of the program to, say Notepad to do changes to the text file, then once the user returns focus to the Text Editor Application, use the Window Activate or Click event to check the file modification date.

Formatting a file path text in VB for textbox

Lets say i have 2 buttons "btnBrowse" and "btnImport", i also have a label "lbPath" and a textbox "txtSubject"
When i click browse, the application allows me to look for a file and import the data to a grid view. So i click btnBrowse, find the excel file, double click it, then click btnImport. 4 things happen when i click "btnImport"
1: lbPath.Text changes to the path of the file i picked "C:\Users\me\Desktop\excel.xlsx"
2: The gridview loads the excel file data
3: txtSubject.Text changes to the path of the file as well "C:\Users\me\Desktop\excel.xlsx"
4: the path name becomes the variable "spath" as a string
I would like 1 and 2 to stay the same but for txtSubject.Text, i would like it to grab the last part of the path so the results would be as follows
1: lbPath.Text = "C:\Users\me\Desktop\excel.xlsx"
2: The gridview loads the excel file data
3: txtSubject.Text = "excel.xlsx"
Any ideas?
Here is a way to do this:
txtSubject.Text = System.IO.Path.GetFileName(lbPath.Text)

How to Open Notepadd ++ With Textbox.text?

I have a small program that I am working on that grabs a script within an object model. I can get the script to display in the text box and I can get notepad++ to launch on button click.
What I would love to do is have the text from the text box open inside notepad++ so that i may edit the script.
Does anyone out there have any ideas?
Save the text from your text box to a file, like this:
System.IO.File.WriteAllText("path and name of text file.txt", textBox1.Text)
Now you can force the user to use Notepad, by doing this:
Process.Start("path to notepad.exe", "path and name of text file.txt")
Or you can have the system determine what the user's preferred application (say they have Notepad++) is for a .txt by doing this:
System.Diagnostics.Process.Start("path and name of text file.txt")