how can i define an array of richtextbox while using in for loop

poringgunner

Active member
Joined
Feb 23, 2014
Messages
42
Programming Experience
Beginner
i have problems on how to use array..
i want to define an array of richtextbox regardless on the count.
here is my code.

                    if(tabControl1.TabPages.Count == browse.FileNames.Count())
                    {
                        for (int i = 0; i < browse.FileNames.Count(); i++)
                        {                         
                            RichTextBox richtext = (RichTextBox)tabControl1.TabPages[i].Controls["richtext" + i.ToString()];

                        }
                        if (richtext.Lines.Count() > richtext.Lines.Count())                        //the error here is the richtext
                        {
                            sub = richtext.Lines.Count() - richtext.Lines.Count();               //and here also.
                        }
                    
                    }

i want the richtext variable increment by 1 on the loop
example richtext1 then after the loop it will be richtext2
 
Last edited by a moderator:
Can we assume that there is one RichTextBox per TabPage? If so then what you should be doing is using a `foreach` loop to enumerate the TabPages and then, for each one, getting the one and only RichTextBox that it contains. The names are irrelevant in that case. E.g.
foreach (TabPage page in tabControl1.TabPages)
{
    var rtb = page.Controls.OfType<RichTextBox>().Single();

    // Use rtb here.
}
 
i have only the tabcontrol and browse file button in form.. i created the richtextbox programmatically. here is my code
                foreach (String file in browse.FileNames)
                {
                    //Create Tab control and tab pages
                    TabPage tab = new TabPage() { Text = System.IO.Path.GetFileName(file) };
                    tabControl1.TabPages.Add(tab);
                    tabControl1.SelectedTab = tab;
                    //Create richtextbox                 
                    RichTextBox rich = new RichTextBox { Parent = tab, Dock = DockStyle.Fill, ReadOnly = true };
                    rich.LoadFile(file, RichTextBoxStreamType.PlainText);
                    //Remove the blank lines and comments                
                    rich.Text = Regex.Replace(rich.Text, @"/\*()-_+=,.:;'\[]~`{}|<>*(.*?)\*/", "", RegexOptions.Multiline);
                    rich.Text = Regex.Replace(rich.Text, @"/\*(.*?)\n", "", RegexOptions.Multiline);
                    rich.Text = Regex.Replace(rich.Text, @"//*(.*?)\n", "", RegexOptions.Multiline);
                    rich.Text = Regex.Replace(rich.Text, @"//()-_+=,.:;'\[]~`{}|<>*(.*?)//", "", RegexOptions.Multiline);
                    rich.Text = Regex.Replace(rich.Text, @"^\s*$(\n)", "", RegexOptions.Multiline);
                    RegexOptions options = RegexOptions.None;
                    Regex regex = new Regex(@"[ ]{2,}", options);
                    rich.Text = regex.Replace(rich.Text, @"");

i need to get the name of all richtextboxes to be created.. but i only get rich name.
i need it because i will compare the text inside of richtextboxes.
 
Last edited by a moderator:
Firstly, I had already formatted the code snippet in your first post for you and now I've had to do so for your second post too. If you are going to post code snippets, please format them for us so that they are comfortably readable.
i need to get the name of all richtextboxes to be created.. but i only get rich name.
i need it because i will compare the text inside of richtextboxes.
That only reinforced what I said before. Why would getting the names help you compare text inside the RichTextBoxes? The only way to get the text from a RichTextBox is to have the RichTextBox first. The only use the name could be is to allow you to get the RichTextBox by name but, in this case, you'd have to have the RichTextBox first in order to get the name, so why get the name so that you can get a RichTextBox that you already have? Not only that, you aren't even giving the RichTextBoxes names when you create them so there are no names to get.

I've already shown you how to get the RichTextBoxes in a loop. You could use a loop like that to add them to an array or collection, or you could create an array or collection in one line using LINQ, e.g.
var rtbs = tabControl1.TabPages
                      .Cast<TabPage>()
                      .Select(tp => tp.Controls.OfType<RichTextBox>().Single())
                      .ToArray();
 
Back
Top Bottom