单击时需要按钮更改文本,然后在外部单击时更​​改回来

时间:2017-07-04 00:55:48

标签: c# visual-studio

我有一个“退出”按钮,可以根据变量更改文本。单击时,变量增加1,当var为2时应用程序退出。

public static int QuitVar = 0;

        private void quitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            QuitVar = QuitVar + 1;

            if (QuitVar == 0)
            {
                quitToolStripMenuItem.Text = "Quit";
            }

            if (QuitVar == 1)
            {
                quitToolStripMenuItem.Text = "Really?";
            }

            if (QuitVar == 2)
            {
                QuitVar = 0;
                Application.Exit();
            }

当用户点击按钮外,我希望QuitVar更改回0,因此按钮文本为“退出”。有没有办法在C#中做到这一点?

2 个答案:

答案 0 :(得分:0)

用户可以点击的地方太多而不是退出按钮。可能更容易的是检测鼠标何时离开带有MouseLeave event的退出按钮。

通过转到Control属性(我在这里使用了Button)和Events(灯光符号),然后双击MouseLeave事件内部,它将为您生成模板事件处理程序方法:
enter image description here

在该事件处理程序中,正确设置文本和from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import matplotlib.cm as cm # import colormap stuff! import numpy as np fig = plt.figure() ax = fig.add_subplot(111, projection='3d') x, y = np.random.rand(2, 100) * 4 hist, xedges, yedges = np.histogram2d(x, y, bins=4, range=[[0, 4], [0, 4]]) # Construct arrays for the anchor positions of the 16 bars. # Note: np.meshgrid gives arrays in (ny, nx) so we use 'F' to flatten xpos, # ypos in column-major order. For numpy >= 1.7, we could instead call meshgrid # with indexing='ij'. xpos, ypos = np.meshgrid(xedges[:-1] + 0.25, yedges[:-1] + 0.25) xpos = xpos.flatten('F') ypos = ypos.flatten('F') zpos = np.zeros_like(xpos) # Construct arrays with the dimensions for the 16 bars. dx = 0.5 * np.ones_like(zpos) dy = dx.copy() dz = hist.flatten() cmap = cm.get_cmap('jet') # Get desired colormap max_height = np.max(dz) # get range of colorbars min_height = np.min(dz) # scale each z to [0,1], and get their rgb values rgba = [cmap((k-min_height)/max_height) for k in dz] ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color=rgba, zsort='average') plt.show()

QuitVar

答案 1 :(得分:0)

您可以尝试创建自定义ToolstripMenuItem,并覆盖WndProc方法 覆盖可以捕获Windows消息,并在点击不在项目上时通知。如果用户点击表单外,下面的代码会根据代码进行调整以关闭表单,但它应该适合您的目的。

/// <summary>
    /// Function to capture if the user clicks outside of the control, in which case need to close this form.
    /// </summary>
    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);

        // if click outside dialog -> Close Dlg
        if (m.Msg == 0x86) //0x86 = NativeConstants.WM_NCACTIVATE
        {
            if (this.Visible)
            {
                if (!this.RectangleToScreen(this.DisplayRectangle).Contains(Cursor.Position))
                    QuitVar = 0;
            }
        }
    }