C# - 在运行时编辑选项卡页面的文本属性

时间:2014-03-27 10:24:14

标签: c# edit tabcontrol tabpage

我有一个标签控件,我可以在其中添加新标签页,在文本框中键入要为每个标签显示的名称。我想要做的是能够右键单击选项卡并重命名(允许我编辑文本),就像许多其他应用程序中的选项...我已经有一个“删除选项卡”选项,工作,但我找不到重命名所选标签的方法..

非常感谢任何帮助!

这是我的删除选项代码,如果可以在任何帮助..

public Form1()
    {
        InitializeComponent();
        ContextMenu cm = new ContextMenu();
        cm.MenuItems.Add("Remove", new EventHandler(rmv_click));
        cm.MenuItems.Add("Rename");
        tabControl1.ContextMenu = cm;


    }
//select tab on right mouse click
        private void tabControl_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                for (int i = 0; i < this.tabControl1.TabCount; ++i)
                {
                    if (this.tabControl1.GetTabRect(i).Contains(new Point(e.X, e.Y)))
                    {
                        this.tabControl1.SelectedIndex = i;
                        break;
                    }
                }
            }
        }

//remove selected tab
    private void rmv_click(object sender, System.EventArgs e)
    {
        tabControl1.TabPages.Remove(tabControl1.SelectedTab);
    }

1 个答案:

答案 0 :(得分:2)

你可以这样做:(快速和肮脏)

 public Form1()
        {
            InitializeComponent();
            ContextMenu cm = new ContextMenu();
            cm.MenuItems.Add("Remove", new EventHandler(rmv_click));
            cm.MenuItems.Add("Rename", new EventHandler(rename_click));
            tabControl1.ContextMenu = cm;
        }

        //select tab on right mouse click
        private void tabControl_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                for (int i = 0; i < this.tabControl1.TabCount; ++i)
                {
                    if (this.tabControl1.GetTabRect(i).Contains(new Point(e.X, e.Y)))
                    {
                        this.tabControl1.SelectedIndex = i;
                        break;
                    }
                }
            }
        }

        //remove selected tab
        private void rmv_click(object sender, System.EventArgs e)
        {
            tabControl1.TabPages.Remove(tabControl1.SelectedTab);
        }

        //rename selected tab
        private void rename_click(object sender, System.EventArgs e)
        {
            var showDialog = this.ShowDialog("Tab Name", "Rename the selected tab");
            tabControl1.SelectedTab.Text = showDialog;
        }

        public string ShowDialog(string text, string caption)
        {
            Form prompt = new Form();
            prompt.Width = 500;
            prompt.Height = 150;
            prompt.Text = caption;
            Label textLabel = new Label() { Left = 50, Top = 20, Text = text };
            TextBox textBox = new TextBox() { Left = 50, Top = 50, Width = 400 };
            Button confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 70 };
            confirmation.Click += (sender, e) => { prompt.Close(); };
            prompt.Controls.Add(confirmation);
            prompt.Controls.Add(textLabel);
            prompt.Controls.Add(textBox);
            prompt.ShowDialog();
            return textBox.Text;
        }
对于Bas Brekelmans的ShowDialog方法的学分。可以找到原始here