C
C#2y ago
ApathyErr

❔ How do I close the authorization window after the user has logged in?

I implemented it this way, but in this case my memory is heavily loaded, so a new window is created every time
IKD IKD = new IKD();
IKD.Show();
Hide();
IKD IKD = new IKD();
IKD.Show();
Hide();
28 Replies
Pobiega
Pobiega2y ago
perhaps show it as a dialog instead? IDK.ShowDialog()
ApathyErr
ApathyErrOP2y ago
It works. And how to hide the authorization window so that it is not visible when the IKD is open?
Pobiega
Pobiega2y ago
not enough information. You've shown literally 3 lines of code. I dont know what your "authorization window" is I thought that was the IKD
ApathyErr
ApathyErrOP2y ago
LoginWindow:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "123" && textBox2.Text == "")
{
Form1 newForm = new Form1();
IKD newForm1 = new IKD();
newForm1.ShowDialog();
Hide();
}
}

private void pictureBox1_Click(object sender, EventArgs e)
{

}

private void Form1_Load(object sender, EventArgs e)
{

}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{

}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "123" && textBox2.Text == "")
{
Form1 newForm = new Form1();
IKD newForm1 = new IKD();
newForm1.ShowDialog();
Hide();
}
}

private void pictureBox1_Click(object sender, EventArgs e)
{

}

private void Form1_Load(object sender, EventArgs e)
{

}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{

}
IKD:
public IKD()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

Form1 Form1 = new Form1();
this.Close();
}
public IKD()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

Form1 Form1 = new Form1();
this.Close();
}
Pobiega
Pobiega2y ago
ok so IKD owns Form1 ah I see, you actually have a weird loop
ApathyErr
ApathyErrOP2y ago
yep
Pobiega
Pobiega2y ago
IKD creates and owns a form 1, but a Form1 also creates/owns an IKD.. and another form1? do you understand what happens each time you do new Form1();?
ApathyErr
ApathyErrOP2y ago
LoginWindow=Form1 no
Pobiega
Pobiega2y ago
it creates a NEW form instance unrelated to the previous ones
ApathyErr
ApathyErrOP2y ago
o no how to avoid this? I just don't understand how to do it right without creating unrelated windows.
Pobiega
Pobiega2y ago
you need to reuse and pass your object references around first, which form is your main form, the one that loads when your program starts?
ApathyErr
ApathyErrOP2y ago
Form1, which is the authorization window .
Pobiega
Pobiega2y ago
Okay. So, there are a few ways to do this. Are there ever a reason to show the login form again after the user goes through it the first time?
ApathyErr
ApathyErrOP2y ago
No
Pobiega
Pobiega2y ago
Im in the middle of some stuff, I can help in 20ish min
ApathyErr
ApathyErrOP2y ago
no prob
Pobiega
Pobiega2y ago
Ok so the idea is that you in Form1 create a single IKD instance, probably as a field
public partial class Form1 : Form
{
private IKD _ikd = new();

public Form1()
{
InitializeComponent();
}
...
public partial class Form1 : Form
{
private IKD _ikd = new();

public Form1()
{
InitializeComponent();
}
...
in your button click event, you do
this.Hide();
_ikd.Show();
this.Hide();
_ikd.Show();
nothing else. and in IKD, remove this:
Form1 Form1 = new Form1();
this.Close();
Form1 Form1 = new Form1();
this.Close();
you said there was no point in "going back" from IKD, so lets remove that
ApathyErr
ApathyErrOP2y ago
I probably will, but I would still like to try to do it in order to use it in the future
Pobiega
Pobiega2y ago
? I don't follow.
ApathyErr
ApathyErrOP2y ago
I'm still trying to implement it as you showed above, but I can't. Before that there was an error "Please use language version 9.0 or greater", but I have already fixed it
Pobiega
Pobiega2y ago
thats the IKD code it cant use the _ikd variable, thats only in Form1
Please use language version 9.0 or greater
oh no, are you using .NET Framework 4.x instead of .NET 7?
ApathyErr
ApathyErrOP2y ago
yes, this is the latest version that is available when creating a WinForms project
Pobiega
Pobiega2y ago
it is not you absolutely can make winforms projects on .NET 7 $newproject
MODiX
MODiX2y ago
When creating a new project, prefer using .NET over .NET Framework, unless you have a very specific reason to be using .NET Framework. .NET Framework is now legacy code and only get security fix updates, it no longer gets new features and is not recommended. https://cdn.discordapp.com/attachments/569261465463160900/899381236617855016/unknown.png
ApathyErr
ApathyErrOP2y ago
should I have chosen the second when creating the project?
Pobiega
Pobiega2y ago
yes
ApathyErr
ApathyErrOP2y ago
Okay, thx ❤️
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server