|
-
Jul 12th, 2010, 07:33 AM
#1
Thread Starter
Member
Class to form
how to assign class to clear the textbox on the form?
-
Jul 12th, 2010, 07:42 AM
#2
-
Jul 12th, 2010, 07:44 AM
#3
Re: Class to form
How about: Textbox1.Clear
-
Jul 12th, 2010, 07:49 AM
#4
Re: Class to form
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
-
Jul 12th, 2010, 08:15 AM
#5
Thread Starter
Member
Re: Class to form
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();
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();
}
}
on class.cs this is not a form
MessageBox.Show("Register Successful.");
i want the all textbox will be clear on form that will assign class to do it when it success.
-
Jul 12th, 2010, 08:24 AM
#6
Re: Class to form
So, one way to fix this would be to do this:
Code:
public void InsertRegister(string userName, string password, string sex, string email, Form myForm)
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:
myForm.UserNameText.Text = String.Empty;
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.
Gary
-
Jul 12th, 2010, 10:38 AM
#7
Thread Starter
Member
Re: Class to form
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();
}
}
how about this ?how can i call the public void reset() in the form. to class?
Last edited by solid2005; Jul 12th, 2010 at 10:51 AM.
-
Jul 12th, 2010, 11:40 AM
#8
Re: Class to form
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:
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();
}
}
And then do something like:
Code:
try
{
DbConnection.InsertRegister("gep13", "password", "M", "[email protected]");
Reset();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
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.
Gary
-
Jul 13th, 2010, 04:33 AM
#9
Thread Starter
Member
Re: Class to form
i got it work i use
public bool InsertRegister(string userName, string password, string sex, string email)
{
bool _check = false;
i share it ^_^
-
Jul 13th, 2010, 04:41 AM
#10
Re: Class to form
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
-
Jul 13th, 2010, 05:35 AM
#11
Thread Starter
Member
Re: Class to form
Code:
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;
}
now i fix it
-
Jul 13th, 2010, 06:10 AM
#12
Re: Class to form
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
-
Jul 13th, 2010, 07:02 AM
#13
Thread Starter
Member
Re: Class to form
Code:
if (dbConnect.InsertRegister(this.txtUsername.Text, this.txtPassword.Text, this.cboGender.Text, this.txtEmail.Text) == true)
{
Reset();
}
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();
}
}
on form
-
Jul 13th, 2010, 07:03 AM
#14
Thread Starter
Member
Re: Class to form
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.
-
Jul 13th, 2010, 07:29 AM
#15
Re: Class to form
 Originally Posted by solid2005
Code:
if (dbConnect.InsertRegister(this.txtUsername.Text, this.txtPassword.Text, this.cboGender.Text, this.txtEmail.Text) == true)
{
Reset();
}
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();
}
}
on form
Yes, this is now the correct approach.
However, you are still losing the information about the exception, if any, that is thrown internally.
I would highly recommend that you try the approach that I suggested in my earlier post.
Gary
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|