C
C#•9mo ago
MementoMori

cannot close window

...
27 Replies
MementoMori
MementoMoriOP•9mo ago
MementoMori
MementoMoriOP•9mo ago
i reached limit
Pobiega
Pobiega•9mo ago
cs not C# after the backticks and break it up if its too long, or use $paste
MODiX
MODiX•9mo ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
MementoMori
MementoMoriOP•9mo ago
ok wait
Pobiega
Pobiega•9mo ago
noone wants to download a .txt file just to read your code
MementoMori
MementoMoriOP•9mo ago
BlazeBin - hrwavucnckji
A tool for sharing your source code with the world!
MementoMori
MementoMoriOP•9mo ago
sorry here it is
Pobiega
Pobiega•9mo ago
Okay, there is a whole pile of wrong here First: don't open new forms in another forms constructor. Especially not your main form. two:
Login loginWindow = new Login();
loginWindow.Show();
loginWindow.Close();
Login loginWindow = new Login();
loginWindow.Show();
loginWindow.Close();
This will open a new form to then immediately close it again
MementoMori
MementoMoriOP•9mo ago
yeah i tried doing .Close() but it dont work for the loginWindow i can only close it from Main um why
Pobiega
Pobiega•9mo ago
Because the main form isnt fully constructed at that point
MementoMori
MementoMoriOP•9mo ago
but it will construct right after Login is closed right
Pobiega
Pobiega•9mo ago
yes, in theory - but with winforms (and probably also WPF) the main form is treated differently it sounds like you should just hide the main form, create and show a login form once the main form has loaded (with the load event), then when the login form is done you can show the main form again HOWEVER this code:
Login loginWindow = new Login();
loginWindow.Show();
loginWindow.Close();
Login loginWindow = new Login();
loginWindow.Show();
loginWindow.Close();
will never work as you want after show, it will not "stop" to wait for you to finish logging in. it will instead move to .Close and close If you want the main forms code to "wait" for the login, you need to use ShowDialog() and then you also dont need to close it yourself
MementoMori
MementoMoriOP•9mo ago
yes i used this ShowDialog() but the thing is the login window wont close unless i press the close button for it i wanted for it to close when the user presses the button for which AttemptLogin runs
Pobiega
Pobiega•9mo ago
And that can be done. if you give your button a DialogResult value
MementoMori
MementoMoriOP•9mo ago
um how do i do that
Pobiega
Pobiega•9mo ago
step 1: add a Loaded event handler to your main form
Pobiega
Pobiega•9mo ago
No description
Pobiega
Pobiega•9mo ago
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var form = new LoginWindow();
var result = form.ShowDialog();

// do something with result here
if (result == true)
{
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var form = new LoginWindow();
var result = form.ShowDialog();

// do something with result here
if (result == true)
{
}
}
Pobiega
Pobiega•9mo ago
in LoginWindows.xaml
No description
Pobiega
Pobiega•9mo ago
private void Button_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
}
MementoMori
MementoMoriOP•9mo ago
what is thiss
MementoMori
MementoMoriOP•9mo ago
before i could even test this suddenly happened 😭
No description
Pobiega
Pobiega•9mo ago
your program is already running close it before starting a new one
MementoMori
MementoMoriOP•9mo ago
yoo its fixed noww thank youu
leowest
leowest•9mo ago
@MementoMori tip, wpf does not work like winform u should learn MVVM and how to navigate instead of opening new window https://www.youtube.com/watch?v=N26C_Cq-gAY&list=PLZnwzjShc0PS1DDfPTW3oLSTsn878a6_P&index=22 might be a bit overwhelming at first for u, since ur new to it.
MementoMori
MementoMoriOP•9mo ago
i will check it out thank you
Want results from more Discord servers?
Add your server