c# winforms, focus on form until form.Close() - c#

I have winform app that have main form and logging form. When logging form is shown I want it to have focus until it will be closed. I tried:
loggingForm = new LoggingForm();
loggingForm.FormClosing += loggingForm_FormClosing;
loggingForm.bOK.Click += bOK_Click;
loggingForm.Show();
loggingForm.Activate();
loggingForm.Focus();
loggingForm.TopMost = true;
loggingForm.TopMost = false;
void loggingForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (isValidPass)
e.Cancel = false;
else
e.Cancel = true;
}

Try this:
loggingForm.ShowDialog();

Related

Close nested form

So i have that aplication:
Menu Picture
and when i press the button "Terminar sessão" it's supposed to log out (and its works)
but when i went back to login the menu does not close!
Login Picture after log out
I am using nested form, the form ends session is closing but not the menu.
This is my menu.cs:
private Form activeForm = null;
private void openChildForm(Form childForm)
{
if (activeForm != null) activeForm.Close();
activeForm = childForm;
childForm.TopLevel = false;
childForm.FormBorderStyle = FormBorderStyle.None;
childForm.Dock = DockStyle.Fill;
panelChildForm.Controls.Add(childForm);
panelChildForm.Tag = childForm;
childForm.BringToFront();
childForm.Show();
}
private void button2_Click(object sender, EventArgs e)
{
panel1.Visible = false;
openChildForm(new Transferências());
Slidepanel.Height = (button2.Height - 15);
Slidepanel.Top = (button2.Top + 10);
}
and on "Transferências" form i have it:
private void Sessao_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Tem a certeza que deseja terminar sessão?", "Terminar Sessão", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
this.Hide();
Login login = new Login();
login.ShowDialog();
}
}
i already tried Menu.Close(); but it does not work
With the amount of information you provide, it is quite hard to tell what the problem might be. Did you try to debug it to verify that your program actually tries to execute Menu.Close()?
If you show your code people might be able to help you more.

C# windows app Minimize in system tray error

Hi Friends i have written the program for minimize the form into system tray it is working fine for the minimize and in tray i can see new icon is appeared.
and in double click it restores, But the error is when i restore the form into its normal state then some labels are automatically goes visible false and the timer i have used in form that timer automatically stops please let me know if anyone have faced the same issue and have any idea about this issue.
please refer below code
private void User_Projects_SizeChanged(object sender, EventArgs e)
{
try
{
bool Mousepointontaskbar = Screen.GetWorkingArea(this).Contains(Cursor.Position);
if (this.WindowState == FormWindowState.Minimized && Mousepointontaskbar)
{
notifyIcon1.Icon = SystemIcons.Application;
notifyIcon1.BalloonTipText = "Tracker Has bin Minimized in System Tray";
notifyIcon1.ShowBalloonTip(1000);
this.ShowInTaskbar = false;
notifyIcon1.Visible = true;
//this.SizeChanged += new EventHandler(User_Projects_SizeChanged);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void User_Projects_Resize(object sender, EventArgs e)
{
this.Resize += new EventHandler(User_Projects_SizeChanged);
}
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
this.WindowState = FormWindowState.Normal;
if (this.WindowState == FormWindowState.Normal)
{
this.ShowInTaskbar = true;
this.Visible = true;
notifyIcon1.Visible = false;
}
}

How to display current window to notification bar NOT HIDE in WPF

I've created one WPF application. It has one window and it's hide on close button. But i want to show it in notification-bar. and when user click on that then windows should display.
Here is my code:
public MainWindow()
{
InitializeComponent();
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
aTimer.Interval = 3000;
aTimer.Enabled = true;
}
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
lblProcess.Content = "Test Window"
}
// minimize to system tray when applicaiton is closed
protected override void OnClosing(CancelEventArgs e)
{
// setting cancel to true will cancel the close request
// so the application is not closed
e.Cancel = true;
this.Hide();
//this.Show();
base.OnClosing(e);
}
I've already read this: create-popup-toaster-notifications-in-windows-with-net
And minimizing-application-to-system-tray-using-wpf-not-using-notifyicon
minimizing-closing-application-to-system-tray-using-wpf
But didn't get you how can i do this?
Any help appreciated!
I have done using this code:
System.Windows.Forms.NotifyIcon ni = new System.Windows.Forms.NotifyIcon();
ni.Icon = new System.Drawing.Icon("D:\\images.ico");
ni.Visible = true;
ni.DoubleClick +=delegate(object sender, EventArgs args)
{
this.Show();
this.WindowState = WindowState.Normal;
};

Two different ways of handling the form Close event

I have a Save button on my WinForm which saves the form information and then closes the form using the this.Close() statement.
There is however another way of closing the form and that is the X button.
I am using the FormClosing event to ask a question before the form is closed.
private void EmailNewsletter_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult dr = MsgBox.Show("Are you sure you want to dimiss this newsletter?", "Dismiss Newsletter", MsgBox.Buttons.YesNo, MsgBox.Icon.Question);
if (dr == System.Windows.Forms.DialogResult.Yes)
{
this.Newsletter = null;
}
else
{
e.Cancel = true;
}
}
But the way of handling the close form depends on how the form is closed. If the user clicks on the Save button, the form should be closed without a question. It is only when user clicks on the X button that the question should be asked.
How can I let my form know what the Close command comes from?
A simple boolean flag should do the trick:
private bool saveClicked = false;
private void btnSave_click(object sender, EventArgs e)
{
saveClicked = true;
}
private void EmailNewsletter_FormClosing(object sender, FormClosingEventArgs e)
{
if(saveClicked)
return;
DialogResult dr = MsgBox.Show("Are you sure you want to dimiss this newsletter?", "Dismiss Newsletter", MsgBox.Buttons.YesNo, MsgBox.Icon.Question);
if (dr == System.Windows.Forms.DialogResult.Yes)
{
this.Newsletter = null;
}
else
{
e.Cancel = true;
}
}

How to use Multiple forms in one Csharp panel in one Windows Form panel?

I am building a kids learning application, where clicking on a button on panel, I want to show different forms in the same place of the panel. Can you please help with any walk-through or tutorial links?
This question should have been posted on Stackoverflow website rather than here.
But you can use this approach to handle the case.
subForm = new SubFormYouWantToLoad();
subForm.TopLevel = false;
subForm.FormBorderStyle = FormBorderStyle.None;
ContainerPanel.Controls.Add(subForm , 0, 1);
subForm .Visible = true;
You can add this code when you click on the specific button.
Here each subform is added to the Panel as a Control. You should remove the subform from the panel's control list before adding another subform. For this ,it is better to remove,close and dispose the first one.
ContainerPanel.Controls.Remove(activeform);
activeform.Close();
activeform.Dispose();
Instead of Forms use user controls and load them in to panels
Sample if you want to show usercontrol1
panel1.Controls.Clear();
panel1.Visible = true;
UserControl1 usr1 = new UserControl1();
usr1.Show();
panel1.Controls.Add(usr1);
If usercontrol2
panel1.Controls.Clear();
panel1.Visible = true;
UserControl1 usr2 = new UserControl2();
usr2.Show();
panel1.Controls.Add(usr2);
You could create a number of forms as user controls or a control that inheriets from a panel. Then have a parent form with a panel to hold the user controls. You can then change the active user control in the container when the panel needs to be changed.
There is a tutorial on msdn for creating user controls.
http://msdn.microsoft.com/en-us/library/a6h7e207(v=vs.71).aspx
I used this code to close the form on the panel but not worked..
private void button12_Click(object sender, EventArgs e)
{
dontShowPANEL();
//ActiveForm.Close();
MainImaginCp kj = new MainImaginCp();
//kj.Visible = false;
kj.panel2.Controls.Clear();
panel1.Visible = true;
EngABCLearning usr1 = new EngABCLearning();
usr1.Show();
kj.panel2.Controls.Add(usr1);
//kj.Focus();
}
And I used the following code to show the form in the panel.
private void toolStripMenuItem1_LR_ENG_Click(object sender, EventArgs e)
{
//kids.Form2 hj = new kids.Form2();
//hj.Show();
EngABCLearning gh = new EngABCLearning();
//gh.Show();
gh.TopLevel = false;
gh.FormBorderStyle = FormBorderStyle.None;
//Panel2.Controls.Add(subForm, 0, 1);
panel2.Controls.Add(gh);
gh.Visible = true;
}
This is closing my main form and exiting the application.
try this out i have loaded two forms inside a single panel
private void Form1_Load(object sender, EventArgs e)
{
Form2 f1 = new Form2();
f1.TopLevel = false;
f1.AutoScroll = true;
panel1.Controls.Add(f1);
f1.Dock = DockStyle.Left;
f1.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
f1.Show();
//form2
Form3 f2 = new Form3();
f2.TopLevel = false;
f2.AutoScroll = true;
panel1.Controls.Add(f2);
f2.Dock = DockStyle.Left;
f2.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
f2.Show();
}
try this i used this method to load multiple forms at one panel
private Form activeForm = null;
public void FormLoad(Form childForm)
{
if (activeForm != null)
{
activeForm.Close();
}
activeForm = childForm;
childForm.TopLevel = false;
childForm.FormBorderStyle = FormBorderStyle.None;
panelName.Controls.Add(childForm);
childForm.Visible = true;
}
private void YourBtn1_Click(object sender, EventArgs e)
{
FormLoad(new youWantToLoadForm1Name());
}
private void YourBtn2_Click(object sender, EventArgs e)
{
FormLoad(new youWantToLoadForm2Name());
}

Categories