C
C#•12mo ago
Soggy

Windows Form, how to await for user input in a constructor.

I have the following code for my MainForm
public partial class MainForm : Form
{
public Client ActiveClient { get; }

public MainForm()
{
LoginForm login = new LoginForm();
this.Hide();
login.Activate();

//ActiveClient = await login.GetClient(); //GetClient() is not a real function in my code

InitializeComponent();
}
}
public partial class MainForm : Form
{
public Client ActiveClient { get; }

public MainForm()
{
LoginForm login = new LoginForm();
this.Hide();
login.Activate();

//ActiveClient = await login.GetClient(); //GetClient() is not a real function in my code

InitializeComponent();
}
}
This is what my LoginCode looks like
public async Task LogIn(string username, string password)
{
if (string.IsNullOrEmpty(username)) { MessageBox.Show("Please enter a username"); return; }
if (string.IsNullOrEmpty(password)) { MessageBox.Show("Please enter a password"); return; }

Task<User.PassCode> passTask = User.PassCode.EncodeAsync(password);
User.PassCode pass = await passTask;
this.UseWaitCursor = true;

Client? client = await Client.Authenticate(User.ID.Parse(username), pass);

if (client == null)
{
MessageBox.Show("Incorrect Username or Password");
return;
}

this.Hide();
this.UseWaitCursor= false;
//new MainForm(client).MainForm_Load();
}
public async Task LogIn(string username, string password)
{
if (string.IsNullOrEmpty(username)) { MessageBox.Show("Please enter a username"); return; }
if (string.IsNullOrEmpty(password)) { MessageBox.Show("Please enter a password"); return; }

Task<User.PassCode> passTask = User.PassCode.EncodeAsync(password);
User.PassCode pass = await passTask;
this.UseWaitCursor = true;

Client? client = await Client.Authenticate(User.ID.Parse(username), pass);

if (client == null)
{
MessageBox.Show("Incorrect Username or Password");
return;
}

this.Hide();
this.UseWaitCursor= false;
//new MainForm(client).MainForm_Load();
}
This gets called when the login button is pressed. Is there a nice way to get the Client from my LoginForm? Any ideas? Am I missing something really simple or is this not possible the way I set it up?
21 Replies
x0rld 👻 🎃
you're not supposed to do user input in the ctor it's a bad practice
Soggy
SoggyOP•12mo ago
hmm i should of guessed as much I originally had it such that LoginForm would make a new MainForm with the Client But I don't like how the Forms work out, it can't exit my LoginForm, I can only hide it.
x0rld 👻 🎃
there is the Close method in Form what happend if you use that ?
Soggy
SoggyOP•12mo ago
Lemmie try that
Omnissiah
Omnissiah•12mo ago
winforms is pretty raw, either you make client a public field and read it from MainForm, or you make your own service to manage a context
Soggy
SoggyOP•12mo ago
@xtreit how do I keep my MainForm from closing when I close LoginForm?
x0rld 👻 🎃
🤔 how did you do it ?
Soggy
SoggyOP•12mo ago
My code now looks like this
public async void LogIn(string username, string password)
{
//code

new MainForm(client).MainForm_Load();
this.Close();
}
public async void LogIn(string username, string password)
{
//code

new MainForm(client).MainForm_Load();
this.Close();
}
public MainForm(Client client)
{
InitializeComponent();
ActiveClient = client;
}
public MainForm(Client client)
{
InitializeComponent();
ActiveClient = client;
}
x0rld 👻 🎃
I don't know 🤔
Soggy
SoggyOP•12mo ago
This is what's weird, isn't MainForm only a local thingy in LoginForm, so what happens to it when I close it
jcotton42
jcotton42•12mo ago
Use Load for stuff that like instead of the ctor Or whatever the event is called
Omnissiah
Omnissiah•12mo ago
what's this new MainForm(client).MainForm_Load(); and should be the flow? main creates login as modal? login starts and then it creates main?
jcotton42
jcotton42•12mo ago
Erm, you don't call Load The UI framework does
Soggy
SoggyOP•12mo ago
It was just a place holder same with load idk the right funny functions So the problem I have now is idk how to transfer control to MainForm I start with LoginForm, which has this code
public async void LogIn(string username, string password)
{
//code

new MainForm(client).Show();
this.Close();
}
public async void LogIn(string username, string password)
{
//code

new MainForm(client).Show();
this.Close();
}
I want to close LoginForm and open MainForm without any shenanigans
jcotton42
jcotton42•12mo ago
Don't use async void unless it's an event handler Always use async Task if you can
Soggy
SoggyOP•12mo ago
it is an event handler, or at least i treat it like it is
jcotton42
jcotton42•12mo ago
I mean it's directly subscribed to an event That's pretty much the only time you use async void
Soggy
SoggyOP•12mo ago
yeh, anyways
Omnissiah
Omnissiah•12mo ago
whatever form is visible is usable there shouldn't be problems
Soggy
SoggyOP•12mo ago
it won't close properly though >:(
Omnissiah
Omnissiah•12mo ago
why is not 'proper' yet
Want results from more Discord servers?
Add your server