how to assign class to clear the textbox on the form?
Printable View
how to assign class to clear the textbox on the form?
????
How about: Textbox1.Clear
Solid,
Although I am aware that this question is an extension of your thread here:
http://www.vbforums.com/showthread.php?t=621108
You can't assume that everyone else on the forum is going to know what you are talking about, so you should be a little bit more descriptive in your post.
As was mentioned in your other thread, if you want your class to be able to interact directly with your form, you need to provide that class a reference to your form. Perhaps in the constructor for DBConnection.
Failing that, you should handle the clearing of the textboxes within the form code, once you get a response from your Register method call.
Gary
on class.cs this is not a formCode:public void InsertRegister(string userName, string password, string sex, string email)
{
try
{
string query = "INSERT INTO login (userid, user_pass, sex, email) VALUES('" + userName + "', '" + password + "', '" + sex + "', '" + email + "')";
//open connection
if (this.OpenConnection() == true)
{
//create command and assign the query and connection from the constructor
MySqlCommand cmd = new MySqlCommand(query, connection);
//Execute command
cmd.ExecuteNonQuery();
MessageBox.Show("Register Successful.");
}
}
catch (Exception ex)
{
if (ex.Message.IndexOf("_userid") >= 0)
MessageBox.Show("That username already exists. Please change the login name.");
else
MessageBox.Show(ex.Message);
}
finally
{
//close connection
this.CloseConnection();
}
}
MessageBox.Show("Register Successful.");
i want the all textbox will be clear on form that will assign class to do it when it success.
So, one way to fix this would be to do this:
And this gives you a reference to the Form, and from there, if you have Public properties declared on this form, you can do something like:Code:public void InsertRegister(string userName, string password, string sex, string email, Form myForm)
However, I would not advice that you do this work in the class. This work should be done in the UI thread, your class should know nothing about the TextBoxes that it is linked to.Code:myForm.UserNameText.Text = String.Empty;
Gary
how about this ?how can i call the public void reset() in the form. to class?Code:public void Reset()
{
if (btnClose.Text == "&Close")
{
Close();
}
else
{
btnClose.Text = "&Close";
txtUsername.Clear();
txtPassword.Clear();
txtRepassword.Clear();
txtEmail.Clear();
cboGender.SelectedIndex = 0;
errorProvider1.Clear();
errorProvider2.Clear();
errorProvider3.Clear();
errorProvider4.Clear();
}
}
solid2005,
Did you read my previous post? It tells you exactly what you need to do. i.e. any public member of a form instance can be accessed if you pass in a reference to the the instance of the form when you call InsertRegister, however, I would strong urge you to not go down that route. Rather, I would recommend that you do something like this:
And then do something like:Code:public void InsertRegister(string userName, string password, string sex, string email)
{
try
{
string query = "INSERT INTO login (userid, user_pass, sex, email) VALUES('" + userName + "', '" + password + "', '" + sex + "', '" + email + "')";
//open connection
if (this.OpenConnection() == true)
{
//create command and assign the query and connection from the constructor
MySqlCommand cmd = new MySqlCommand(query, connection);
//Execute command
cmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
if (ex.Message.IndexOf("_userid") >= 0)
throw new Exception("That username already exists. Please change the login name."));
else
throw;
}
finally
{
//close connection
this.CloseConnection();
}
}
The above was written off the top of my head, and not in Visual Studio, so there may be errors, but hopefully you get the idea.Code:try
{
DbConnection.InsertRegister("gep13", "password", "M", "[email protected]");
Reset();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
Gary
i got it work i use
i share it ^_^Quote:
public bool InsertRegister(string userName, string password, string sex, string email)
{
bool _check = false;
That is fine, but then you lose the clarity about what happened.
i.e. if you return false, when the username already exists, how do you know that that happened? Basically you don't. By throwing, or re-throwing an exception, you get to keep all the information about the problem, not just a false.
Gary
now i fix itCode:public bool InsertRegister(string userName, string password, string sex, string email)
{
bool _check = false;
try
{
string query = "INSERT INTO login (userid, user_pass, sex, email) VALUES('" + userName + "', '" + password + "', '" + sex + "', '" + email + "')";
//open connection
if (this.OpenConnection() == true)
{
//create command and assign the query and connection from the constructor
MySqlCommand cmd = new MySqlCommand(query, connection);
//Execute command
cmd.ExecuteNonQuery();
MessageBox.Show("Register Successful.");
_check = true;
}
}
catch (Exception ex)
{
_check = false;
if (ex.Message.IndexOf("_userid") >= 0)
MessageBox.Show("That username already exists. Please change the login name.");
else
MessageBox.Show(ex.Message);
}
finally
{
//close connection
this.CloseConnection();
}
return _check;
}
Hey,
I think you are still missing the point...
Your class, which knows nothing about the fact that it is in a Windows Form Application, i.e. it should be able to be used within a Windows Service, or a Web Application, shouldn't be making a call to MessageBox.Show(). It has no way of knowing that this will work in the application you are building. Your class should be completely agnostic, and any visual indicators, should be done in the main UI code.
Gary
Code:if (dbConnect.InsertRegister(this.txtUsername.Text, this.txtPassword.Text, this.cboGender.Text, this.txtEmail.Text) == true)
{
Reset();
}
on formCode:public void Reset()
{
if (btnClose.Text == "&Close")
{
Close();
}
else
{
btnClose.Text = "&Close";
txtUsername.Clear();
txtPassword.Clear();
txtRepassword.Clear();
txtEmail.Clear();
cboGender.SelectedIndex = 0;
errorProvider1.Clear();
errorProvider2.Clear();
errorProvider3.Clear();
errorProvider4.Clear();
}
}
you have a example code for this? http://www.vbforums.com/showthread.php?p=3842663
if i have create new client for login the client will be connect on server ip the server will check if login correct username and password.