Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find text and change state.

Status
Not open for further replies.

BrasilianGuy

IS-IT--Management
Oct 27, 2005
25
US
HI everybody

I'm using the following code to find a especific string in a txt file and then change the properties of the object. the text is there but it's not changing the textbox color as I want it to do.

StreamReader tr = new StreamReader("Numbers.txt");
Regex rx = new Regex(tr.ToString());
string thisbox = "14";
if (rx.IsMatch(thisbox))
{
this.textBox1.BackColor = Color.FromKnownColor(KnownColor.LightSalmon);
}
tr.Close();


Thanks
 
You are not using a regex correctly.

In fact, I wouldn't use a regex at all in this case.

Simply use string.Contains

string doc = tr.ReadToEnd();

if (doc.Contains("14"))
{
//change your color here
}


This operation will be a bit slow though if the document is large.
 
Now...

I'm trying to do this.... I'm building a bingo app as my first app.

I got it to save the number inside of a text box in a txt file so when we call the app again it will remember the numbers that were clicked last time.

The problem is when I click on textboxt 2 it overides the content in the txt file and deletes the content of textbox1.


private void TextBox1_Click(object sender, EventArgs e)
{


if (this.textBox1.BackColor == Color.FromKnownColor(KnownColor.LightSalmon))
{
this.textBox1.BackColor = Color.FromKnownColor(KnownColor.White);
StreamWriter tw = new StreamWriter("Numbers.txt");
string result = Regex.Replace(tw.ToString(), "14,", "");
tw.Close();
}
else
{
this.textBox1.BackColor = Color.FromKnownColor(KnownColor.LightSalmon);
StreamWriter tw = new StreamWriter("Numbers.txt");
tw.WriteLine("14,");
tw.Close();
}
}

private void TextBox2_Click(object sender, MouseEventArgs e)
{
if (this.textBox2.BackColor == Color.FromKnownColor(KnownColor.LightSalmon))
{
this.textBox2.BackColor = Color.FromKnownColor(KnownColor.White);
this.textBox1.BackColor = Color.FromKnownColor(KnownColor.White);
StreamWriter tw = new StreamWriter("Numbers.txt");
string result = Regex.Replace(tw.ToString(), "18,", "");
tw.Close();
}
else
{
this.textBox2.BackColor = Color.FromKnownColor(KnownColor.LightSalmon);
this.textBox1.BackColor = Color.FromKnownColor(KnownColor.LightSalmon);
StreamWriter tw = new StreamWriter("Numbers.txt");
tw.WriteLine("18,");
tw.Close();

}
}



Is this thing happennign because the file is being recreated everytime I click on one of the 2 textboxes, instead of updated??

If yes... is it what regex do?

How can I solve this problem
 
I'd be interested in seeing this app once you have finished it. Fire me an email with it attached and I'll give you some quick pointers for future design.

email llamachant%at%yahoo%dot%ca

In the meantime - I'm not sure what's going on here. You are trying to replace a called number with a blank?

I'm assuming your text file is comma delimited. So really what you need is a stream reader, and a stream writer.

StreamReader reader = new StreamReader("Numbers.txt");
string nums = reader.ReadToEnd();

nums = nums.Replace("18,", ""); //the replace() method returns a string rather than modifying the string itself

reader.Close();

System.IO.File.Delete("Numbers.txt");

StreamWriter writer = new StreamWriter("Numbers.txt");
writer.Write(nums);

writer.Close();



This is the point where I will tell you that depending on a text file is a bad thing during the runtime of your application. Generally you would save a "snapshot" of your current data and then reload that snapshot. This implies that you only ever touch the file when saving before you exit, or loading when you start the app. When you email me your application I will show you what I mean.
 
Ok.... I scraped the main idea of saving the states of each textbox one by one and went for a saving button that will read trough the for and save all textboxes with back color lightsalmon into a txt file.

here is my code:

so I have 25 of these (one for each number of my bingo sheet)

public void TextBox1_Click(object sender, MouseEventArgs e)
{
if (this.textBox1.BackColor == Color.FromKnownColor(KnownColor.LightSalmon))
this.textBox1.BackColor = Color.FromKnownColor(KnownColor.White);
else
this.textBox1.BackColor = Color.FromKnownColor(KnownColor.LightSalmon);
}


and I have a save button with this action


public void SaveGame_Click(object sender, EventArgs e)
{
//string[] myTextboxes;
TextBox Tb = new TextBox();
for (int boxes = 0; boxes <= 25; boxes++)
{

if ( ((TextBox)Tb).BackColor== Color.LightSalmon)
{
StreamWriter writer = new StreamWriter("Numbers.txt");
writer.Write(Tb);
writer.Close();
}
}
}


The button code is not breaking anywhere but it is not saving anything int the txt file

Any help??
 
foreach (Control t in Controls)
{
if (t.BackColor == Color.FromKnownColor(KnownColor.LightSalmon))
{
StreamWriter writer = new StreamWriter("Numbers.txt");
writer.Write(t.ToString()+",");
writer.Close();
}
}


this code at least saves ONE of the values in the txt file.... but it only saves one The first one picked
 
On each iteration your code replaces the file.

Open the file, do the iteration etc., close the file.

Hope this helps.

[vampire][bat]
 
OHHHHHH.......

So close....


StreamWriter writer = new StreamWriter("Numbers.txt");
foreach (Control t in Controls)
{
if (t.BackColor == Color.FromKnownColor(KnownColor.LightSalmon))
{

writer.Write(t.ToString()+",");

}
}
writer.Close();



Nice.... Thanks

As soon as a read your post I new exactly what you were talking about....

Thank again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top