471,599 Members | 1,765 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

VC# 2005 Express Windows Forms: Replacing parameters in a text file with values from textboxes

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
entered into textboxes in the Windows Forms UI?

Thanks,
J.S.
--
Nov 17 '05 #1
31 2059
foreach (object o in Form1.Controls)
{
if (o is TextBox)
{
String.Replace("@@" + ((TextBox)o).Name + "@@", ((TextBox)o).Text);
}
}

Of course, you'll need to have read in the contents of the file, and write
it back out again once you're finished.

"J.S." <js*@nospam.com> wrote in message
news:e0**************@TK2MSFTNGP11.phx.gbl...
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
entered into textboxes in the Windows Forms UI?

Thanks,
J.S.
--

Nov 17 '05 #2
foreach (object o in Form1.Controls)
{
if (o is TextBox)
{
String.Replace("@@" + ((TextBox)o).Name + "@@", ((TextBox)o).Text);
}
}

Of course, you'll need to have read in the contents of the file, and write
it back out again once you're finished.

"J.S." <js*@nospam.com> wrote in message
news:e0**************@TK2MSFTNGP11.phx.gbl...
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
entered into textboxes in the Windows Forms UI?

Thanks,
J.S.
--

Nov 17 '05 #3
Thanks, Brendan! Does C# have any equivalent to the VB
My.Computer.FileSystem.OpenTextFileReader or
My.Computer.FileSystem.ReadAllText? I have been searching all over MSDN and
Google without much success. It seems that VB is much better covered at
MSDN.

Thanks,
J.S.

--

"Brendan Green" <bg****@simtap.com.au> wrote in message
news:u6**************@TK2MSFTNGP12.phx.gbl...
foreach (object o in Form1.Controls)
{
if (o is TextBox)
{
String.Replace("@@" + ((TextBox)o).Name + "@@", ((TextBox)o).Text);
}
}

Of course, you'll need to have read in the contents of the file, and write
it back out again once you're finished.

Nov 17 '05 #4
Thanks, Brendan! Does C# have any equivalent to the VB
My.Computer.FileSystem.OpenTextFileReader or
My.Computer.FileSystem.ReadAllText? I have been searching all over MSDN and
Google without much success. It seems that VB is much better covered at
MSDN.

Thanks,
J.S.

--

"Brendan Green" <bg****@simtap.com.au> wrote in message
news:u6**************@TK2MSFTNGP12.phx.gbl...
foreach (object o in Form1.Controls)
{
if (o is TextBox)
{
String.Replace("@@" + ((TextBox)o).Name + "@@", ((TextBox)o).Text);
}
}

Of course, you'll need to have read in the contents of the file, and write
it back out again once you're finished.

Nov 17 '05 #5
StreamReader and File.OpenText sort of worked. They gave me the last line
in the text file.
--

"J.S." <js*@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Thanks, Brendan! Does C# have any equivalent to the VB
My.Computer.FileSystem.OpenTextFileReader or
My.Computer.FileSystem.ReadAllText? I have been searching all over MSDN
and Google without much success. It seems that VB is much better covered
at MSDN.

Nov 17 '05 #6
StreamReader and File.OpenText sort of worked. They gave me the last line
in the text file.
--

"J.S." <js*@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Thanks, Brendan! Does C# have any equivalent to the VB
My.Computer.FileSystem.OpenTextFileReader or
My.Computer.FileSystem.ReadAllText? I have been searching all over MSDN
and Google without much success. It seems that VB is much better covered
at MSDN.

Nov 17 '05 #7
J.S. <js*@nospam.com> wrote:
Thanks, Brendan! Does C# have any equivalent to the VB
My.Computer.FileSystem.OpenTextFileReader or
My.Computer.FileSystem.ReadAllText? I have been searching all over MSDN and
Google without much success. It seems that VB is much better covered at
MSDN.


Use File.OpenText, or new StreamReader to create a TextReader which
reads data from a file. After that, use TextReader.ReadToEnd to read
the whole file.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8
J.S. <js*@nospam.com> wrote:
Thanks, Brendan! Does C# have any equivalent to the VB
My.Computer.FileSystem.OpenTextFileReader or
My.Computer.FileSystem.ReadAllText? I have been searching all over MSDN and
Google without much success. It seems that VB is much better covered at
MSDN.


Use File.OpenText, or new StreamReader to create a TextReader which
reads data from a file. After that, use TextReader.ReadToEnd to read
the whole file.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #9
J.S. <js*@nospam.com> wrote:
StreamReader and File.OpenText sort of worked. They gave me the last line
in the text file.


Then I suspect you're using them incorrectly. Perhaps you're calling
ReadLine multiple times, and only using the last returned value?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #10
J.S. <js*@nospam.com> wrote:
StreamReader and File.OpenText sort of worked. They gave me the last line
in the text file.


Then I suspect you're using them incorrectly. Perhaps you're calling
ReadLine multiple times, and only using the last returned value?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #11
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Use File.OpenText, or new StreamReader to create a TextReader which
reads data from a file. After that, use TextReader.ReadToEnd to read
the whole file.


Thanks, Jon! That worked fine.

J.S.
Nov 17 '05 #12
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Use File.OpenText, or new StreamReader to create a TextReader which
reads data from a file. After that, use TextReader.ReadToEnd to read
the whole file.


Thanks, Jon! That worked fine.

J.S.
Nov 17 '05 #13
> Then I suspect you're using them incorrectly. Perhaps you're calling
ReadLine multiple times, and only using the last returned value?


You were right, Jon. I was using the wrong code first.

I changed this:

StreamReader re = File.OpenText("D:\\My Documents\\Visual Studio
2005\\textfile.txt");
string input = null;
while ((input = re.ReadLine()) != null)
{
textBox1.Text = input;
}

to this:

// Read the file as one string.
System.IO.StreamReader myFile =
new System.IO.StreamReader("D:\\My Documents\\Visual Studio
2005\\textfile.txt");
string myString = myFile.ReadToEnd();
myFile.Close();
textBox1.Text = myString;

and it worked as desired.

Thanks,
J.S.
Nov 17 '05 #14
> Then I suspect you're using them incorrectly. Perhaps you're calling
ReadLine multiple times, and only using the last returned value?


You were right, Jon. I was using the wrong code first.

I changed this:

StreamReader re = File.OpenText("D:\\My Documents\\Visual Studio
2005\\textfile.txt");
string input = null;
while ((input = re.ReadLine()) != null)
{
textBox1.Text = input;
}

to this:

// Read the file as one string.
System.IO.StreamReader myFile =
new System.IO.StreamReader("D:\\My Documents\\Visual Studio
2005\\textfile.txt");
string myString = myFile.ReadToEnd();
myFile.Close();
textBox1.Text = myString;

and it worked as desired.

Thanks,
J.S.
Nov 17 '05 #15
When I try to debug this code I get this error message:

An object reference is required for the nonstatic field, method, or property
'System.Windows.Forms.Control.Controls.get'

My code is:

StreamReader myFile = new StreamReader("D:\\My Documents\\Visual Studio
2005\\textfile.txt");
string myString = myFile.ReadToEnd();
string replacedString = textBox2.Text;

foreach (object o in Form1.Controls)
{
if (o is TextBox)
{
myString.Replace("@@" + ((TextBox)o).Name + "@@", ((TextBox)o).Text);
}
}
myFile.Close();
// Display the file contents.
textBox1.Text = myString;

I already have the "using System.Windows.Forms;" directive in my cs file.
--

"Brendan Green" <bg****@simtap.com.au> wrote in message
news:u6**************@TK2MSFTNGP12.phx.gbl...
foreach (object o in Form1.Controls)
{
if (o is TextBox)
{
String.Replace("@@" + ((TextBox)o).Name + "@@", ((TextBox)o).Text);
}
}

Of course, you'll need to have read in the contents of the file, and write
it back out again once you're finished.

Nov 17 '05 #16
When I try to debug this code I get this error message:

An object reference is required for the nonstatic field, method, or property
'System.Windows.Forms.Control.Controls.get'

My code is:

StreamReader myFile = new StreamReader("D:\\My Documents\\Visual Studio
2005\\textfile.txt");
string myString = myFile.ReadToEnd();
string replacedString = textBox2.Text;

foreach (object o in Form1.Controls)
{
if (o is TextBox)
{
myString.Replace("@@" + ((TextBox)o).Name + "@@", ((TextBox)o).Text);
}
}
myFile.Close();
// Display the file contents.
textBox1.Text = myString;

I already have the "using System.Windows.Forms;" directive in my cs file.
--

"Brendan Green" <bg****@simtap.com.au> wrote in message
news:u6**************@TK2MSFTNGP12.phx.gbl...
foreach (object o in Form1.Controls)
{
if (o is TextBox)
{
String.Replace("@@" + ((TextBox)o).Name + "@@", ((TextBox)o).Text);
}
}

Of course, you'll need to have read in the contents of the file, and write
it back out again once you're finished.

Nov 17 '05 #17
J.S. <js*@nospam.com> wrote:
When I try to debug this code I get this error message:

An object reference is required for the nonstatic field, method, or property
'System.Windows.Forms.Control.Controls.get'

My code is:

StreamReader myFile = new StreamReader("D:\\My Documents\\Visual Studio
2005\\textfile.txt");
string myString = myFile.ReadToEnd();
string replacedString = textBox2.Text;

foreach (object o in Form1.Controls)
{
if (o is TextBox)
{
myString.Replace("@@" + ((TextBox)o).Name + "@@", ((TextBox)o).Text);
}
}
myFile.Close();
// Display the file contents.
textBox1.Text = myString;

I already have the "using System.Windows.Forms;" directive in my cs file.


Form1 is the name of a class, not an instance. When you say:

foreach (object o in Form1.Controls)

which *instance* of Form1 are you talking about? Solve that and I think
you'll find the answer...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #18
J.S. <js*@nospam.com> wrote:
When I try to debug this code I get this error message:

An object reference is required for the nonstatic field, method, or property
'System.Windows.Forms.Control.Controls.get'

My code is:

StreamReader myFile = new StreamReader("D:\\My Documents\\Visual Studio
2005\\textfile.txt");
string myString = myFile.ReadToEnd();
string replacedString = textBox2.Text;

foreach (object o in Form1.Controls)
{
if (o is TextBox)
{
myString.Replace("@@" + ((TextBox)o).Name + "@@", ((TextBox)o).Text);
}
}
myFile.Close();
// Display the file contents.
textBox1.Text = myString;

I already have the "using System.Windows.Forms;" directive in my cs file.


Form1 is the name of a class, not an instance. When you say:

foreach (object o in Form1.Controls)

which *instance* of Form1 are you talking about? Solve that and I think
you'll find the answer...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #19
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Form1 is the name of a class, not an instance. When you say:

foreach (object o in Form1.Controls)

which *instance* of Form1 are you talking about? Solve that and I think
you'll find the answer...


I have only one Form1 in my application but you are probably referring to
something else.

J.S.
Nov 17 '05 #20
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Form1 is the name of a class, not an instance. When you say:

foreach (object o in Form1.Controls)

which *instance* of Form1 are you talking about? Solve that and I think
you'll find the answer...


I have only one Form1 in my application but you are probably referring to
something else.

J.S.
Nov 17 '05 #21
J.S. <js*@nospam.com> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Form1 is the name of a class, not an instance. When you say:

foreach (object o in Form1.Controls)

which *instance* of Form1 are you talking about? Solve that and I think
you'll find the answer...


I have only one Form1 in my application but you are probably referring to
something else.


No, I'm saying that Form1 is a class. You *may* have only created one
instance, but how's the compiler going to know that? Put it this way -
using Form1.Controls is like using String.Length - what would you
expect a call to String.Length to return?

You need to use the Controls property on a reference to an instance to
Form1, not on the type itself.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #22
J.S. <js*@nospam.com> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Form1 is the name of a class, not an instance. When you say:

foreach (object o in Form1.Controls)

which *instance* of Form1 are you talking about? Solve that and I think
you'll find the answer...


I have only one Form1 in my application but you are probably referring to
something else.


No, I'm saying that Form1 is a class. You *may* have only created one
instance, but how's the compiler going to know that? Put it this way -
using Form1.Controls is like using String.Length - what would you
expect a call to String.Length to return?

You need to use the Controls property on a reference to an instance to
Form1, not on the type itself.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #23
> No, I'm saying that Form1 is a class. You *may* have only created one
instance, but how's the compiler going to know that? Put it this way -
using Form1.Controls is like using String.Length - what would you
expect a call to String.Length to return?

You need to use the Controls property on a reference to an instance to
Form1, not on the type itself.


Ok! I changed Form1.Controls to this.Controls and it worked.

Thanks, Brendan and Jon!

J.S.
Nov 17 '05 #24
> No, I'm saying that Form1 is a class. You *may* have only created one
instance, but how's the compiler going to know that? Put it this way -
using Form1.Controls is like using String.Length - what would you
expect a call to String.Length to return?

You need to use the Controls property on a reference to an instance to
Form1, not on the type itself.


Ok! I changed Form1.Controls to this.Controls and it worked.

Thanks, Brendan and Jon!

J.S.
Nov 17 '05 #25
> Ok! I changed Form1.Controls to this.Controls and it worked.

Sorry, I was looking at something else. Just ignore that.
Nov 17 '05 #26
> Ok! I changed Form1.Controls to this.Controls and it worked.

Sorry, I was looking at something else. Just ignore that.
Nov 17 '05 #27
> Ok! I changed Form1.Controls to this.Controls and it worked.

Actually, I really did get it working. :)
Nov 17 '05 #28
> Ok! I changed Form1.Controls to this.Controls and it worked.

Actually, I really did get it working. :)
Nov 17 '05 #29
Thanks to Jon Skeet and Brendan Green for their help.

Here is the code I eventually used for the application (in case anyone else
is interested):

foreach (Control Ctl in this.Controls)
{
if (Ctl.GetType() == typeof(TextBox))
{
TextBox Txt = (TextBox)Ctl;
myString = myString.Replace("@@" + Txt.Name + "@@", Txt.Text);
}
}

--
Nov 17 '05 #30
J.S. <js*@nospam.com> wrote:
Thanks to Jon Skeet and Brendan Green for their help.

Here is the code I eventually used for the application (in case anyone else
is interested):

foreach (Control Ctl in this.Controls)
{
if (Ctl.GetType() == typeof(TextBox))
{
TextBox Txt = (TextBox)Ctl;
myString = myString.Replace("@@" + Txt.Name + "@@", Txt.Text);
}
}


There's no need to use typeof here. Just use:

foreach (Control control in Controls)
{
TextBox textBox = control as TextBox;
if (textBox != null)
{
myString = myString.Replace("@@"+textBox.Name+"@@",
textBox.Text);
}
}

That's more idiomatic C#, IMO.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #31
Thanks, Jon!

--

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
There's no need to use typeof here. Just use:

foreach (Control control in Controls)
{
TextBox textBox = control as TextBox;
if (textBox != null)
{
myString = myString.Replace("@@"+textBox.Name+"@@",
textBox.Text);
}
}

That's more idiomatic C#, IMO.

Nov 17 '05 #32

This discussion thread is closed

Replies have been disabled for this discussion.

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.