LoginForm hides or closes automatically when the ShowInTaskBar property is false


LoginForm hides or closes automatically when the ShowInTaskBar property is false



I am developing an application in C# Winforms with system login where I have two forms, one being the main MainForm and the other which is for the login LoginForm.


MainForm


LoginForm



This application has a Modern Flat UI Design interface, so it wants the LoginForm form is displayed in front of the MainForm form, to not display its icon in the taskbar and not allow the user to interact with MainForm to display it's been use of the method ShowDialog();.


Modern Flat UI Design


LoginForm form


MainForm


MainForm


ShowDialog();



LoginForm compacted code:


LoginForm


public partial class LoginForm : Form
{
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);

this.ShowInTaskbar = this.ShowIcon = false;
}
}



MainForm compacted code:


MainForm


public partial class MainForm : Form
{
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);

new LoginForm().ShowDialog();
//new LoginForm().ShowDialog(this);
}

protected override void OnShown(EventArgs e)
{
base.OnShown(e);

//new LoginForm().ShowDialog();
//new LoginForm().ShowDialog(this);
}
}



My problem is that nothing else to start MainForm, shows the LoginForm for about 2 seconds and then hides it, then, I minimize the MainForm to verify that the LoginForm has not moved behind MainForm, but it turns out that LoginForm is not there.


MainForm


LoginForm


MainForm


LoginForm


MainForm


LoginForm



If I remove the property ShowInTaskbar = false; then everything works normally, but the LoginForm is displayed in the taskbar which is precisely what I do not want.


ShowInTaskbar = false;


LoginForm



So, how can I show my LoginForm in front of MainForm without LoginForm appearing in the taskbar?


LoginForm


MainForm


LoginForm



Note:
Yes, while the LoginForm is not closed, the user not may interact with MainForm and the only way to close the LoginForm is logging properly or by clicking on the button close the LoginForm calling Application.Exit();.


LoginForm


MainForm


LoginForm


LoginForm


Application.Exit();



Also, I've tried to hide the MainForm to start with this.Hide(); but equally LoginForm disappears.


MainForm


this.Hide();


LoginForm





new LoginForm().ShowDialog(Me); ? then it wont go behind the mainform
– Ctznkane525
Jun 29 at 17:29






@Ctznkane525 is C# not VB and yes, I already tried with new LoginForm().ShowDialog(this);, but, the result is the same
– Héctor Manuel Martínez Durán
Jun 29 at 17:59


new LoginForm().ShowDialog(this);





Can''t reproduce. When I override OnShown, the LoginForm shows on top of MainForm without issue, has no task bar button or icon, and does not disappear. Could be an issue with the way you are launching MainForm; please post that portion of your code.
– John Wu
Jun 29 at 22:10


OnShown


LoginForm


MainForm





It seems you should set ShowInTaskbar of the modal form before creating its handle.
– Reza Aghaei
Jun 30 at 5:14


ShowInTaskbar




2 Answers
2



Set ShowInTaskBar and ShowIcon in constructor of your dialog:


ShowInTaskBar


ShowIcon


public partial class LoginForm : Form
{
public LoginForm()
{
InitializeComponent();
this.ShowInTaskbar = false;
this.ShowIcon = false;
}
}



And then show it in Shown method of the main form:


Shown


public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
new LoginForm().ShowDialog();
}
}





It seems you should set ShowInTaskbar of the modal form before creating its handle.
– Reza Aghaei
Jun 30 at 5:14



ShowInTaskbar





You can refactor the code based on your requirement. I simply call it like new Form(){ShowInTaskbar = false, ShowIcon=false}.ShowDialog();
– Reza Aghaei
Jun 30 at 5:43



new Form(){ShowInTaskbar = false, ShowIcon=false}.ShowDialog();





Thanks Reza Agaehi ;)
– Héctor Manuel Martínez Durán
Jun 30 at 6:15





You're welcome :)
– Reza Aghaei
Jun 30 at 6:15



You could try setting the parent of the login form to the main form.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV