简体   繁体   中英

Timer check Winform topmost enabled

how i can make a loop timer that check if in main form topmost.enable is false until a label is visible and then set to true when the label deactive?

If tried this code but not work:

 private void InitializeAlive()
    {
        alive = new System.Timers.Timer();
        alive.Interval = 1000;
        alive.AutoReset = true;
        alive.Elapsed += Alive_Tick;
        alive.Start();
    }

    private void Alive_Tick(object sender, EventArgs e)
    {
        if (lblPassword.Enabled)
        {                
            this.TopMost = false;
        }
        else
        {
            this.TopMost = true;
            alive.Dispose();
        }
    }

    private void btnPrint_Click(object sender, EventArgs e)
    {
        if (txtPassword.Text == pswd)
        {
            TopMost = false;
            webPrintSetting.ShowPageSetupDialog();
            InitializeAlive();
        }
        else
        {
            btnPrint.Enabled = false;
            btnPrint.Visible = false;
            lblPassword.Visible = false;
            txtPassword.Enabled = false;
            txtPassword.Visible = false;
            txtPassword.Clear();
        }
    }

If you only need to do something when 'Enabled' property of the label changes, then you can simply add handler to the 'EnabledChanged' property, like this:

public Form1()
{
    InitializeComponent();

    lblPassword.EnabledChanged += new System.EventHandler(this.LblPassword_EnabledChanged);
}

And implement the handler like this:

private void LblPassword_EnabledChanged(object sender, EventArgs e)
{
    TopMost = !lblPassword.Enabled;
}

I find a solution to toggle on/off topmost (off until a target process is running).

private Timer check;

public MyForm()
{
InitializeCheck();
}

private void InitializeCheck()
{
check = new Timer();
check.Interval = 5000;
check.Tick += Check_Tick;
check.Enabled = false;
}

private void Check_Tick(object sender, EventArgs e)
{
CheckProgram();
}

private void CheckProgram()
{
Process[] program = rocess.GetProcessesByName("notepad");
if (program.Length == 0)
{
check.Enabled = false;
TopMost = true;
}

private void button1_Click(object sender, EventArgs e)
{
TopMost = false;
check.Enabled = true;
}

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