Tab 控件共享 Winform 相同的按钮

发布于 2024-12-18 17:39:47 字数 484 浏览 0 评论 0原文

我想知道是否可以有固定数量的按钮由不同的标签页共享。但是我不知道如何实现这一点。你们有什么想法吗?

这是我的 gui 的屏幕截图,以便大家可以更清楚地了解我的意思。

1

我希望客户、预订和签入/签出列表将共享按钮搜索、编辑、删除和刷新。

是否可以?或者我应该为每个标签页创建差异按钮?

如果我这样做是否正确:

private void buttonSearch_Click(object sender, EventArgs e)
{ 
  if(tabpage.SelectedIndex == 1){ then perform action..}
  if(tabpage.SelectedIndex == 2) {then perform action...}
}

I wonder If it is possible to have fixed number of buttons that is shared by different tab pages. However I don't know how to implement this. Do you guys have any idea.

Heres a screenshot of my gui so that all of you can have a clearer view of what I meant.

1

I want that that the list of Customers, Reservations, and Check In/out will share the buttons search, edit, delete and refresh.

Is it possible? or should I create diff buttons for every tabpage?

is it correct if i do:

private void buttonSearch_Click(object sender, EventArgs e)
{ 
  if(tabpage.SelectedIndex == 1){ then perform action..}
  if(tabpage.SelectedIndex == 2) {then perform action...}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

离鸿 2024-12-25 17:39:47

您可以将按钮放在用户控件中,向用户控件添加一些事件(例如SearchClicked、EditClicked 等)。将用户控件放在选项卡控件之外。

然后,当您更改选项卡 (TabIndexChanged) 时,从前一个选项卡中删除事件处理程序,并为新选项卡添加事件处理程序:

    private void tabControl_TabIndexChanged(object sender, EventArgs e)
    {
        UserControl1.EditClicked -= OldEventHandler;
        UserControl1.EditClicked += NewEventHandler;
    }

You could put the buttons in a User Control, add some events to the User Control (e.g. SearchClicked, EditClicked, etc.). Put the user control outside of the tabcontrol.

Then when you change tabs (TabIndexChanged), remove event handlers from the previous tab, and add event handlers for the new tab:

    private void tabControl_TabIndexChanged(object sender, EventArgs e)
    {
        UserControl1.EditClicked -= OldEventHandler;
        UserControl1.EditClicked += NewEventHandler;
    }
不寐倦长更 2024-12-25 17:39:47

是的,您可以在运行时更改按钮的 .Parent 属性 - 但将按钮移到选项卡控件外部不是更好吗?

Yes, you can change the .Parent property of the buttons at runtime - but wouldn't it be better to just move the buttons outside the tab control?

英雄似剑 2024-12-25 17:39:47

我认为您应该为每个选项卡页面创建不同的按钮,因为每个选项卡页面都在不同的实体上运行。如果只有一组按钮,则必须先检查选择了哪个选项卡页,然后再进行操作。所以你将有一个大方法来做很多事情。

另外,当选择选项卡页时,您还必须编写额外的 UI 代码来移动按钮。

使用不同的按钮,您将拥有高度内聚和松散耦合的代码。更好的设计。更易于维护和管理。更少的代码。

I feel you should create different buttons for every tab page as each one operates on a different entity. If there is only one set of buttons then you will have to first check which tab page is selected and then do the operation. So you will have one big method doing lots of things.

Plus there would be extra UI code that you will have to write to move the buttons when a tab page is selected.

With different buttons you will have highly cohesive and loosely coupled code. Better design. More maintainable and manageable. Less code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文