简体   繁体   中英

C# How to fix run data show in RichTextBox by instand

I have a problem about showing data in RichTextBox by selected tab, but I don't need to show in the selected tab, I need show by RichTextBox for run.

    private void btn_Runserver_Click(object sender, EventArgs e)
    {
       AddTab();
       StartCMD();

    }

    private void AddTab()
    {
        TabPage newTab = new TabPage((string)cbConfig.SelectedItem);

        RichTextBox rtb = new RichTextBox();

        rtb.Dock = DockStyle.Fill;
        rtb.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.Vertical;
        rtb.BorderStyle = System.Windows.Forms.BorderStyle.None;
        rtb.BackColor = System.Drawing.Color.White;
        rtb.ReadOnly = true;

        newTab.Tag = rtb;
        newTab.Name = (string)cbConfig.SelectedItem;
        newTab.Controls.Add(rtb);

        tabControl.Controls.Add(newTab);
        tabControl.SelectTab(newTab);
    }

    private void build_ErrorDataReceived(object sender, DataReceivedEventArgs e)
    {
        string strMessage = e.Data;
        if (tabControl.InvokeRequired)
        {
            tabControl.Invoke(new Action(() =>
            {
                RichTextBox rtb = (RichTextBox)tabControl.SelectedTab.Tag;
                rtb.AppendText(strMessage + Environment.NewLine);
                rtb.Select(rtb.Text.Length - 1, 0);
                rtb.ScrollToCaret();
            }));
        }
    }

Code RunCMD Form

        proc.OutputDataReceived += build_ErrorDataReceived;
        proc.BeginOutputReadLine();

Now my problem is that if I run the program and data show in RichTextbox by selected tab, but I need to show data in RichTextbox On RichText Tab Safe

Someone said "change RichTextBox rtb = (RichTextBox)tabControl.SelectedTab.Tag; , don't use SelectedTab ", but I not know how to change it accordingly.

I believe you are adding RichTextBox in one event and adding the text to it in another event. So there is a post back while adding the text. In this case, you need to re-add the controls which are dynamically created. Refer This Link which explains how to handle dynamic controls in asp.net.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM