动态创建的RichTextBoxes的TextChanged事件

时间:2022-10-22 07:48:45

I'm trying to do a notepad with chrome-like tabs on it. I have a "New Page" button on my page. When I click on it, it creates a new tabpage with a richtexbox on it. The richboxes are created like this

我正在尝试使用带有铬状标签的记事本。我的页面上有一个“新页面”按钮。当我点击它时,它会创建一个带有richtexbox的新标签页。富客盒就是这样创建的

public void yeni()
    {
        //create a new tabpage
        TabPage newPage = new TabPage("Not-" + (tabControl1.TabPages.Count + 1));

        //create a new richtexbox
        RichTextBox rtb = new RichTextBox();

        int rtbname = tabControl1.TabPages.Count + 1;
        rtb.Name = "richTextBox" + rtbname.ToString();
        rtb.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top);
        rtb.BorderStyle = BorderStyle.None;
        rtb.Width = 778;
        rtb.Height = 395;
        rtb.Location = new Point(0, 4);
        rtb.HideSelection = false;
        rtb.Font = new Font("Lucida Console", 9.75f);
        rtb.ForeColor = Color.Maroon;

        //add rtb to the tabpage
        newPage.Controls.Add(rtb);
        tabControl1.TabPages.Add(newPage);

        //make the new created tab the selected one
        tabControl1.SelectedTab = tabControl1.TabPages[tabControl1.TabPages.Count - 1];

        //selectedRtb.Text = null;
        openFileDialog1.FileName = null;
    }

Now I create a RichTextBox and the name of that rtb is richTextBox*indexofthetabhere*. So if I'm working on the second tabpage, the name of the rtb is "richTextBox2". Now what I'm trying to do is I want an textchanged event for the richtextbox on the selected tabpage. I'm getting the selected richtextbox with this code here.

现在我创建一个RichTextBox,该rtb的名称是richTextBox * indexofthetabhere *。因此,如果我正在处理第二个标签页,则rtb的名称为“richTextBox2”。现在我要做的是我想在所选标签页上为richtextbox创建一个textchanged事件。我在这里使用此代码获取所选的richtextbox。

private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
    {

        selectedone = "richTextBox" + (tabControl1.SelectedIndex+1).ToString();


            selectedRtb = (RichTextBox)tabControl1.SelectedTab.Controls[selectedone];
            textBox2.Text = selectedone;
    }

Now here I get the selected tab index and i get the rtb name then I get the selected rtb as "selectedRtb". Now I can't make a textchanged event for this. I don't know what to do actually. I tested if the above code was working and yes I get the rtb names right. But I can't use them because I don't know how to do.. Thanks for the help.

现在我在这里获得选定的选项卡索引,然后我得到rtb名称,然后我将选中的rtb作为“selectedRtb”。现在我无法为此创建一个textchanged事件。我不知道该怎么做。我测试了上面的代码是否正常工作,是的,我得到了正确的rtb名称。但我不能使用它们,因为我不知道该怎么办..感谢您的帮助。

3 个解决方案

#1


3  

    public void yeni()
    {
       //....
       RichTextBox rtb = new RichTextBox();
       rtb.Name = "richTextBox" + selectedTabPageIndex.ToString();
       rtb.TextChanged += rtb_TextChanged;
        //....    
     }



     void rtb_TextChanged(object sender, EventArgs e)
     {
          RichTextBox rtb = (RichTextBox)sender;

          if (rtb.Name == "richTextBox" + selectedTabPageIndex.ToString())
          {
              //rtb is selected page richtextbox
              //......
          }
      }

#2


0  

You don't know how to create events? Or you can't access something while knowing it's name (use reflection)?

你不知道如何创建活动?或者你知道它的名字(使用反射)时你无法访问某些东西?

#3


0  

Alright I solved my problem. Here is the answer;

好吧,我解决了我的问题。这是答案;

selectedRtb.TextChanged += (bs, be) =>
        {
           //whatever you want to do
        };

Simply added this to my code after I created the rtb, and it worked. Thanks to everyone who helped.

在我创建rtb之后,只需将此添加到我的代码中,它就可以了。感谢所有帮助过的人。

#1


3  

    public void yeni()
    {
       //....
       RichTextBox rtb = new RichTextBox();
       rtb.Name = "richTextBox" + selectedTabPageIndex.ToString();
       rtb.TextChanged += rtb_TextChanged;
        //....    
     }



     void rtb_TextChanged(object sender, EventArgs e)
     {
          RichTextBox rtb = (RichTextBox)sender;

          if (rtb.Name == "richTextBox" + selectedTabPageIndex.ToString())
          {
              //rtb is selected page richtextbox
              //......
          }
      }

#2


0  

You don't know how to create events? Or you can't access something while knowing it's name (use reflection)?

你不知道如何创建活动?或者你知道它的名字(使用反射)时你无法访问某些东西?

#3


0  

Alright I solved my problem. Here is the answer;

好吧,我解决了我的问题。这是答案;

selectedRtb.TextChanged += (bs, be) =>
        {
           //whatever you want to do
        };

Simply added this to my code after I created the rtb, and it worked. Thanks to everyone who helped.

在我创建rtb之后,只需将此添加到我的代码中,它就可以了。感谢所有帮助过的人。