Visual Basic 2010 - How to append certain areas of a text document using data collected in a form - vb.net

I routinely reuse code for Cisco routers / switches and some web accelerators. I usually have to open the text document, edit a few IPs, host names, license keys etc. The remaining 98% of the text document stays the same. My end goal is to develop a program in Visual Basic (or whatever anyone can recommend would accomplish this easier) that will allow me to generate a text document with their data in the right place.
I have been able to develop a GUI form, it looks exactly how I want. I have a box to enter their hostname, classification, IP address etc; now, when I press the "Generate" button at the bottom, I want a new text document on the desktop. When you open the txt, all of their data has been appended to the document in the exact places it should be and all of my original text is still there, just the specific parts have been changed.
I've got the gui, just dont know where to add my existing code and how to make the different objects change their respective parts.
Hopefully this all makes sense. Thank you in advance, I really appreciate it.
Josh

Here is a quick and dirty example of my comment, using String.Replace to replace the placeholders. See if this helps you.
Imports System.IO
Public Class Form1
Private Sub generate_Click(sender As System.Object, e As System.EventArgs) Handles generate.Click
Dim temp As String = System.IO.File.ReadAllText("C:\temp\template.txt")
temp = temp.Replace("##placeHolder1##", TextBox1.Text) _
.Replace("##placeHolder2##", TextBox2.Text) _
.Replace("##placeHolder3##", TextBox3.Text)
File.WriteAllText(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\custom.txt", temp)
End Sub
End Class
To answer your question about embedding the file into the application. Yes it is possible just go to Project --> Properties --> Resources click Add Resource and select Add Existing File (Just make sure that your template has a .txt file extension, you can rename it to what you want when you save the file), then when you go to your solution explorer you will see a directory called Resources with template.txt in it. right click on it and select properties then select the Build Action Property to Embedded Resource this will embed the file into the program. You will then be able to access the template like this.
Dim temp As String = My.Resources.template

Related

How to change the image displayed on a button at runtime in Visual Basic?

I am trying to change the Image attribute associated with a button when the button is clicked.
I have added the image I want to display (called "Black Pawn.png") as a resource within the solution and (from searching around and looking at similar questions) am trying to set the image attribute to it as shown below:
Private Sub boardButtonA2_Click(sender As Object, e As EventArgs) Handles boardButtonA2.Click
Dim sprites As Object = My.Resources.ResourceManager
boardButtonA2.Image = sprites.GetObject("Black Pawn.png")
End Sub
But when I click the button, the default image on it just disappears and is replaced with nothing instead of Black Pawn.png.
I am pretty new to using Visual Basic and Visual Studio so I may have failed to have added the image as a resource properly but I can see the image in the Solution Explorer above the form the button is in.
Any help or advice would be a great help.
Thanks.
EDIT:
Having thought more about the potential scenario, I'm wondering whether this answer is actually relevant. Your example code uses a hard-coded String but I'm wondering whether the actual String really would be a user selection. I'll leave it here anyway.
ORIGINAL:
There's no reason to use a hard-coded String to get a resource. If the String was from user entry then maybe, depending on the circumstances. As it is though, you should be using the dedicated property for that resource. That means using this:
boardButtonA2.Image = My.Resources.Black_Pawn
Just be aware that a new object is created every time you get a resource from My.Resources. For that reason, don't keep getting the same property over and over. If you need to use a resource multiple times, get it once and assign it to a field, then use that field multiple times, e.g.
Private blackPawnImage As Image
Private Function GetBlackPawnImage() As Image
If blackPawnImage Is Nothing Then
blackPawnImage = My.Resources.Black_Pawn
End If
Return blackPawnImage
End Function
and then:
boardButtonA2.Image = GetBlackPawnImage()
Also, I suggest that you change the name of the property to BlackPawn rather than Black_Pawn. You can change it to whatever you want on the Resources page of the project properties.
EDIT:
If this application is a chess game then you definitely will need every resource image for the playing pieces so you probably ought to get all of them at load and assign them to variables, then use them from those variables over the course of the game.
(I am assuming the question is for WinForms, if it isn't I will remove this answer)
When adding images as resources to a project, you have to pay attention to the name given to the resource after it is imported - the name can be changed if you want.
The image below is from one of my projects:
The name of the files on disk are:
CSV - Excel.png
GreenDot.png
RedDot.png
To fix your problem change the line:
boardButtonA2.Image = sprites.GetObject("Black Pawn.png")
to be:
boardButtonA2.Image = sprites.GetObject("Black_Pawn")
I don't think adding the image by going into Project > Add Existing Item properly added the image as a resource.
I instead went into *Project > (Solution Name) Properties > Resources > Images > Add Existing Item * and added it that way and was then able to get it working using jmcilhinney's method (I think my original method/a variation of it would work too but theirs is better).

What am I missing in my approach to add new records to my database in VB.NET Applicaton?

I have a data table (Named "Effects") from Access in my VB.Net Userform.
I have two buttons (AddEffect.Click and SaveEffect.Click) that use ADD NEW, END EDIT and UPDATEALL. This works whilst the app is open.
A new record appears in the GRIDVIEW with the Data I inputed into the various textboxes.
Private Sub AddEffect_Click(sender As Object, e As EventArgs) Handles AddEffect.Click
Me.EffectsBindingSource.AddNew()
End Sub
Private Sub SaveEffect_Click(sender As Object, e As EventArgs) Handles SaveEffect.Click
Me.EffectsBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.DatabaseDataSet)
End Sub
However, there are two issues.
The ID Number of the New Record generated is always a -ve integer.
When I click the SAVE ENTRY button and restart the application, the changes have not been saved.
Other examples I look at online do not resemble my issue.
EDIT:
The ID Number of the New Record generated is always a -ve integer.
That's exactly as it should be. The value generated by your application is only temporary. The final ID is generated by the database when you save. The temp value is negative to ensure that it doesn't clash with anything that's already in the database.
When I click the SAVE ENTRY button and restart the application, the changes have not been saved.
You are almost certainly over-writing your database. The way file-based databases should work is that you add a source file to your project and that gets copied to the output folder along with your EXE. It's that copy that your app connects to while you're debugging. If you create a new copy the next time you build, which is the default, you lose any changes you made to the old copy.
To avoid that, select your data file in the Solution Explorer, open the Properties window and set Copy to output directory to Copy if newer. That way, a new copy will only be created if you explicitly delete the old copy or make a change to the source file.
The reason for the dual-file approach is that you're supposed to keep your source file clean and free of test data so that the copy created when you build a Release version of your app will be ready to deploy with a fresh app.

How can I save the textbox content to the application itself?

Simply put, for example, I want to save content of a textbox to the application itself.
I tried "My.Settings" but type of settings must be "user-scoped" & settings(string content of textbox) extracting to %appdata% folder(user.config).
I want store the data(confidential text) on the application without extracting it to anywhere.
On the other hand "application-scoped" settings are "Read-Only" and values of settings can not be change while type of settings "application-scoped". I want settings(string content of textbox) always changeable by the user of app.
TextFile in Resources is also read-only...I have to extract TextFile for changing the content of TextFile.
How can i do that?
You can use My.Computer.Filesystem.WriteAllText() & My.Computer.Filesystem.ReadAllText() to write/read files. And there's no better way.
But yeah, you can achieve your requirement by saving the data into Registry, i.e. if you want to save your any text data which is accessible anywhere from the device, it could really be helpful.
Note that your application will require to be ran as administrator for saving/reading registry values.
To save registry value:
My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\something", "ValueName", "Value")
To access it:
My.Computer.Registry.GetValue("Same Location", "Same ValueName", Nothing) ' Note that "Nothing" is returned when value doesn't exists.
Hope it helps.

How to link webform through vb.net code parsing data.

net and I am trying to create a project that will handle questions from a text file and save the answers to a new text file. I was searching the internet and I couldn't find an example for linking two webform through vb code so here is my problem:
I want to preprocess a text file that will be uploaded in a specific form through vb.net . That context is parsed in a string table. Now I want that table to pass over to the rest of my forms which they will handle the data in order to modify the below webform. Here is where I want to call the other forms in order.
Private Sub BtdContinue_Click(sender As Object, e As EventArgs) Handles BtdContinue.Click
If FileUpl.PostedFile IsNot Nothing Then
Dim finalTextTable(rows.Length, 4) As String
'Preprocess
'for i=0 to finalTextTable.Lenght-1
' for j=0 to 4
'Call the other web form from here in a loop if possible
End if
Here is my web form with labels and images that I want every question to change it
I also want to be able to go to the previous webform through a
previous button.
If i did understand your question correctly you are asking to redirect the data from the preprocessed text file to another form. The easiest solution i can think of is by using session variables. After you have preprocessed your text file and stored it into a dataTable you should add the following:
'Set the value of DataTable to session
Session("DataTb") = finalTextTable
'Perform your Redirect
Response.Redirect("FormB.aspx");
The above code should save the table into a session variable accessible by FormB.aspx. You can then handle the table in the second form as you wish.

Open multiple copies of word form?

I'm fair at Access vba but Word is a new dialect for me.
I work for a hospital that has an Electronic Medical Record (EMR). We need a specific format and structure for the Nurse’s Clinical Progress Notes that can’t be created and enforced in the EMR. I created a Word vba UserForm, "frmProgNote" attached to a document "ProgNote.doc". FrmProgNote" has textboxes that create the structure we need for documentation and there is a command button that when clicked sends the info from the form to bookmarks in ProgNote.doc and copies all text from the document, including the newly inserted text onto the clipboard. The user then pastes it into the EMR. This all works great (believe it or not) but now they want to have multiple copies of frmProgNote open at the same time so they can move between several patients at once.
I’ve worked on this for 2 days and just can’t get it. I found I could not open multiple instances of Word and have the same form open in more than one. I copied the document and form so now have ProgNote1.doc with frmProgNote1 and ProgNote2.doc with frmProgNote2. I can get both to open BUT if I open frmProgNote1 then open frmProgNote2 the only data I can copy comes from frmProgNote2; the copy button doesn’t copy anything from the first form. I can click on frm1 and get it to take new data but the copy button doesn’t work anymore. Any suggestions? Thanks so much.
The following code gives the basis for creating a new instance of a UserForm.
Private frmNewInstance As Object
Private Sub CommandButton1_Click()
Set frmNewInstance = UserForms.Add(Me.Name)
frmNewInstance.Show
End Sub
Private Sub UserForm_Terminate()
Set frmNewInstance = Nothing
End Sub
It is necessary to control the object-reference, setting it to Nothing when the form instance is closed.
Note that this process is not as robust or reliable as it would be in, for example, VB.NET or C#. In particular, it is more difficult to distinguish between the different instances of the form.
One approach to consider is to use the Tag property of the form:
frmNewInstance.Tag = "OtherOne"
frmNewInstance.Show