C
C#•8mo ago
MementoMori

cannot close window

...
27 Replies
MementoMori
MementoMori•8mo ago
i reached limit
Pobiega
Pobiega•8mo ago
cs not C# after the backticks and break it up if its too long, or use $paste
MODiX
MODiX•8mo 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
MementoMori•8mo ago
ok wait
Pobiega
Pobiega•8mo ago
noone wants to download a .txt file just to read your code
MementoMori
MementoMori•8mo ago
BlazeBin - hrwavucnckji
A tool for sharing your source code with the world!
MementoMori
MementoMori•8mo ago
sorry here it is
Pobiega
Pobiega•8mo 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
MementoMori•8mo ago
yeah i tried doing .Close() but it dont work for the loginWindow i can only close it from Main um why
Pobiega
Pobiega•8mo ago
Because the main form isnt fully constructed at that point
MementoMori
MementoMori•8mo ago
but it will construct right after Login is closed right
Pobiega
Pobiega•8mo 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
MementoMori•8mo 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•8mo ago
And that can be done. if you give your button a DialogResult value
MementoMori
MementoMori•8mo ago
um how do i do that
Pobiega
Pobiega•8mo ago
step 1: add a Loaded event handler to your main form
Pobiega
Pobiega•8mo ago
No description
Pobiega
Pobiega•8mo 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•8mo ago
in LoginWindows.xaml
No description
Pobiega
Pobiega•8mo ago
private void Button_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
}
MementoMori
MementoMori•8mo ago
what is thiss
MementoMori
MementoMori•8mo ago
before i could even test this suddenly happened 😭
No description
Pobiega
Pobiega•8mo ago
your program is already running close it before starting a new one
MementoMori
MementoMori•8mo ago
yoo its fixed noww thank youu
leowest
leowest•8mo 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
MementoMori•8mo ago
i will check it out thank you
Want results from more Discord servers?
Add your server