Hi Sir, Mam,
I would like to ask how can I access the textbox value like UserName in Login WPF into
another WPF form? I am trying to initialize the form LoginWindow objLogin = new LoginWindow ()
MessageBox.Show(objLogin.user_name_txt.Text); but it return null
Thank you.
Regards,
Rodel
Tahir AnsariPosted Sep 5, 2023, 6:11 AM
Create a Property in the LoginWindow: In your LoginWindow class, create a public property that exposes the value of the user_name_txt TextBox. This allows you to access the value from another window.
Initialize and Show the LoginWindow: In your other WPF window where you want to access the value, create an instance of LoginWindow, show it, and then access the UserName property to get the value
Make sure you call ShowDialog() instead of Show() if you want the login window to be modal, which means it will block interaction with other windows until it's closed.
Sam HobbsPosted Sep 6, 2023, 4:11 AM
There are many ways to do it. You can use the LoginWindow class to store the data, as has been suggested. The professional way is to create a separate class and create an instance of that class outside of a form and then pass that instance to the other forms, including the LoginWindow class.