Timer check Winform topmost enabled - c#

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;
}

Related

Reuse of null class

Look at the following codes
namespace WindowsFormsApplication1
{
public partial class myprogram : Form
{
public myprogram()
{
InitializeComponent();
}
WebKitBrowser wb1 = new WebKitBrowser();
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
wb1.Navigate("site.com");
timer2.Enabled = true;
}
private void timer2_Tick(object sender, EventArgs e)
{
timer2.Enabled = false;
wb1=null;
timer1.Enabled = true;
}
}
}
To empty RAM every time, wb1 must be null. But then it gets null and no longer hits and says it has already been null. So how can I get null in timer2 and then Navigate in timer1?
Add wb1 = new WebKitBrowser(); to timer1_Tick

How can I manipulate 4 Panel in one form?

I have 4 panels each of one has their own TextBoxes, Buttons, and DataGridView. My problem is only two panels are showing the other is none. When I click the 1st Button I want to show the panel1 and hide the other panels. And when I click the 2nd Button I want to hide the panel1 and the other panel. How can I do it? Can somebody help me with my problem? It this possible to happen?
private void btnItems_Click(object sender, EventArgs e)
{
if (pnlItems.Visible != true)
{
pnlItems.Visible = true;
pnlCustomer.Visible = false;
pnlPOS.Visible = false;
pnlDelivery.Visible = false;
}
}
private void btnCustomers_Click(object sender, EventArgs e)
{
if (pnlCustomer.Visible != true)
{
pnlCustomer.Visible = true;
pnlItems.Visible = false;
pnlPOS.Visible = false;
pnlDelivery.Visible = false;
}
}
private void btnPOS_Click(object sender, EventArgs e)
{
if (pnlPOS.Visible != true)
{
pnlPOS.Visible = true;
pnlCustomer.Visible = false;
pnlItems.Visible = false;
}
}
private void btnDelivery_Click(object sender, EventArgs e)
{
if (pnlDelivery.Visible != true)
{
pnlDelivery.Visible = true;
pnlPOS.Visible = false;
pnlCustomer.Visible = false;
pnlItems.Visible = false;
}
}
private void frmMain_Load(object sender, EventArgs e)
{
pnlItems.Visible = true;
pnlCustomer.Visible = false;
pnlPOS.Visible = false;
pnlDelivery.Visible = false;
}
Let's extract a method:
private void MakePanelVisisble(Panel panel) {
Panel[] panels = new Panel[] {
pnlItems, pnlCustomer, pnlPOS, pnlDelivery,
};
foreach (var p in panels)
p.Visible = (p == panel);
}
Then
private void btnItems_Click(object sender, EventArgs e) {
MakePanelVisisble(pnlItems);
}
private void btnCustomers_Click(object sender, EventArgs e) {
MakePanelVisisble(pnlCustomer);
}
...
private void frmMain_Load(object sender, EventArgs e) {
MakePanelVisisble(pnlItems);
}
Just Give your Panels Tags 1,2,3,4;
the write a method like this:
private void ShowPanel(int id)
{
var panels = myform.Controls.OfType<Panel>();
foreach(Panel p in panels)
p.Visible = (int)p.Tag == id)
}
Then in your buttons use it like:
private void btnPOS_Click(object sender, EventArgs e)
{
ShowPanel(2);
}

Class variable resets after the class is instantiated

So i'm writing an application that has a log in screen followed by a menu screen.
The menu has different buttons enabled for each level of user. To do this when the user logs in, an int is assigned, letting the application know what level the user is logged on and which buttons/reports to enable.
The problem I'm having is when the report is closed and the menu is called up again, the variable is reset back to the default admin level.
Menu M = new Menu(Permission);
M.Show();
this.Hide();
This code is used to pull up the menu from the log in, the int is being passed to a constructor.
public Menu(string Permission)
{
InitializeComponent();
AccessLevel = int.Parse(Permission);
LockKeys();
}
public Menu()
{
InitializeComponent();
LockKeys();
}
This is now the menu code which assigns the int to a class variable.
I've got 10 different reports and when I hit the back key on a report, this is how i call the menu back.
Menu shwMenu = new Menu();
shwMenu.Show();
this.Hide();
Now I understand the problem is that its calling the constructor that doesn't assign the variable. Right now all i can think of doing is bouncing the variable to each report and back, simply to call the correct constructor.
My question is, is there an easier way to do it ? some way to save the class variable so I don't have to re-instantiate it every time the class is called.
public partial class Menu : Form
{
private int AccessLevel;
public Menu(string Permission)
{
InitializeComponent();
AccessLevel = int.Parse(Permission);
LockKeys();
}
public Menu()
{
InitializeComponent();
LockKeys();
}
private void LockKeys()
{
if(AccessLevel == 1)
{
btnSR.Enabled = false;
btnVSC.Enabled = false;
btnPT.Enabled = false;
btnCT.Enabled = false;
btnTS.Enabled = false;
btnBookJob.Enabled = false;
btnCompJob.Enabled = false;
btnCompServ.Enabled = false;
btnVS.Enabled = true;
}
else if (AccessLevel == 2)
{
btnSR.Enabled = false;
btnVSC.Enabled = false;
btnPT.Enabled = false;
btnCT.Enabled = false;
btnTS.Enabled = false;
btnBookJob.Enabled = true;
btnCompJob.Enabled = true;
btnCompServ.Enabled = false;
btnVS.Enabled = false;
}
else if (AccessLevel == 3)
{
btnSR.Enabled = false;
btnVSC.Enabled = false;
btnPT.Enabled = false;
btnCT.Enabled = false;
btnTS.Enabled = false;
btnBookJob.Enabled = true;
btnCompJob.Enabled = false;
btnCompServ.Enabled = true;
btnVS.Enabled = false;
}
else if (AccessLevel == 4)
{
btnSR.Enabled = false;
btnVSC.Enabled = false;
btnPT.Enabled = false;
btnCT.Enabled = false;
btnTS.Enabled = true;
btnBookJob.Enabled = false;
btnCompJob.Enabled = false;
btnCompServ.Enabled = false;
btnVS.Enabled = false;
}
}
private void btnVS_Click(object sender, EventArgs e)
{
VehicleStatR1 VS = new VehicleStatR1();
VS.Show();
this.Hide();
}
private void btnSR_Click(object sender, EventArgs e)
{
ServRequireR3 SR = new ServRequireR3();
SR.Show();
this.Hide();
}
private void btnVSC_Click(object sender, EventArgs e)
{
VehServCompleteR4 VSC = new VehServCompleteR4();
VSC.Show();
this.Hide();
}
private void btnPT_Click(object sender, EventArgs e)
{
PlannedTripR6 PT = new PlannedTripR6();
PT.Show();
this.Hide();
}
private void btnCT_Click(object sender, EventArgs e)
{
ActualTripR7 CT = new ActualTripR7();
CT.Show();
this.Hide();
}
private void btnTS_Click(object sender, EventArgs e)
{
HoursWorkedR8 TS = new HoursWorkedR8();
TS.Show();
this.Hide();
}
private void btnBookJob_Click(object sender, EventArgs e)
{
BookJobServ BJ = new BookJobServ();
BJ.Show();
this.Hide();
}
private void btnCompJob_Click(object sender, EventArgs e)
{
CompJob CJ = new CompJob();
CJ.Show();
this.Hide();
}
private void btnCompServ_Click(object sender, EventArgs e)
{
CompServ CS = new CompServ();
CS.Show();
this.Hide();
}
private void btnLogOut_Click(object sender, EventArgs e)
{
LogIn LI = new LogIn();
this.Hide();
LI.Show();
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
This is the menu class, the one i keep backtracking to but the AccessLevel variable changes to the default value.
Thanks
I think what you are looking for is a "static" variable which is a class level variable.
private static int AccessLevel;
Static variables are defined in a class level and all instances of the class can access them and modify them. In other words, static variables are shared between class instances. more information: https://msdn.microsoft.com/en-us/library/98f28cdx.aspx?f=255&MSPPError=-2147217396
Another option would be to use a static class that holds all the information you need, for example:
public static class CurrentUser
{
public static string UserName { get; set; }
public static int AccessLevel { get; set; }
}
Which you use when your user logs in:
public void LogUserIn(string userName, string password)
{
//Make sure to reset, so an invalid login doesn't retain data
LogUserOut();
//Do the user validation here
CurrentUser.UserName = userName;
CurrentUser.AccessLevel = 0; //whatever was read from your user database
}
public void LogUserOut()
{
CurrentUser.UserName = "";
CurrentUser.AccessLevel = int.MinValue;
}
And then, when you need to use the access level you don't need to worry about passing values around, for example your Menu form would look like this:
public partial class Menu : Form
{
public Menu()
{
InitializeComponent();
LockKeys();
}
private void LockKeys()
{
if(CurrentUser.AccessLevel == 1)
{
//Enable/Disable Controls
}
else if (CurrentUser.AccessLevel == 2)
{
//Enable/Disable Controls
}
else if (CurrentUser.AccessLevel == 3)
{
//Enable/Disable Controls
}
else if (CurrentUser.AccessLevel == 4)
{
//Enable/Disable Controls
}
}
}
As a side note, in order to avoid code duplication and to increase security, you should create your initialization function LockKeys like this:
private void LockKeys()
{
//Set them all default false, and then only enable.
btnSR.Enabled = false;
btnVSC.Enabled = false;
btnPT.Enabled = false;
btnCT.Enabled = false;
btnTS.Enabled = false;
btnBookJob.Enabled = false;
btnCompJob.Enabled = false;
btnCompServ.Enabled = false;
btnVS.Enabled = false;
if(AccessLevel == 1)
{
btnVS.Enabled = true;
}
else if (AccessLevel == 2)
{
btnBookJob.Enabled = true;
btnCompJob.Enabled = true;
}
else if (AccessLevel == 3)
{
btnBookJob.Enabled = true;
btnCompServ.Enabled = true;
}
else if (AccessLevel == 4)
{
btnTS.Enabled = true;
}
}

Paint on next frame on Windows's graphics?

I need to draw some things on the screen for only 1 frame.
Things such as a line of text.
I tried to achieve this with moving windows, but I failed. I cannot get them to show for only 1 frame (8-16 msec), either by showing/hiding or moving in and out of place.
Is there any way to do this?
(Urgh, for the curious, I'm doing this for someone else, so rationalizing over the reason why this needs to be done is useless.)
Edit: Last thing I tried:
public partial class Form2 : Form
{
static Random rand = new Random();
public void ShowString(string s)
{
this.label1.Text = s; // Has .AutoSize = true
this.Size = this.label1.Size;
var bnds = Screen.PrimaryScreen.WorkingArea;
bnds.Size -= this.Size;
this.Location = new Point(rand.Next(bnds.X, bnds.Right), rand.Next(bnds.Y, bnds.Bottom));
}
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (Location.X != -10000 && Location.Y != -10000)
{
Location = new Point(-10000, -10000);
timer1.Interval = Program.interval; // How long to wait before showing two things.
}
else
{
timer1.Interval = Program.delay; // For how long to show.
ShowString("just something to test");
}
}
}
And before that:
public partial class Form2 : Form
{
static Random rand = new Random();
public void ShowString(string s)
{
this.label1.Text = s; // Has .AutoSize = true
this.Size = this.label1.Size;
var bnds = Screen.PrimaryScreen.WorkingArea;
bnds.Size -= this.Size;
this.Location = new Point(rand.Next(bnds.X, bnds.Right), rand.Next(bnds.Y, bnds.Bottom));
}
public Form2()
{
InitializeComponent();
timer1.Interval = Program.interval;
}
private void Form2_Load(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void Form2_Move(object sender, EventArgs e)
{
if (Location.X != -10000 && Location.Y != -10000)
{
Thread.Sleep(Program.delay); // Dirty cheat, but I was just trying.
this.Location = new Point(-10000, -10000);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
ShowString("just something to test");
}
}

How to prevent firing CheckedChanged event when checking a control programmatically?

How do I prevent firing CheckedChanged event when checking a control programmatically?
I usually do this the following way.
private bool isFrozen = false;
private void btn1_CheckedChanged(object sender, EventArgs e)
{
if (isFrozen)
return;
isFrozen = true;
btn2.Checked = false;
isFrozen = false;
// Do some stuff
}
private void btn2_CheckedChanged(object sender, EventArgs e)
{
if (isFrozen)
return;
isFrozen = true;
btn1.Checked = false;
isFrozen = false;
// Do another stuff
}
Is there a better or more common solution?
I think your way is fine.
The other way to do it is remove the EventHandler before the check, and then add it back again after the check. This way eliminates the need for the isFrozen variable.
private void btn1_CheckedChanged(object sender, EventArgs e)
{
btn2.CheckedChanged -= btn2_CheckedChanged;
btn2.Checked = false;
btn2.CheckedChanged += btn2_CheckedChanged;
// Do some staff
}
private void btn2_CheckedChanged(object sender, EventArgs e)
{
btn1.CheckedChanged -= btn1_CheckedChanged;
btn1.Checked = false;
btn1.CheckedChanged += btn1_CheckedChanged;
// Do another staff
}
In VB:
RemoveHandler btn2.CheckedChanged, AddressOf btn2_CheckedChanged
btn2.Checked = false
AddHandler btn2.CheckedChanged, AddressOf btn2_CheckedChanged
I came across this post after wanting to implement something like this for a while. I regularly use Measurement Studio from National Instruments, and their WinForms controls that have the event StateChanging or StateChanged pass a parameter of type ActionEventArgs, which has a property Action which can take three values: ByKeyboard, ByMouse and Programatic. This is very useful in determining what has caused the state of the control to change. I wanted to replicate this in a standard WinForms checkbox.
Here is my code:
public enum ControlSource
{
Programatic,
ByKeyboard,
ByMouse
}
public class AwareCheckBox : Checkbox
{
public AwareCheckBox()
: base()
{
this.MouseDown += AwareCheckbox_MouseDown;
this.KeyDown += AwareCheckbox_KeyDown;
}
private ControlSource controlSource = ControlSource.Programatic;
void AwareCheckbox_KeyDown(object sender, KeyEventArgs e)
{
controlSource = ControlSource.ByKeyboard;
}
void AwareCheckbox_MouseDown(object sender, MouseEventArgs e)
{
controlSource = ControlSource.ByMouse;
}
public new event AwareControlEventHandler CheckedChanged;
protected override void OnCheckedChanged(EventArgs e)
{
var handler = CheckedChanged;
if (handler != null)
handler(this, new AwareControlEventArgs(controlSource));
controlSource = ControlSource.Programatic;
}
}
public delegate void AwareControlEventHandler(object source, AwareControlEventArgs e);
public class AwareControlEventArgs : EventArgs
{
public ControlSource Source { get; private set; }
public AwareControlEventArgs(ControlSource s)
{
Source = s;
}
}
I'm sure there are improvements to make, but my rudimentary testing has demonstrated that it works. I have posted here simply in case others stumble across this issue and want a clearer way of distinguishing where the change was initiated. Any comments welcome.
Just have a counter value set and check for the value in the beginning of the event. It solved my problem in 10 minutes. I am using 5 slide buttons in Xamarin to make it as a radio button.
private void testtoggle1(object sender, ToggledEventArgs e)
{
if (chk_ctr == 1) { return; }
chk_ctr = 1;
sw2.IsToggled= false;
sw3.IsToggled = false;
sw4.IsToggled = false;
sw5.IsToggled = false;
chk_ctr = 0;
}
private void testtoggle2(object sender, ToggledEventArgs e)
{
if (chk_ctr == 1) { return; }
chk_ctr = 1;
sw1.IsToggled = false;
sw3.IsToggled = false;
sw4.IsToggled = false;
sw5.IsToggled = false;
chk_ctr = 0;
}
private void testtoggle3(object sender, ToggledEventArgs e)
{
if (chk_ctr == 1) { return; }
chk_ctr = 1;
sw1.IsToggled = false;
sw2.IsToggled = false;
sw4.IsToggled = false;
sw5.IsToggled = false;
chk_ctr = 0;
}
private void testtoggle4(object sender, ToggledEventArgs e)
{
if (chk_ctr == 1) { return; }
chk_ctr = 1;
sw1.IsToggled = false;
sw2.IsToggled = false;
sw3.IsToggled = false;
sw5.IsToggled = false;
chk_ctr = 0;
}
private void testtoggle5(object sender, ToggledEventArgs e)
{
if (chk_ctr == 1) { return; }
chk_ctr = 1;
sw1.IsToggled = false;
sw2.IsToggled = false;
sw3.IsToggled = false;
sw4.IsToggled = false;
chk_ctr = 0;
}

Categories