Returning Value From Window To Class WPF












1















Within a method in my class I call Login.Show(), which is a Login Window. I would like the window to pass the email back to the class when the Login button is clicked, without creating a new instance of the class.



Is there any way to do this?



Currently I have



Login loginWindow;
public void AppStartup {
loginWindow = new Login();
loginWindow.Show();
//in this instance I'd like the email to be returned here


Within the Login.xaml.cs



public void Login_Click(object sender, RoutedEventArgs e)
{
string email;
try {
email = InputEmail.Text;
//ideally I would like to return email to AppStartup without
//using new AppStartup(); , rather back in the same instance
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}









share|improve this question


















  • 2





    i would read up on mvvm & binding for wpf

    – JohnB
    Nov 20 '18 at 2:09











  • Just add a public property to the LoginWindow class and set it in the Login_Click handler

    – Jon
    Nov 20 '18 at 2:51













  • you know, that it is tricky to use Console.WriteLine(ex.Message); method from WPF (or even WinForms) application?

    – vasily.sib
    Nov 20 '18 at 3:14











  • You may want to use ShowDialog() instead of Show(). Then you can access the member variable @Jon suggest after the call.

    – Klaus Gütter
    Nov 20 '18 at 5:13
















1















Within a method in my class I call Login.Show(), which is a Login Window. I would like the window to pass the email back to the class when the Login button is clicked, without creating a new instance of the class.



Is there any way to do this?



Currently I have



Login loginWindow;
public void AppStartup {
loginWindow = new Login();
loginWindow.Show();
//in this instance I'd like the email to be returned here


Within the Login.xaml.cs



public void Login_Click(object sender, RoutedEventArgs e)
{
string email;
try {
email = InputEmail.Text;
//ideally I would like to return email to AppStartup without
//using new AppStartup(); , rather back in the same instance
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}









share|improve this question


















  • 2





    i would read up on mvvm & binding for wpf

    – JohnB
    Nov 20 '18 at 2:09











  • Just add a public property to the LoginWindow class and set it in the Login_Click handler

    – Jon
    Nov 20 '18 at 2:51













  • you know, that it is tricky to use Console.WriteLine(ex.Message); method from WPF (or even WinForms) application?

    – vasily.sib
    Nov 20 '18 at 3:14











  • You may want to use ShowDialog() instead of Show(). Then you can access the member variable @Jon suggest after the call.

    – Klaus Gütter
    Nov 20 '18 at 5:13














1












1








1








Within a method in my class I call Login.Show(), which is a Login Window. I would like the window to pass the email back to the class when the Login button is clicked, without creating a new instance of the class.



Is there any way to do this?



Currently I have



Login loginWindow;
public void AppStartup {
loginWindow = new Login();
loginWindow.Show();
//in this instance I'd like the email to be returned here


Within the Login.xaml.cs



public void Login_Click(object sender, RoutedEventArgs e)
{
string email;
try {
email = InputEmail.Text;
//ideally I would like to return email to AppStartup without
//using new AppStartup(); , rather back in the same instance
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}









share|improve this question














Within a method in my class I call Login.Show(), which is a Login Window. I would like the window to pass the email back to the class when the Login button is clicked, without creating a new instance of the class.



Is there any way to do this?



Currently I have



Login loginWindow;
public void AppStartup {
loginWindow = new Login();
loginWindow.Show();
//in this instance I'd like the email to be returned here


Within the Login.xaml.cs



public void Login_Click(object sender, RoutedEventArgs e)
{
string email;
try {
email = InputEmail.Text;
//ideally I would like to return email to AppStartup without
//using new AppStartup(); , rather back in the same instance
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}






c# wpf return-value






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 2:04









ExplorexExplorex

597




597








  • 2





    i would read up on mvvm & binding for wpf

    – JohnB
    Nov 20 '18 at 2:09











  • Just add a public property to the LoginWindow class and set it in the Login_Click handler

    – Jon
    Nov 20 '18 at 2:51













  • you know, that it is tricky to use Console.WriteLine(ex.Message); method from WPF (or even WinForms) application?

    – vasily.sib
    Nov 20 '18 at 3:14











  • You may want to use ShowDialog() instead of Show(). Then you can access the member variable @Jon suggest after the call.

    – Klaus Gütter
    Nov 20 '18 at 5:13














  • 2





    i would read up on mvvm & binding for wpf

    – JohnB
    Nov 20 '18 at 2:09











  • Just add a public property to the LoginWindow class and set it in the Login_Click handler

    – Jon
    Nov 20 '18 at 2:51













  • you know, that it is tricky to use Console.WriteLine(ex.Message); method from WPF (or even WinForms) application?

    – vasily.sib
    Nov 20 '18 at 3:14











  • You may want to use ShowDialog() instead of Show(). Then you can access the member variable @Jon suggest after the call.

    – Klaus Gütter
    Nov 20 '18 at 5:13








2




2





i would read up on mvvm & binding for wpf

– JohnB
Nov 20 '18 at 2:09





i would read up on mvvm & binding for wpf

– JohnB
Nov 20 '18 at 2:09













Just add a public property to the LoginWindow class and set it in the Login_Click handler

– Jon
Nov 20 '18 at 2:51







Just add a public property to the LoginWindow class and set it in the Login_Click handler

– Jon
Nov 20 '18 at 2:51















you know, that it is tricky to use Console.WriteLine(ex.Message); method from WPF (or even WinForms) application?

– vasily.sib
Nov 20 '18 at 3:14





you know, that it is tricky to use Console.WriteLine(ex.Message); method from WPF (or even WinForms) application?

– vasily.sib
Nov 20 '18 at 3:14













You may want to use ShowDialog() instead of Show(). Then you can access the member variable @Jon suggest after the call.

– Klaus Gütter
Nov 20 '18 at 5:13





You may want to use ShowDialog() instead of Show(). Then you can access the member variable @Jon suggest after the call.

– Klaus Gütter
Nov 20 '18 at 5:13












1 Answer
1






active

oldest

votes


















2














You could call ShowDialog() instead of Show() to display the window and then access the Text property of the InputEmail control directly:



loginWindow = new Login();
loginWindow.ShowDialog();
string email = loginWindow.InputEmail.Text;


Unlike Show(), ShowDialog() won't return until the window has been closed.



Or you could add a property to the Login window or its DataContext, and set this one when the button is clicked.



public string Email { get; set; }

public void Login_Click(object sender, RoutedEventArgs e)
{
Email = InputEmail.Text;
}




string email = loginWindow.Email;





share|improve this answer
























  • I tried that but it doesnt seem to work, it doesnt wait for Login_Click so 'email' value is always nothing. Do I need to use a while loop and wait for Login click to be triggered?

    – Explorex
    Nov 20 '18 at 11:06











  • You need to call ShowDialog() instead of Show(). Did you really try this?

    – mm8
    Nov 20 '18 at 12:05











  • Yes, but it runs through showdialog and email =window.email instantly and so it never gives it a chance to update, as in wait for login click to occur. I almost need it to be like await login click ? Does this make sense?

    – Explorex
    Nov 20 '18 at 12:12













  • No, ShowDialog doesn't return until loginWindow has been closed. And you can't click on a button after a window has been closed...

    – mm8
    Nov 20 '18 at 12:17






  • 1





    It needs to close before the method that calls ShowDialog continues to the next line...if you don't want this you could raise an event from the login window and handle this in your app class.

    – mm8
    Nov 20 '18 at 12:35











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385195%2freturning-value-from-window-to-class-wpf%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You could call ShowDialog() instead of Show() to display the window and then access the Text property of the InputEmail control directly:



loginWindow = new Login();
loginWindow.ShowDialog();
string email = loginWindow.InputEmail.Text;


Unlike Show(), ShowDialog() won't return until the window has been closed.



Or you could add a property to the Login window or its DataContext, and set this one when the button is clicked.



public string Email { get; set; }

public void Login_Click(object sender, RoutedEventArgs e)
{
Email = InputEmail.Text;
}




string email = loginWindow.Email;





share|improve this answer
























  • I tried that but it doesnt seem to work, it doesnt wait for Login_Click so 'email' value is always nothing. Do I need to use a while loop and wait for Login click to be triggered?

    – Explorex
    Nov 20 '18 at 11:06











  • You need to call ShowDialog() instead of Show(). Did you really try this?

    – mm8
    Nov 20 '18 at 12:05











  • Yes, but it runs through showdialog and email =window.email instantly and so it never gives it a chance to update, as in wait for login click to occur. I almost need it to be like await login click ? Does this make sense?

    – Explorex
    Nov 20 '18 at 12:12













  • No, ShowDialog doesn't return until loginWindow has been closed. And you can't click on a button after a window has been closed...

    – mm8
    Nov 20 '18 at 12:17






  • 1





    It needs to close before the method that calls ShowDialog continues to the next line...if you don't want this you could raise an event from the login window and handle this in your app class.

    – mm8
    Nov 20 '18 at 12:35
















2














You could call ShowDialog() instead of Show() to display the window and then access the Text property of the InputEmail control directly:



loginWindow = new Login();
loginWindow.ShowDialog();
string email = loginWindow.InputEmail.Text;


Unlike Show(), ShowDialog() won't return until the window has been closed.



Or you could add a property to the Login window or its DataContext, and set this one when the button is clicked.



public string Email { get; set; }

public void Login_Click(object sender, RoutedEventArgs e)
{
Email = InputEmail.Text;
}




string email = loginWindow.Email;





share|improve this answer
























  • I tried that but it doesnt seem to work, it doesnt wait for Login_Click so 'email' value is always nothing. Do I need to use a while loop and wait for Login click to be triggered?

    – Explorex
    Nov 20 '18 at 11:06











  • You need to call ShowDialog() instead of Show(). Did you really try this?

    – mm8
    Nov 20 '18 at 12:05











  • Yes, but it runs through showdialog and email =window.email instantly and so it never gives it a chance to update, as in wait for login click to occur. I almost need it to be like await login click ? Does this make sense?

    – Explorex
    Nov 20 '18 at 12:12













  • No, ShowDialog doesn't return until loginWindow has been closed. And you can't click on a button after a window has been closed...

    – mm8
    Nov 20 '18 at 12:17






  • 1





    It needs to close before the method that calls ShowDialog continues to the next line...if you don't want this you could raise an event from the login window and handle this in your app class.

    – mm8
    Nov 20 '18 at 12:35














2












2








2







You could call ShowDialog() instead of Show() to display the window and then access the Text property of the InputEmail control directly:



loginWindow = new Login();
loginWindow.ShowDialog();
string email = loginWindow.InputEmail.Text;


Unlike Show(), ShowDialog() won't return until the window has been closed.



Or you could add a property to the Login window or its DataContext, and set this one when the button is clicked.



public string Email { get; set; }

public void Login_Click(object sender, RoutedEventArgs e)
{
Email = InputEmail.Text;
}




string email = loginWindow.Email;





share|improve this answer













You could call ShowDialog() instead of Show() to display the window and then access the Text property of the InputEmail control directly:



loginWindow = new Login();
loginWindow.ShowDialog();
string email = loginWindow.InputEmail.Text;


Unlike Show(), ShowDialog() won't return until the window has been closed.



Or you could add a property to the Login window or its DataContext, and set this one when the button is clicked.



public string Email { get; set; }

public void Login_Click(object sender, RoutedEventArgs e)
{
Email = InputEmail.Text;
}




string email = loginWindow.Email;






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 10:51









mm8mm8

83.2k81931




83.2k81931













  • I tried that but it doesnt seem to work, it doesnt wait for Login_Click so 'email' value is always nothing. Do I need to use a while loop and wait for Login click to be triggered?

    – Explorex
    Nov 20 '18 at 11:06











  • You need to call ShowDialog() instead of Show(). Did you really try this?

    – mm8
    Nov 20 '18 at 12:05











  • Yes, but it runs through showdialog and email =window.email instantly and so it never gives it a chance to update, as in wait for login click to occur. I almost need it to be like await login click ? Does this make sense?

    – Explorex
    Nov 20 '18 at 12:12













  • No, ShowDialog doesn't return until loginWindow has been closed. And you can't click on a button after a window has been closed...

    – mm8
    Nov 20 '18 at 12:17






  • 1





    It needs to close before the method that calls ShowDialog continues to the next line...if you don't want this you could raise an event from the login window and handle this in your app class.

    – mm8
    Nov 20 '18 at 12:35



















  • I tried that but it doesnt seem to work, it doesnt wait for Login_Click so 'email' value is always nothing. Do I need to use a while loop and wait for Login click to be triggered?

    – Explorex
    Nov 20 '18 at 11:06











  • You need to call ShowDialog() instead of Show(). Did you really try this?

    – mm8
    Nov 20 '18 at 12:05











  • Yes, but it runs through showdialog and email =window.email instantly and so it never gives it a chance to update, as in wait for login click to occur. I almost need it to be like await login click ? Does this make sense?

    – Explorex
    Nov 20 '18 at 12:12













  • No, ShowDialog doesn't return until loginWindow has been closed. And you can't click on a button after a window has been closed...

    – mm8
    Nov 20 '18 at 12:17






  • 1





    It needs to close before the method that calls ShowDialog continues to the next line...if you don't want this you could raise an event from the login window and handle this in your app class.

    – mm8
    Nov 20 '18 at 12:35

















I tried that but it doesnt seem to work, it doesnt wait for Login_Click so 'email' value is always nothing. Do I need to use a while loop and wait for Login click to be triggered?

– Explorex
Nov 20 '18 at 11:06





I tried that but it doesnt seem to work, it doesnt wait for Login_Click so 'email' value is always nothing. Do I need to use a while loop and wait for Login click to be triggered?

– Explorex
Nov 20 '18 at 11:06













You need to call ShowDialog() instead of Show(). Did you really try this?

– mm8
Nov 20 '18 at 12:05





You need to call ShowDialog() instead of Show(). Did you really try this?

– mm8
Nov 20 '18 at 12:05













Yes, but it runs through showdialog and email =window.email instantly and so it never gives it a chance to update, as in wait for login click to occur. I almost need it to be like await login click ? Does this make sense?

– Explorex
Nov 20 '18 at 12:12







Yes, but it runs through showdialog and email =window.email instantly and so it never gives it a chance to update, as in wait for login click to occur. I almost need it to be like await login click ? Does this make sense?

– Explorex
Nov 20 '18 at 12:12















No, ShowDialog doesn't return until loginWindow has been closed. And you can't click on a button after a window has been closed...

– mm8
Nov 20 '18 at 12:17





No, ShowDialog doesn't return until loginWindow has been closed. And you can't click on a button after a window has been closed...

– mm8
Nov 20 '18 at 12:17




1




1





It needs to close before the method that calls ShowDialog continues to the next line...if you don't want this you could raise an event from the login window and handle this in your app class.

– mm8
Nov 20 '18 at 12:35





It needs to close before the method that calls ShowDialog continues to the next line...if you don't want this you could raise an event from the login window and handle this in your app class.

– mm8
Nov 20 '18 at 12:35


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385195%2freturning-value-from-window-to-class-wpf%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?