0

タブページの右クリックでコンテキストメニューを表示するコードを作成しました。ユーザーがコンテキストメニューから[タブの削除]をクリックしたときに、実際にタブページを削除するにはどうすればよいですか?私はここまで到達しました。(unloadProfileは私のコンテキストメニュー項目です)。コンテキストメニューが関連付けているタブページを取得して削除する方法がわかりません。どんな助けでも大歓迎です。

// My Context Menu
private void tabControl_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            // iterate through all the tab pages
            for (int i = 0; i < tabControl.TabCount; i++)
            {
                // get their rectangle area and check if it contains the mouse cursor
                Rectangle r = tabControl.GetTabRect(i);
                if (r.Contains(e.Location))
                {
                    // show the context menu here
                    this.contextMenuStrip1.Show(this.tabControl, e.Location);
                }
            }
        }
    }

// Context menu click event
private void unloadProfile_Click(object sender, EventArgs e)
    {
        // iterate through all the tab pages
        for (int i = 0; i < tabControl.TabCount; i++)
        {

        }
    }
4

1 に答える 1

4

これが正しい方法ではないと思いますが、機能します。

tabControl1_MouseClick(object sender、MouseEventArgs e)イベントで、menustripのTagプロパティを選択したTabPageに設定します。

// show the context menu here
this.contextMenuStrip1.Tag = this.tabControl1.TabPages[i];
this.contextMenuStrip1.Show(this.tabControl1, e.Location);

そして、removeTabToolStripMenuItem_Click(object sender、EventArgs e)イベントで、Tagプロパティを使用してタブページを削除します

this.tabControl1.TabPages.Remove(this.contextMenuStrip1.Tag as TabPage);

ヌルチェックが良いでしょう:)それが役立つことを願っています。

于 2011-02-16T01:36:48.020 に答える