Click here to Skip to main content
15,885,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a winform with bottoms and rtb.
the bottoms are : next and back.
with next I show wrapped statements. I want to remove the statements from last one to first when back clicked. because the statements have different font and color I cant use
richTextBox1.Text.Remove()
. because it changed color of remaining statements.
also there is a newline between each statements.
how can I remove statements correctly with back bottom?

here is my next bottom program :
private void button_woc2_MouseClick(object sender, MouseEventArgs e) //next
        {
            count++;
         
            write_text(count,page);}

private void write_text(int count,int page)
        {
            if (page == 1)
            {
                switch (count)
                {
                    case 1:
                        pictureBox2.Image = new Bitmap(_12.res.Resource4.p1);
                        pictureBox1.Image = new Bitmap(_12.res.Resource4.head1);
                        richTextBox1.Text = "example1";
                        richTextBox1.AppendText(Environment.NewLine);
                        back = true;
                        break;

                    case 2:

                        richTextBox1.Text += Environment.NewLine + "example2 ";
                        richTextBox1.AppendText(Environment.NewLine);
                        break;

                    case 3:

                        richTextBox1.SelectionStart = richTextBox1.TextLength;
                        richTextBox1.SelectionLength = 0;
                        richTextBox1.SelectionColor = Color.Black;
                        richTextBox1.AppendText(Environment.NewLine + "long statement example");
                        richTextBox1.SelectionColor = richTextBox1.ForeColor;
                        richTextBox1.SelectionFont = new Font("arial", 12);
                        richTextBox1.AppendText(Environment.NewLine);
                        break;

                    case 4:

                        richTextBox1.SelectionStart = richTextBox1.TextLength;
                        richTextBox1.SelectionLength = 0;
                        richTextBox1.SelectionColor = Color.Blue;
                        richTextBox1.AppendText(Environment.NewLine + "example3");
                        richTextBox1.SelectionColor = richTextBox1.ForeColor;
                        richTextBox1.SelectionFont = new Font("arial", 12);
                        richTextBox1.AppendText(Environment.NewLine);
                        break;}
}
}


and this is my back bottom program :

private void button_woc1_Click(object sender, EventArgs e)
        {
           
            loop:
                int start_index;
                int charcount;
                List<string> Lines = richTextBox1.Lines.ToList();

                linecount = Lines.Count;
                if (richTextBox1.Lines[linecount - 1] == "") //detect new line and //delete it
                {
          start_index =richTextBox1.GetFirstCharIndexFromLine(linecount-1 ) - 1;
                    charcount = 1;
                    richTextBox1.Select(start_index, charcount);
                    richTextBox1.SelectedText = " ";
                   
                  goto loop;
                }
else if (richTextBox1.Lines[linecount - 1] == " " ||richTextBox1.Lines[linecount - 1] == "  ")// delete " " after statement deleting
                {
                    start_index = richTextBox1.GetFirstCharIndexFromLine(richTextBox1.GetLineFromCharIndex(richTextBox1.Text.Length-1)) ;
                    charcount =1;
                    richTextBox1.Select(start_index, charcount);
                    richTextBox1.SelectedText = null;
                
                    goto loop;
                }

                else// delete last statement
                {

  start_index = richTextBox1.GetFirstCharIndexFromLine(richTextBox1.GetLineFromCharIndex(richTextBox1.Text.Length));
                    charcount = richTextBox1.Lines[linecount - 1].Length;
                    richTextBox1.Select(start_index, charcount);
                    richTextBox1.SelectedText = " ";
           
                    count--;

     }
   }        
 }


the problem is with new lines, I cant delete them


thank you.

What I have tried:

I also tried :
richTextBox1.Select(start_index, charcount);
                   richTextBox1.SelectedText = " ";

but it didnt work correctly too.
Posted
Updated 11-Jun-22 21:01pm
v3
Comments
PIEBALDconsult 4-Jun-22 10:39am    
Use a RegularExpression maybe?
Ali Valizadegan 4-Jun-22 10:45am    
can you explain with simple example?
[no name] 4-Jun-22 15:32pm    
I think you want "scroll bars" instead.

https://docs.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-display-scroll-bars-in-the-windows-forms-richtextbox-control?view=netframeworkdesktop-4.8
Ali Valizadegan 5-Jun-22 1:25am    
My issue is not about using scroll bar, I want to remove context s in specific order. From last to first with back bottom

As the richttextbox control is rather cumbersome, I would recommend using this
Fast Colored TextBox[^] instead.
 
Share this answer
 
I have completed this by adding some condition and loops like this:

private void button_woc1_Click(object sender, EventArgs e)
       {
           int linecount; //number of lines in page
           next = true;
           if (count <1)
           {
               back = false;
               next = true;
               count = 0;
               this.Refresh();
           }

           if (count >0)
           {

               int start_index, start_index2;
               int charcount, charcount2;
               List<string> Lines = richTextBox1.Lines.ToList();
               linecount = Lines.Count;
               richTextBox1.Text.IndexOf("\n", 0);

               int counter = 1,counter2=0;
               start_index = richTextBox1.GetFirstCharIndexFromLine(richTextBox1.GetLineFromCharIndex(richTextBox1.Text.Length));

           loop:
               Lines = richTextBox1.Lines.ToList();
               linecount = Lines.Count;
               if(linecount>1)
               charcount = richTextBox1.Lines[linecount - 1].Length;
               else
               {
                    //your own text
                 }
                   Lines = richTextBox1.Lines.ToList();
                   linecount = Lines.Count;
                   charcount = richTextBox1.Lines[linecount - 1].Length;
                   goto end;
               }

               if (charcount == 0)
                   charcount = 1;
               richTextBox1.ReadOnly = false;
               richTextBox1.Select(start_index, charcount);

               if (richTextBox1.SelectedText =="")
               {

                   start_index = richTextBox1.GetFirstCharIndexFromLine(richTextBox1.GetLineFromCharIndex(richTextBox1.Text.Length)-counter);
                   counter++;
                   goto loop;
               }
               if(richTextBox1.SelectedText == "\n")
               {
                   richTextBox1.SelectedText = "";
                   counter = 1;
                   goto loop;
               }
               start_index2 = start_index;
           loop2:

               if(richTextBox1.SelectedText !="\n"&& linecount>2)
               {
                   counter2++;
                   start_index2 =richTextBox1.GetFirstCharIndexFromLine(richTextBox1.GetLineFromCharIndex(richTextBox1.Text.Length) - counter2);
                   charcount2 = 1;
                   richTextBox1.DeselectAll();// = string.Empty;
                   richTextBox1.Select(start_index2, charcount2);
                   goto loop2;
               }
                 if (linecount == 2)
               {
                   charcount = richTextBox1.Lines[linecount - 2].Length;
                   start_index = 0;
                   richTextBox1.Select(start_index, charcount);
                   richTextBox1.SelectedText = "";
                   richTextBox1.ReadOnly = true;
                   if (count==1)
                   back = false;
                   count--;

               }
                 else
               {
                   charcount = richTextBox1.Lines[linecount - 2].Length;
                   start_index = start_index2 + 1;
                   richTextBox1.Select(start_index, charcount);
                   richTextBox1.SelectedText = "";
                   richTextBox1.ReadOnly = true;
                   count--;
               }

           end:;
           }



       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900