473,406 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

VB 2005 Express: Replacing parameters in a text file with TextBox values

I have a text file with parameters like the following embedded in the text:

@@TextBox1@@, @@TextBox2@@, etc.

I know how to read this text file. However, I am trying to figure out how
to replace these parameters in the text file with values of the textboxes on
my Windows Form called Form1. For example, replace @@TextBox1@@ with value
of TextBox1, replace @@TextBox2@@ with value of TextBox2, replace
@@ComboBox1@@ with value of ComboBox1, and so on. I am told this is doable
but I am not quite sure how.

I'd appreciate any pointers.

Thanks,
J.S.

--
Nov 21 '05 #1
17 1986
JS,

My last one today very roughly in this message.

Give your messagesboxes in the Tag the number of your replacer.

I assume all your textboxes are direct on the form and those names are 1 to
10 and not in a groupbox than it can be something as

\\\
For each ctr as control in form.controls
if cint(ctr.tag) > 0 and Cint(ctr.tag) < 11
mytext = mytext.replace("@" & Cstr(ctr.tag) & "@", ctr.text)
end if
next
///

As I said not tested, be aware that because you are building everytime new
strings this procedure can if it are very long strings cost time, however
probably not so long that the user will really recognise that. Just try.

I hope this helps,

Cor.

Nov 21 '05 #2
> I assume all your textboxes are direct on the form and those names are 1
to 10 and not in a groupbox
Yes, that's correct.
\\\
For each ctr as control in form.controls
if cint(ctr.tag) > 0 and Cint(ctr.tag) < 11
mytext = mytext.replace("@" & Cstr(ctr.tag) & "@", ctr.text)
end if
next
///
Thanks, Cor! VB 2005 Express didn't like "form.controls" so I changed it to
"Me.Controls" but something is still missing. I'll keep playing with it to
see if I can get it working.
As I said not tested, be aware that because you are building everytime new
strings this procedure can if it are very long strings cost time, however
probably not so long that the user will really recognise that. Just try.


Ok, I'll keep that in mind.

Thanka again!
J.S.
Nov 21 '05 #3
JS.
Thanks, Cor! VB 2005 Express didn't like "form.controls" so I changed it
to "Me.Controls" but something is still missing. I'll keep playing with
it to see if I can get it working.

doh

Terrible that I wrote that, not any vb program likes forms.controls.
I wrote it was my last message yesterday and did it very quick and dirty.

However I tested it now, and with that me.controls it works for me nice

Therefore if you have problems tell them.

Cor
Nov 21 '05 #4
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:ed**************@tk2msftngp13.phx.gbl...
Terrible that I wrote that, not any vb program likes forms.controls.
I wrote it was my last message yesterday and did it very quick and dirty.

However I tested it now, and with that me.controls it works for me nice


How are you verifying the results? Are you showing the results i.e. the
modified text also in a TextBox? Or are you using MsgBox or writing back to
the text file?

Thanks,
J.S.
Nov 21 '05 #5
I noticed you have single @ in your code. Are your parameters named like
@TextBox1@ instead of @@TextBox1@@?
Nov 21 '05 #6
For the time being I have only one parameter in my text file called
@@TextBox1@@.

I can replace that parameter with the entered value if I use this:

Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("D:\My Documents\Visual
Studio 2005\textfile.txt")
Dim newString As String
newString = TextBox1.Text
fileReader = fileReader.Replace("@@TextBox1@@", newString)
MsgBox(fileReader)

However, this does not work:

Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("D:\My Documents\Visual
Studio 2005\textfile.txt")
For Each ctr As Control In Me.Controls
If CInt(ctr.Tag) > 0 And CInt(ctr.Tag) < 3 Then
fileReader = fileReader.Replace("@@" & CStr(ctr.Tag) & "@@", ctr.Text)
End If
Next
MsgBox(fileReader)

Even if I change @@ to @ it does not work.

Thanks,
J.S.
--

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:ed**************@tk2msftngp13.phx.gbl...
JS.
Thanks, Cor! VB 2005 Express didn't like "form.controls" so I changed it
to "Me.Controls" but something is still missing. I'll keep playing with
it to see if I can get it working.

doh

Terrible that I wrote that, not any vb program likes forms.controls.
I wrote it was my last message yesterday and did it very quick and dirty.

However I tested it now, and with that me.controls it works for me nice

Therefore if you have problems tell them.

Cor

Nov 21 '05 #7
JS,

Open a form and use than this code

\\\
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim textbox1 As New TextBox
Dim textbox2 As New TextBox
textbox1.Tag = 1
textbox2.Tag = 2
textbox1.Text = "Cor"
textbox2.Text = "JS"
Me.Controls.Add(textbox1)
Me.Controls.Add(textbox2)
Dim mytext As String = "I am @1@ testing this for @2@"
For Each ctr As Control In Me.Controls
If CInt(ctr.Tag) > 0 And CInt(ctr.Tag) < 11 Then
mytext = mytext.Replace("@" & CStr(ctr.Tag) & "@", ctr.Text)
End If
Next
MessageBox.Show(mytext)
Me.Close()
End Sub
///

As you told you before I have not VB express installed I tested this in
VB2005B2. Therefore if it not works, tell than I will intall VB express and
have a look at that and this.

Cor
Nov 21 '05 #8
JS,

I just used 1 to 10, that textbox1 makes it more difucult to do the trick I
did by using it as integer and string while I see no need for that textbox,
if you want a double @@ than you can just make from the single a double @@.

Cor
Nov 21 '05 #9
That code works!
As you told you before I have not VB express installed I tested this in
VB2005B2. Therefore if it not works, tell than I will intall VB express
and have a look at that and this.
Actually, I have a copy of VB2005B2. I'll install it soon.

Thanks for all your help!

J.S.

--

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl... JS,

Open a form and use than this code

\\\
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim textbox1 As New TextBox
Dim textbox2 As New TextBox
textbox1.Tag = 1
textbox2.Tag = 2
textbox1.Text = "Cor"
textbox2.Text = "JS"
Me.Controls.Add(textbox1)
Me.Controls.Add(textbox2)
Dim mytext As String = "I am @1@ testing this for @2@"
For Each ctr As Control In Me.Controls
If CInt(ctr.Tag) > 0 And CInt(ctr.Tag) < 11 Then
mytext = mytext.Replace("@" & CStr(ctr.Tag) & "@",
ctr.Text)
End If
Next
MessageBox.Show(mytext)
Me.Close()
End Sub
///

As you told you before I have not VB express installed I tested this in
VB2005B2. Therefore if it not works, tell than I will intall VB express
and have a look at that and this.

Cor

Nov 21 '05 #10
You are right.

Thanks,
J.S.

--

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:eQ**************@TK2MSFTNGP12.phx.gbl...
JS,

I just used 1 to 10, that textbox1 makes it more difucult to do the trick
I did by using it as integer and string while I see no need for that
textbox, if you want a double @@ than you can just make from the single a
double @@.

Cor

Nov 21 '05 #11
First, much thanks to Cor for all his help. Second, one of my parameter
names in the text file was incorrect so that was causing some problems
although that might not have been related to my posts/problems here. Third,
apart from all the wonderful code posted by Cor here are two other methods I
used to get this application to work successfully (in case anyone else needs
it):

For Each Ctl As Control In Me.Controls
If Ctl.GetType() Is GetType(TextBox) Then
Dim Txt As TextBox = CType(Ctl, TextBox)
fileReader = fileReader.Replace("@@" & Txt.Name & "@@",
Txt.Text)
End If
Next

or:

For Each Ctl In Me.Controls
If Ctl.GetType is GetType(TextBox) Then
fileReader = fileReader.Replace("@@" & ctl.Name & "@@",
ctl.Text)
End If
Next

--
Nov 21 '05 #12
J.S.
This appears to be a continuation of the "Which is a better method to
concatenate large blocks of text" thread.

I would probably base the template on XML as discussed in the other thread.
Then the parameters would just be emitted as they are referenced...

If you want to stay with the parameter markers in a text file, I would use
either StringBuilder.Replace or RegEx Replace. I would avoid String.Replace
as the memory penalty may be too great!

Something like:

Dim input As String = My.Computer.FileSystem.ReadAllText("template.txt")
Dim sb As New StringBuilder(input, input.Length * 2)

' You could use a for loop as the others showed
sb.Replace("@@TextBox1@@", Me.TextBox1.Text)
sb.Replace("@@TextBox2@@", Me.TextBox2.Text)
sb.Replace("@@TextBox3@@", Me.TextBox3.Text)
sb.Replace("@@ComboBox1@@", Me.ComboBox1.Text)
sb.Replace("@@ComboBox2@@", Me.ComboBox2.Text)

Notice that the StringBuilder is mutable, so you can keep making changes to
the single instance. The "input.Length * 2" on the constructor ensures that
the StringBuilder's buffer is twice the size as the input file, allowing for
lots of room before its buffer needs to be expanded again.

Alternatively I would consider using RegEx.Replace:

Private Shared ReadOnly m_variables As New Hashtable

Private Shared Function EscapeCharacters(ByVal match As Match) As String
Return DirectCast(m_variables(match.Value), String)
End Function
Public Shared Sub Main()
' Add all the "variables" and their values to the HashTable.
m_variables.Add("@@TextBox1@@", TextBox1.Text)
m_variables.Add("@@TextBox2@@", TextBox2.Text)

Const pattern As String = "@@\w*@@"
Static parser As New Regex(pattern, RegexOptions.Compiled)

Dim input As String =
My.Computer.FileSystem.ReadAllText("template.txt")
Dim output As String
output = parser.Replace(input, AddressOf EscapeCharacters)

Return

End Sub

Hope this helps
Jay

"J.S." <js*@nospam.com> wrote in message
news:O2*************@TK2MSFTNGP15.phx.gbl...
|I have a text file with parameters like the following embedded in the text:
|
| @@TextBox1@@, @@TextBox2@@, etc.
|
| I know how to read this text file. However, I am trying to figure out how
| to replace these parameters in the text file with values of the textboxes
on
| my Windows Form called Form1. For example, replace @@TextBox1@@ with
value
| of TextBox1, replace @@TextBox2@@ with value of TextBox2, replace
| @@ComboBox1@@ with value of ComboBox1, and so on. I am told this is
doable
| but I am not quite sure how.
|
| I'd appreciate any pointers.
|
| Thanks,
| J.S.
|
| --
|
|
Nov 21 '05 #13
Jay,

I never tetsted it, therefore are you sure that the strings in a
stringbuilder are mutable and not that the mutability is better because that
the stringbuilder exist from small string objects and therefore the
performance is better.

That is the reason that I did not use the SB, while as you probably know
particulary *I* use in my samples the SB from the first days that I am
active in these newsgroups.

Cor
Nov 21 '05 #14
Hi Jay,

Thanks for your kind response and advice.
This appears to be a continuation of the "Which is a better method to
concatenate large blocks of text" thread.
Yes, indeed it is. :)
I would probably base the template on XML as discussed in the other
thread.
Then the parameters would just be emitted as they are referenced...
I am trying to figure out how blocks of code can best be included in an XML
file.
If you want to stay with the parameter markers in a text file, I would use
either StringBuilder.Replace or RegEx Replace. I would avoid
String.Replace
as the memory penalty may be too great!
Ok, thanks! Till I figure out the XML thing I'll try StringBuilder.Replace
and RegEx Replace.

Thanks for the code examples.

Best,
J.S.
--

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:eZ**************@TK2MSFTNGP15.phx.gbl... J.S.
This appears to be a continuation of the "Which is a better method to
concatenate large blocks of text" thread.

I would probably base the template on XML as discussed in the other
thread.
Then the parameters would just be emitted as they are referenced...

If you want to stay with the parameter markers in a text file, I would use
either StringBuilder.Replace or RegEx Replace. I would avoid
String.Replace
as the memory penalty may be too great!

Something like:

Dim input As String =
My.Computer.FileSystem.ReadAllText("template.txt")
Dim sb As New StringBuilder(input, input.Length * 2)

' You could use a for loop as the others showed
sb.Replace("@@TextBox1@@", Me.TextBox1.Text)
sb.Replace("@@TextBox2@@", Me.TextBox2.Text)
sb.Replace("@@TextBox3@@", Me.TextBox3.Text)
sb.Replace("@@ComboBox1@@", Me.ComboBox1.Text)
sb.Replace("@@ComboBox2@@", Me.ComboBox2.Text)

Notice that the StringBuilder is mutable, so you can keep making changes
to
the single instance. The "input.Length * 2" on the constructor ensures
that
the StringBuilder's buffer is twice the size as the input file, allowing
for
lots of room before its buffer needs to be expanded again.

Alternatively I would consider using RegEx.Replace:

Private Shared ReadOnly m_variables As New Hashtable

Private Shared Function EscapeCharacters(ByVal match As Match) As
String
Return DirectCast(m_variables(match.Value), String)
End Function
Public Shared Sub Main()
' Add all the "variables" and their values to the HashTable.
m_variables.Add("@@TextBox1@@", TextBox1.Text)
m_variables.Add("@@TextBox2@@", TextBox2.Text)

Const pattern As String = "@@\w*@@"
Static parser As New Regex(pattern, RegexOptions.Compiled)

Dim input As String =
My.Computer.FileSystem.ReadAllText("template.txt")
Dim output As String
output = parser.Replace(input, AddressOf EscapeCharacters)

Return

End Sub

Hope this helps
Jay

Nov 21 '05 #15
Hi Jay,

Thanks for your kind response and advice.
This appears to be a continuation of the "Which is a better method to
concatenate large blocks of text" thread.
Yes, indeed it is. :)
I would probably base the template on XML as discussed in the other
thread.
Then the parameters would just be emitted as they are referenced...
I am trying to figure out how blocks of code can best be included in an XML
file.
If you want to stay with the parameter markers in a text file, I would use
either StringBuilder.Replace or RegEx Replace. I would avoid
String.Replace
as the memory penalty may be too great!
Ok, thanks! Till I figure out the XML thing I'll try StringBuilder.Replace
and RegEx Replace.

Thanks for the code examples.

Best,
J.S.
--

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:eZ**************@TK2MSFTNGP15.phx.gbl... J.S.
This appears to be a continuation of the "Which is a better method to
concatenate large blocks of text" thread.

I would probably base the template on XML as discussed in the other
thread.
Then the parameters would just be emitted as they are referenced...

If you want to stay with the parameter markers in a text file, I would use
either StringBuilder.Replace or RegEx Replace. I would avoid
String.Replace
as the memory penalty may be too great!

Something like:

Dim input As String =
My.Computer.FileSystem.ReadAllText("template.txt")
Dim sb As New StringBuilder(input, input.Length * 2)

' You could use a for loop as the others showed
sb.Replace("@@TextBox1@@", Me.TextBox1.Text)
sb.Replace("@@TextBox2@@", Me.TextBox2.Text)
sb.Replace("@@TextBox3@@", Me.TextBox3.Text)
sb.Replace("@@ComboBox1@@", Me.ComboBox1.Text)
sb.Replace("@@ComboBox2@@", Me.ComboBox2.Text)

Notice that the StringBuilder is mutable, so you can keep making changes
to
the single instance. The "input.Length * 2" on the constructor ensures
that
the StringBuilder's buffer is twice the size as the input file, allowing
for
lots of room before its buffer needs to be expanded again.

Alternatively I would consider using RegEx.Replace:

Private Shared ReadOnly m_variables As New Hashtable

Private Shared Function EscapeCharacters(ByVal match As Match) As
String
Return DirectCast(m_variables(match.Value), String)
End Function
Public Shared Sub Main()
' Add all the "variables" and their values to the HashTable.
m_variables.Add("@@TextBox1@@", TextBox1.Text)
m_variables.Add("@@TextBox2@@", TextBox2.Text)

Const pattern As String = "@@\w*@@"
Static parser As New Regex(pattern, RegexOptions.Compiled)

Dim input As String =
My.Computer.FileSystem.ReadAllText("template.txt")
Dim output As String
output = parser.Replace(input, AddressOf EscapeCharacters)

Return

End Sub

Hope this helps
Jay

Nov 21 '05 #16
Cor,
| I never tetsted it, therefore are you sure that the strings in a
| stringbuilder are mutable
Yes I'm sure, as I have tested it.

| and not that the mutability is better because that
| the stringbuilder exist from small string objects and therefore the
| performance is better.
No.

As you may know from MSDN "StringBuilder represents a mutable String of
characters":

http://msdn.microsoft.com/library/de...classtopic.asp

According to the remarks on the above site "This class represents a
string-like object whose value is a mutable sequence of characters." Which
IMHO clearly suggests the StringBuilder contains a single "string" or array
of Characters. "Small string objects" have little or nothing to do with it.
Its the size of the StringBuilder's "buffer" that matters.

Hope this helps
Jay

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
| Jay,
|
| I never tetsted it, therefore are you sure that the strings in a
| stringbuilder are mutable and not that the mutability is better because
that
| the stringbuilder exist from small string objects and therefore the
| performance is better.
|
| That is the reason that I did not use the SB, while as you probably know
| particulary *I* use in my samples the SB from the first days that I am
| active in these newsgroups.
|
| Cor
|
|
Nov 21 '05 #17
> Cor,
| I never tetsted it, therefore are you sure that the strings in a
| stringbuilder are mutable
No.
That is not true (however read further)
As you may know from MSDN "StringBuilder represents a mutable String of >
characters":


That is the clue.

However the word String is here in my opinion not in its place and that has
probably confused me all the time.

In the remarks it is cleared, however that does not mean that the sentence
above is in my opinion right, there had to be placed "string-like" as in the
remarks.

*This class represents a string-like object whose value is a mutable
sequence of characters*.

And now it becomes clear for me, that than the replace with the sb will be
probably mostly faster in this case.

Thanks for helping clearing this up.

Cor

Nov 21 '05 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: J.S. | last post by:
Let's say I have a text file with parameters like the following embedded in the text: @@Textbox1@@, @@Textbox2@@, etc. Is it possible to replace the parameters in the text file with values...
6
by: | last post by:
Hi all, is there a better way to stream binary data stored in a table in sql 2005 to a browser in .net 2.0? Or is the code same as in .net 1.1? We noticed that in certain heavy load scenarios,...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.