Replace multiples words in text document C# - c#

So I'm trying to get a program running to save me copying and pasting loads of text for android studio. For this I have created listboxes with all the different bits of information required, added button click event to create a document, and another button click event to add the text into the document. So far I'm able to generate all the text when adding one set of latlongs, but I can't seem to work out how to add another set latlongs in..
For example
I need:
googleMap.addMarker(new MarkerOptions().position(new LatLng(-17.79940000000, 31.01680000000)).title(bbb));
googleMap.addMarker(new MarkerOptions().position(new LatLng(-17.80150000000,
31.03650000000)).title(ccc));
But all's I am getting is:
googleMap.addMarker(new MarkerOptions().position(new LatLng(-17.79940000000, 31.01680000000)).title(bbb));
googleMap.addMarker(new MarkerOptions().position(new LatLng(-17.80150000000, 31.01680000000)).title(bbb));
The Longitude value is not changing? I'm hoping all of this makes sense?
string path = Environment.CurrentDirectory + "/" + "latlong.txt";
private void button1_Click(object sender, EventArgs e)
{
if (!File.Exists(path))
{
File.CreateText(path);
MessageBox.Show("File has been created.");
}
}
private void button2_Click(object sender, EventArgs e)
{
using (StreamWriter stwr = new StreamWriter(path))
{
for (int i = 0; i < listBox1.Items.Count; i++)
{
stwr.WriteLine("googleMap.addMarker(new MarkerOptions().position(new LatLng(" + listBox1.Items[i] + ", " + "ii" + ")).title(" + "bbb" + "));");
}
stwr.Close();
string text = File.ReadAllText("latlong.txt");
for (int ii = 0; ii < listBox2.Items.Count; ii++)
{
text = text.Replace("ii", Convert.ToString(listBox2.Items[ii]));
}
File.WriteAllText("latlong.txt", text);
}
}

I guess the problem is that Replace is replacing all occurences of ii, so if you debug your loop you'll see that only the first time iiis replaced by the first item in your listBox2. To solve that,i think you should add the index to ii,something like this
private void button2_Click(object sender, EventArgs e)
{
using (StreamWriter stwr = new StreamWriter(path))
{
for (int i = 0; i < listBox1.Items.Count; i++)
{
stwr.WriteLine("googleMap.addMarker(new MarkerOptions().position(new LatLng(" + listBox1.Items[i] + ", " + "ii" + i + ")).title(" + "bbb" + "));");
}
stwr.Close();
string text = File.ReadAllText("latlong.txt");
for (int ii = 0; ii < listBox2.Items.Count; ii++)
{
text = text.Replace("ii"+ii, Convert.ToString(listBox2.Items[ii]));
}
File.WriteAllText("latlong.txt", text);
}
}
Notice that in the first loop i'm adding "ii" + i and in the second i'm replacing "ii"+ii

Related

Insert data in RichTextBox C#

My code:
private void timer4_Tick(object sender, EventArgs e)
{
for (int a = 0; a < 10; a++)
{
var infos = webBrowser1.Document.GetElementsByTagName("img")[a].GetAttribute("src");
richTextBox1.Text = infos;
}
timer4.Stop();
}
I want to insert all of 10 src values in RichTextBox, while my code do it only once.
You can use AppendText
Replace
richTextBox1.Text = infos;
with
richTextBox1.AppendText(infos);
OR
richTextBox1.Text += infos + Environment.NewLine;
This line is wrong.
richTextBox1.Text = infos;
This is right.
richTextBox1.AppendText= infos;
What your code is doing is setting the text to equal each infos, 10 times over.
So I'm guessing your output would be the last infos variable? What you might want to do instead is this:
private void timer4_Tick(object sender, EventArgs e)
{
for (int a = 0; a < 10; a++)
{
var infos = webBrowser1.Document.GetElementsByTagName("img")[a].GetAttribute("src");
richTextBox1.Text += infos; // the "+=" will add each infos to the textbox
}
timer4.Stop();
}
As you can see, if you use the += instead of just =, it will add each iteration to the whole, instead of just overriding the whole value each time.

Get value of dynamically created textbox

I'm in a bit of a pickle at the moment, I've created a bit of code that creates 4 textboxes and adds them to a table layout at run time (code below) but I'm struggling with getting text from it, I tried getting the value from it as you would string s = TxtBox1.Text.ToString(); but it just gets a null reference, then I tried txt.Text.ToString();and this just gets the text from the last text box that was created.
private void button2_Click(object sender, EventArgs e)
{
int counter;
for (counter = 1; counter <= 4; counter++)
{
// Output counter every fifth iteration
if (counter % 1 == 0)
{
AddNewTextBox();
}
}
}
public void AddNewTextBox()
{
txt = new TextBox();
tableLayoutPanel1.Controls.Add(txt);
txt.Name = "TxtBox" + this.cLeft.ToString();
txt.Text = "TextBox " + this.cLeft.ToString();
cLeft = cLeft + 1;
}
I've looked all over for the answers to this and as of yet found nothing if anybody has any ideas I would be grateful.
Thanks
this code picks textbox1 from tableLayoutPanel1, cast it from Control to TextBox and takes Text property:
string s = ((TextBox)tableLayoutPanel1.Controls["TxtBox1"]).Text;
if you need them all, then iterate over textboxes:
string[] t = new string[4];
for(int i=0; i<4; i++)
t[i] = ((TextBox)tableLayoutPanel1.Controls["TxtBox"+(i+1).ToString()]).Text;
You can try
var asTexts = tableLayoutPanel1.Controls
.OfType<TextBox>()
.Where(control => control.Name.StartsWith("TxtBox"))
.Select(control => control.Text);
That will enumerate the Text value for all child controls of tableLayoutPanel1 where their type is TextBox and their name starts with "TxtBox".
You can optionally relax the filters removing the OfType line (that excludes any non TextBox control) or the Where line (that allow only the control which name matches your example).
Ensure to have
Using System.Linq;
at the beginning of the file.
Regards,
Daniele.
public void AddNewTextBox()
{
txt = new TextBox();
tableLayoutPanel1.Controls.Add(txt);
txt.Name = "TxtBox" + this.cLeft.ToString();
txt.Text = "TextBox " + this.cLeft.ToString();
cLeft = cLeft + 1;
txt.KeyPress += txt_KeyPress;
}
private void txt_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
//the sender is now the textbox, so that you can access it
System.Windows.Forms.TextBox textbox = sender as System.Windows.Forms.TextBox;
var textOfTextBox = textbox.Text;
doSomethingWithTextFromTextBox(textOfTextBox);
}

Writing Array LineWise to a .txt file?

Here is a small part of my program. here i am basically writing a .txt file when the button - 'HideItBtn' is clicked. in this piece of code first the.txt file is created then the value of the ListView SubItem - 'Folder path' is stored in a string array and that array is used to write the text file.
private void HideItBtn_Click(object sender, EventArgs e)
{
string[] strArray = new string[500];
int i = 0;
//Creat Hide.bat and write in it !
StreamWriter hide = new StreamWriter(HideNameTxt.Text + ".txt");
for (int j = 1; j < FolderList.Items[i].SubItems.Count; j++)
{
ListViewItem.ListViewSubItem cur = FolderList.Items[i].SubItems[j];
strArray[i] = cur.Text;
hide.WriteLine("attrib \" + strArray[i] + "\" + Environment.NewLine);
i++;
}
hide.Close();
}
Now the problem:
i run my application and select 3 folders from which show up in the ListView !
But the output .txt file only contains :
attrib "C:\Users\Sand\Desktop\nf"
Non of the other folder r
listed ! i added -"Environment.NewLine" at the end of .WriteLine ! but nothing happened ! Please Help ! Thanks !
You need to have two for loops, not one. Currently you're incrementing i at the end of your for loop, but you've only written one of the sub items, not all of them. You need to have a loop that goes through all items, and another loop to go through all sub-items.
private void HideItBtn_Click(object sender, EventArgs e)
{
using (StreamWriter hide = new StreamWriter(HideNameTxt.Text + ".txt"))
for (int i = 0; i < FolderList.Items.Count; i++)
for (int j = 1; j < FolderList.Items[i].SubItems.Count; j++)
{
ListViewSubItem cur = FolderList.Items[i].SubItems[j];
hide.WriteLine("attrib \"" + cur.Text + "\""
+ Environment.NewLine);
}
}

How to do looping in C# programming?

I have to allow 20 balls to move around the screen. I would like to know how do I use a loop so I would not have to type the codes out long. Currently, the codes I have are
for (int i = 0; i < ballSpeedXAxis.Length; i++)
{
ballSpeedXAxis[i] = 1;
}
for (int i = 0; i < ballSpeedYAxis.Length; i++)
{
ballSpeedYAxis[i] = 1;
}
private void OnUpdate(object sender, object e)
{
Canvas.SetLeft(this.ball1, this.ballSpeedXAxis[1] + Canvas.GetLeft(this.ball1));
Canvas.SetTop(this.ball1, this.ballSpeedYAxis[1] + Canvas.GetTop(this.ball1));
Canvas.SetLeft(this.ball2, this.ballSpeedXAxis[2] + Canvas.GetLeft(this.ball2));
Canvas.SetTop(this.ball2, this.ballSpeedXAxis[2] + Canvas.GetTop(this.ball2));
...
Canvas.SetLeft(this.ball20, this.ballSpeedXAxis[20] + Canvas.GetLeft(this.ball20));
Canvas.SetTop(this.ball20, this.ballSpeedXAxis[20] + Canvas.GetTop(this.ball20));
}
ball1, ball2 ... ball3 are images name.
There are varying ways.. the most obvious being instead of this:
Image ball1;
Image ball2;
Image ball3;
// .. etc ...
You would put those in an array also:
Image[] balls = new Image[20];
..same with your speeds. Then you can change your update method to this:
private void OnUpdate(object sender, object e) {
for (int i = 0; i < balls.Length; i++) {
Canvas.SetLeft(balls[i], ballSpeedXAxis[i] + Canvas.GetLeft(balls[i]));
Canvas.SetTop(balls[i], ballSpeedYAxis[i] + Canvas.GetTop(balls[i]));
}
}
Others include putting the already created images into a List<Image>.. but that's a bit yuck.

C# Need help editing my line numbering code

I got a code for line numbering, it works perfectly fine for numbering lines the regular way but I'm looking for something a little bit different. I want my code to only count line breaks when i press enter(the program receives an return keycode) and not then the textbox automatically cut the lines with word wrapping. This is the code i'm using right now:
//Instructions
int maxLC = 1; //maxLineCount - should be public
private void InstructionsSyncTextBox_KeyUp(object sender, KeyEventArgs e)
{
int linecount = InstructionsSyncTextBox.GetLineFromCharIndex(InstructionsSyncTextBox.TextLength) + 1;
if (linecount != maxLC)
{
InstructionsLineNumberSyncTextBox.Clear();
for (int i = 1; i < linecount + 1; i++)
{
InstructionsLineNumberSyncTextBox.AppendText(Convert.ToString(i) + "\n");
}
maxLC = linecount;
}
}
How i think i would be done easiest is by saving the line count every time someone presses enter to a list and also everytime someone presses enter it updates the line number texbox with every line number at positions said in list. But i have no idea how to detect when an return is removed. Anybody knows how to solve this?
Extremely basic example that counts the lines in your code you posted:
class Program
{
static void Main(string[] args)
{
string stringFromTextBox =
#" int maxLC = 1; //maxLineCount - should be public
private void InstructionsSyncTextBox_KeyUp(object sender, KeyEventArgs e)
{
int linecount = InstructionsSyncTextBox.GetLineFromCharIndex(InstructionsSyncTextBox.TextLength) + 1;
if (linecount != maxLC)
{
InstructionsLineNumberSyncTextBox.Clear();
for (int i = 1; i < linecount + 1; i++)
{
InstructionsLineNumberSyncTextBox.AppendText(Convert.ToString(i) + ""\n"");
}
maxLC = linecount;
}
}";
Regex r = new Regex("\r", RegexOptions.Multiline);
int lines = r.Matches(stringFromTextBox).Count;
//You'll need to run this line every time the user presses a key
}
}

Categories