Im going to try to make this as simple as i possible can. I work in a shipping company, where we email our clients if there is any issues with their orders. I have recently made a vb.net program that standardizes these emails. Now i am rewriting the program so that the templates can be edited from a text file. right now they are hard coded into the program itself.
SO, this is what im wanting to do
Template.txt -> Good afternoon %NAME% your order has shipped.
I want the program to open this text file, Change %NAME% to variable CustName which is entered from a text box, Then show the updated text in a RichTextBox. But i dont want the template.txt to save the changes.
Was wondering if you fine people could help me out with this.
Thanks a lot!
The first answer is on the right track. You just need to store the template text in your file. With this in your text file:
Good afternoon {0} your order has shipped.
you can then do something like this:
Dim name = "someone"
Dim template = File.ReadAllText("TextFile1.txt")
RichTextBox1.Text = String.Format(template, name)
The first argument to String.Format is the template, containing the numbered placeholders. The remaining arguments are the values to replace those placeholders. The first of those arguments will replace every instance of "{0}" in the template, the second will replace "{1}" and so on. You need to make sure that your numbered placeholders always match the order of the values you pass to String.Format.
If you don't want the person editing that template to have to remember what each number means, you can write your own replacement code. For instance, you might store this in the file:
Good afternoon {name} your order has shipped.
and then do this:
Private Function FillTemplate(template As String, name As String) As String
Return template.Replace("{name}", name)
End Function
and this:
Dim name = "someone"
Dim text = FillTemplate(File.ReadAllText("TextFile1.txt"), name)
RichTextBox1.Text = text
You need to add a parameter and a Replace call to FillTemplate for each value you want to insert, e.g.
Private Function FillTemplate(template As String,
name As String,
address As String) As String
Return template.Replace("{name}", name).
Replace("{address}", address)
End Function
I think the syntax you're looking for is something like
String.Format("Good afternoon {0} your order has shipped", name_variable)
If you wanted to change two pieces of information, you could use
String.Format("Good afternoon {0} your order has {1}", name_variable, "shipped")
Related
Essentially I'm creating a D&D character sheet with 9 forms, 6 of which will be saved. I need to save every TextBox in each of those 6 forms. I haven't tried Serialization yet as I haven't found an especially good guide that clarifies exactly what I need to do yet, and at this point it's fairly confusing to me. I have, however, tried saving to a file and writing back when the file is opened. This works for one form, but I'm almost certain that it won't work for multiple forms given the way that the code is written.
Dim SaveCharacter As StreamWriter
If txtName.Text = "" Then
MessageBox.Show("In order to save, you must at least enter a name for your character.", "Error: No Name Entered")
Else
SaveCharacterAs.ShowDialog()
System.IO.File.WriteAllText(SaveCharacterAs.FileName, "")
Try
For Each cnt In Me.Controls.OfType(Of TextBox)
SaveCharacter = File.AppendText(SaveCharacterAs.FileName)
SaveCharacter.WriteLine(cnt.Text)
SaveCharacter.Close()
Next
For Each cnt In frmAttributeRoller.Controls.OfType(Of TextBox)
SaveCharacter = File.AppendText(SaveCharacterAs.FileName)
SaveCharacter.WriteLine(cnt.Text)
SaveCharacter.Close()
Next
The above code is what I have for writing to a file. I was thinking of trying to save the file to a .txt file with the name of the character that's entered in the form, and for each subform that I'm saving, create a separate .txt file with the character name concatenated with the form name, then write code to open all of those files in the correct forms, but that could get messy if the user selects the wrong file to start with. Is there a better way to save multiple forms to one file without serializing? Is serializing my best bet?
Thanks for the help!
Here's a suggestion. Create a DataSet with tables that describe your character sheet fields. Then create an instance of that DataSet and add a record with default (mostly blank) values to the table.
Now you can bind the columns to text boxes like this. (I'm doing this from memory, so forgive me if it doesn't compile - it should at least point you in the right direction
Dim ds As New CharacterDataSet ' Whatever you call your dataset
Dim bs As New BindingSource(ds, ds.CharacterTable.TableName)
bs.MoveFirst()
' Note, if you end up having more than one record, you can use bs.Filter
' to select which record the BindingSource points to.
txtCharacterName.DataBindings.Add("Text", bs, "CharacterName") ' Where CharacterName is the name of the column in the table
cboCharacterRace.DataBindings.Add("SelectedValue", bs, "Race") ' For a ComboBox
Also, if you want to be more robust with your column names in the DataBindings.Add, you can do something like ds.CharacterTable.CharacterNameColumn.ColumnName. That way, you can't accidentally misspell the column name in the binding.
Now you can use DataSet.WriteXML to write out the the whole dataset in one shot.
Ok.
So I am working on a project, irrelevant, and I have a bunch (8) of ComboBoxes (they are in DropDownList mode) and there is 8 save files. I have them being imported and converted to strings:
Using class2 As New StreamReader(path & "SaveData/classdata/classdata2.NIC")
Dim fdcount1 As String
fdcount = class2.ReadToEnd()
MessageBox.Show(fdcount1)
hr2choice.SelectedItem = fdcount1
End Using
I already tested this, and it seems to be working.
(Test code I used:)
MessageBox.Show(fdcount1)
and it showed the value ("DiVita")
Despite this, when I tried setting the ComboBox value to this, it did not seem to work.
The ComboBox does have this value in it, and if I try this, it works:
hr2choice.SelectedItem = "DiVita"
For whatever reasons though, it does not work when I try doing it directly from the string.
Thanks for any help with this!
Nic
To answer this, I have to assume that the data in the text file is formatted as one line for each piece of data.
There seems to be a couple of issues with your code. fdcount is just declared as a string where it should be an array to make it easier to access each line that is read from the file. fdcount1 has no relationship to fdcount - it is a completely separate entity, so the data in fdcount1 is coming from somewhere else.
Rather than the above code, It's easier to use this
Dim fdcount() As String
fdcount = File.ReadAllLines("SaveData/classdata/classdata2.NIC")
MessageBox.Show(fdcount(1))
Note that fdcount is declared as an Array of String. The 2nd line does all the opening, reading into the array, and closing of the file.
You can then access each element of the array as shown in the 3rd line.
I found this create an outlook rule to create folders if needed based on text in subject line
Which gets me 99% of the way there. But I would like the reference point to create the folder name to start after a set character. For example if the subjet was "xxxx #1234", I want the folder name to be 1234, not #1234 as would happen if I put the str value as #. Any pointers?
It looks like you need to get a substring, see String Manipulation
for more information about string VBA functions.
For example, to get number you can use the Right function:
MsgBox Right("xxxx #1234", 4)
In VB.net, I want to make a counting program using a Function and a Sub.
There is a textbox to input a date and a button to exercise the programme in Form1.
I have a txt file which was extracted from MS-Excel with sequential date of time at its column A.
And from that txt file, I want to count the number of date(Actually string) such as "18-Jun-12".
The answer showing the count should be in the format of msgbox in the Sub.
I really have no idea how to link a Function and a Sub using variable, because I am just beginner.
Any help will be gratefully accepted.
If the fields are delimited by comma you must be careful since the field itself could contain a comma. Then you cannot differentiate between the value and the delimiter. You either could enclose the fields with quotes to mask them. But then you should use an available CSV parser anyway.
If the values never contain comma and you want a simple solution use File.ReadLines or File.ReadAllLines to read the lines and String.Split to get all fields per line.
Here's a simple approach using a little bit of LINQ to count all lines which contain the searched date (as string):
Dim linesWithThatDate = From line in File.ReadLines("Path to File")
Where line.Split(","c)(0).Trim() = "18-Jun-12"
Dim count = linesWithThatDate.Count()
As an aside, if the user must enter a date you could use a DateTimePicker control instead. Then you should also use Date.Parse(line.Split(","c)(0).Trim()) or Date.TryParse to get a real date.
I have some contract text that may change.
It is currently (non live system) stored in a database field (we determine which contract text to get by using other fields in table). I, however need to display a specific date (based on individual contract) within this text.
IE (but Jan 12, changes depending on the individual contract):
blah blah blah... on Jan 12, 2009... blah blah blah
but everything else in the contract text is the same.
I'm looking for a way to inject the date into this text. Similar to .NET's
Console.Write("Some text here {0} and some more here", "My text to inject");
Is there something like this? Or am I going to need to split the text into two fields and just concatenate?
I am always displaying this information using Crystal Reports so if I can inject the data in through Crystal then that's fine.
I am using Crystal Reports, SqlServer 2005, and VB.net.
You could use some "reserved string" (such as the "{0}") as part of the contract, and then perform a replace after reading from the database.
If there's no option for this reserved string (for instance, if the contract may contain any type of string characters in any sequence, which I find unlikely), then you'll probably need to split into 2 text fields
Have you tried putting a text marker like the {0} above that can be replaced in the crystal reports code?
You can create a formula field and concatenate your text there.
If the data is stored in the database, the formula text should look like this:
"Some static text " & totext({yourRecord.yourDateField}, "yyyy")
Or you can provide it as a parameter before you show the report:
Dim parameterValue As New CrystalDecisions.Shared.ParameterDiscreteValue
value.Value = yourDate
Dim parameter As New CrystalDecisions.Shared.ParameterField
parameter.ParameterFieldName = "MyParam"
parameter.CurrentValues.Add(value)
parameter.HasCurrentValue = True
Me.CrystalReportViewer1.ReportSource = rapport
Me.CrystalReportViewer1.ParameterFieldInfo.Clear()
Me.CrystalReportViewer1.ParameterFieldInfo.Add(parameter)
Then the formula text should look like this:
"some static text " & {?MyParam}
I'm assuming you have a data source connected to your report. You can drag the field from the Database Explorer drop where it should appear. This way whenever the report runs the correct text will always be shown.