'Windows Form TextBox User input
I'm new to windows forms and have never tried it before, i mainly do razorpages and stuff along those lines.
I'm making a form that is suppose to create an account. i have a Class account with a bunch of properties already, the problem is that i can't seem to figure out how i make whatever is written in the textboxes = my Account properties.
I've tried making properties in the form and initializing them to my account properties liek this Firstname = Account.FirstName; but i just get "object reference not set to an instance of an object" error when i run it.
public partial class AddAccountDialog : Form
{
#region Properties
public Account Account { get; set; }
public string Id { get; set; }
public string Firstname { get;
set; }
public string Lastname { get; set; }
public string Username { get; set; }
public string Password { get; set; }
private bool Accepted { get; set; }
#endregion
public AddAccountDialog()
{
#region Applying Properties
Id = Account.UserID;
Firstname = Account.FirstName;
Lastname = Account.LastName;
Username = Account.Username;
Accepted = false;
#endregion
#region Setup Methods
InitializeComponent();
SetupForm();
ShowDialog();
#endregion
}
private void SetupForm()
{
}
private void textBox_user_id_TextChanged(object sender, EventArgs e)
{
Id = textBox_user_id.Text;
}
private void textBox_firstname_TextChanged(object sender, EventArgs e)
{
Firstname = textBox_firstname.Text;
}
private void textBox_lastname_TextChanged(object sender, EventArgs e)
{
Lastname = textBox_lastname.Text;
}
private void textBox_username_TextChanged(object sender, EventArgs e)
{
Username = textBox_username.Text;
}
private void textBox_password_TextChanged(object sender, EventArgs e)
{
Password = textBox_password.Text;
}
private void button_accept_changes_Click(object sender, EventArgs e)
{
Accepted = true;
if (textBox_firstname.Text == "" ||
textBox_lastname.Text == "" ||
textBox_username.Text == "" ||
textBox_password.Text == "") { MessageBox.Show("Udfyld alle kasser"); }
else
{
//Account.FirstName = textBox_firstname.Text;
//Account.LastName = textBox_lastname.Text;
//Account.Username = textBox_username.Text;
//Account.UserID = textBox_user_id.Text;
Account = new Account();
Account.ToString();
//Close();
}
}
public Account GetResponse()
{
Close();
if (Accepted) return Account;
else return null;
Dispose();
}
}
Account class
public class Account
{
#region Properties
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName { get { return $"{FirstName} {LastName}"; } }
public string Username { get; set; }
public string UserID { get; set; }
public string[] AllowedPermissionIDS { get; set; }
public bool IsAdmin { get; set; }
#endregion
/// <summary>
///
/// </summary>
public Account(string firstName, string lastName, string username, string userId, string[] allowedPermissionIds, bool isAdmin)
{
FirstName = firstName;
LastName = lastName;
Username = username;
UserID = userId;
AllowedPermissionIDS = allowedPermissionIds;
IsAdmin = isAdmin;
}
public Account()
{
}
Solution 1:[1]
Have you tried this in the button_accept_changes_Click event? It is unclear why the code is commented out, but it should go something likeā¦
Account = new Account();
Account.FirstName = textBox_firstname.Text;
Account.LastName = textBox_lastname.Text;
Account.Username = textBox_username.Text;
Account.UserID = textBox_user_id.Text;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | JohnG |
