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

  • Thread starter Thread starter J.S.
  • Start date Start date
J

J.S.

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.


--
 
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.
 
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.
 
StreamReader and File.OpenText sort of worked. They gave me the last line
in the text file.
 
J.S. said:
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.
 
J.S. said:
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 said:
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.
 
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.
 
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.
 
J.S. said:
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 said:
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.
 
J.S. said:
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.
 
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.
 
Ok! I changed Form1.Controls to this.Controls and it worked.

Sorry, I was looking at something else. Just ignore that.
 
Ok! I changed Form1.Controls to this.Controls and it worked.

Actually, I really did get it working. :)
 
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);
}
}

--
 
J.S. said:
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.
 
Back
Top