Replacing items within a string
-
Aha! I knew I was missing something so simple... I am running into my next problem... That removes items in position 3, over 2 spaces. How do I tell it to do ROW 2, position 3, over 2 spaces? And thank you so much for the speedy response.
Goaty65109 wrote:
ROW 2
I don't know too much about it but I do not believe that the stock rich text box supports the concepts of rows.... A cheap easy way to do what you want would be to split your string (Split function) replace the text in the resulting array and then rejoining the array back into a string. Easier than is sounds. Or you can replace your stock control with something like Line Numbers for RichText Control in C#[^]
-
Aha! I knew I was missing something so simple... I am running into my next problem... That removes items in position 3, over 2 spaces. How do I tell it to do ROW 2, position 3, over 2 spaces? And thank you so much for the speedy response.
emailBody.Text = emailBody.Lines[1].Remove(3, 2).Insert(3, "Y");
-
Goaty65109 wrote:
ROW 2
I don't know too much about it but I do not believe that the stock rich text box supports the concepts of rows.... A cheap easy way to do what you want would be to split your string (Split function) replace the text in the resulting array and then rejoining the array back into a string. Easier than is sounds. Or you can replace your stock control with something like Line Numbers for RichText Control in C#[^]
Hey Phantom, I understand what you're saying there and if a rich text box isn't the best way I think I can work backwards on what I want to do then. How about writing the information to a text file, then calling it to the text box once it's updated in the text file? Here is the "flow" of things: Form loads and a .txt file is generated as a template. User checks the box which makes the N turn into a Y in the text file. User inputs the time into a text box. User clicks the "Update Form" button which saves the text document and then pulls the entire document into the rich text box revealing the changes. Does that sound like a better option? This just creates a new situation I have yet to attempt, but is on my "to learn" list, writing information to a specified position in a text file/reading from a text file. Thank you for your response :)
-
emailBody.Text = emailBody.Lines[1].Remove(3, 2).Insert(3, "Y");
Hi Rikki! I tried this method and it appears that this will clear the emailBody and replaces it with just the first line, and replaces the third position with Y. So this Example Blah = N turns to this ExYmple I just want this if possible Example Blah = Y Haha, sorry for my illiteracy, I am trying to be as specific as I can with the knowledge I have. :P
-
Hi Rikki! I tried this method and it appears that this will clear the emailBody and replaces it with just the first line, and replaces the third position with Y. So this Example Blah = N turns to this ExYmple I just want this if possible Example Blah = Y Haha, sorry for my illiteracy, I am trying to be as specific as I can with the knowledge I have. :P
For example:
private void SomeForm_Load(object sender, EventArgs e)
{
string cifTime = timeCIF.Text;
string chkCIF = "N";
emailBody.Text = "Example\nCIF = " + chkCIF + "\nTIME = " + timeCIF.Text;
}private void SomeBtn_Click(object sender, EventArgs e)
{
string[] sLines = emailBody.Lines;
sLines[1] = emailBody.Lines[1].Remove(6, 1).Insert(6, "Y");
emailBody.Lines = sLines;
} -
Hello CP! Alright, I thought this would be simple, but apparently I don't know what I am doing. I have a Rich Text Box that populates upon form load. There are fields that are meant to be updated by text boxes on this form. Here's a snip of the form_load:
private void Form1\_Load(object sender, EventArgs e) { string cifTime = timeCIF.Text; string chkCIF = "N"; emailBody.Text = "Example\\nCIF = " + chkCIF + "\\nTIME = " + timeCIF.Text; }
So what happens here is the form opens and populates a rich text box with: Example CIF = N TIME = Now, what I need to have a button do is update just the items I have defined, in this case chkCIF (a check box) and timeCIF.Text (a textbox). I have a button that's called update, and here's what it looks like.
private void updateButton\_Click(object sender, EventArgs e) { //There is a combo box on this form and example is the first option if (comboInst.Text == "Example") { //Here is where the check box verification is, if it is true it should change the N to Y if (checkCIF.Checked == true) { //This part needs to update the current contents of the email body. Obviously, this isn't working for me so I commented out some examples I tried... //string s = emailBody.Text; //string chkCIF = "Y"; //s = s.Replace("N", chkCIF); string theString = emailBody.Text; theString.Remove(3, 2).Insert(3, "Y"); } }
And there you have it. I just need to be able to update certain defined words from a string with a click of a button. Thanks in advance :)
Why not keep all the individual pieces of text as separate variables and then rebuild the
RichTextBox
[^] using its methods, whenever an item changes? This should make it much easier to add or remove specific items in the future.Use the best guess
-
Hey Phantom, I understand what you're saying there and if a rich text box isn't the best way I think I can work backwards on what I want to do then. How about writing the information to a text file, then calling it to the text box once it's updated in the text file? Here is the "flow" of things: Form loads and a .txt file is generated as a template. User checks the box which makes the N turn into a Y in the text file. User inputs the time into a text box. User clicks the "Update Form" button which saves the text document and then pulls the entire document into the rich text box revealing the changes. Does that sound like a better option? This just creates a new situation I have yet to attempt, but is on my "to learn" list, writing information to a specified position in a text file/reading from a text file. Thank you for your response :)
No, please don't bring a text file into this. It will not make it any easier to replace your substrings and you will just be complicating matters. See Richard's answer below, it seems like a much better solution. Soren Madsen
"When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty
-
Hey Phantom, I understand what you're saying there and if a rich text box isn't the best way I think I can work backwards on what I want to do then. How about writing the information to a text file, then calling it to the text box once it's updated in the text file? Here is the "flow" of things: Form loads and a .txt file is generated as a template. User checks the box which makes the N turn into a Y in the text file. User inputs the time into a text box. User clicks the "Update Form" button which saves the text document and then pulls the entire document into the rich text box revealing the changes. Does that sound like a better option? This just creates a new situation I have yet to attempt, but is on my "to learn" list, writing information to a specified position in a text file/reading from a text file. Thank you for your response :)
-
Why not keep all the individual pieces of text as separate variables and then rebuild the
RichTextBox
[^] using its methods, whenever an item changes? This should make it much easier to add or remove specific items in the future.Use the best guess