Dynamic Controls & Naming Conventions

K0D3R

New member
Joined
Feb 2, 2016
Messages
2
Programming Experience
10+
I have a TabControl and i'm creating a new tab, with a RichTextBox inside of it.
The Name of that RichTextBox will depend on the name of the tab (ex: Tab Name is "TEST"(RTB Name will be: TestChatArea))
So far i've been able to create the tab and rtb fine, but i'm having trouble accessing the RTB itself.
I've been searching for days on how to properly do this, as i'm attempting to learn how to make a Light-Weight IRC Client
that has tabs for each channel the user is in (kind of like how mIRC used to be). I keep seeing ways of adding the created control
to a list, but i'm not sure if i'm doing something wrong, or i'm just catching a bad case of the dumb.

Hope some one can help me:

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;


namespace TEST
{
    public partial class Form1 : Form
    {
        IList<RichTextBox> _ChatWindows = new List<RichTextBox>();


        public Form1()
        {
            InitializeComponent();
        }
        private void btnNewTab1_Click(object sender, EventArgs e)
        {
            CreateTab("Test");
        }


        private void CreateTab(string name)
        {
            TabPage newPage = new TabPage();
            newPage.Text = "#" + name;
            newPage.Name = "tb" + name;


            tabControl.TabPages.Add(newPage);


            RichTextBox chatWindow = new RichTextBox();
            chatWindow.BackColor = System.Drawing.Color.Black;
            chatWindow.ForeColor = System.Drawing.Color.White;
            chatWindow.Location = new System.Drawing.Point(6, 6);
            chatWindow.Name = name + "ChatArea";
            chatWindow.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.Vertical;
            chatWindow.Size = new System.Drawing.Size(539, 395);
            chatWindow.ReadOnly = true;
            chatWindow.WordWrap = true;
            chatWindow.Text = "";
            newPage.Controls.Add(chatWindow);


            chatWindow.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right);


            _ChatWindows.Add(chatWindow);
        }


        private void btnTest_Click(object sender, EventArgs e)
        {
            string tabName = tabControl.SelectedTab.Name;
            
           /*What to do here?*/
        }
    }
}
 
You can get the RTB from TabPage.Controls collection by name.
 
Back
Top Bottom