Results 1 to 15 of 15

Thread: Class to form

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    63

    Class to form

    how to assign class to clear the textbox on the form?

  2. #2
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    Re: Class to form

    ????
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Class to form

    How about: Textbox1.Clear

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    63

    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.

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  7. #7

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    63

    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.

  8. #8
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  9. #9

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    63

    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 ^_^

  10. #10
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  11. #11

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    63

    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

  12. #12
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  13. #13

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    63

    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

  14. #14

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    63

    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.

  15. #15
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Class to form

    Quote Originally Posted by solid2005 View Post
    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
  •  



Click Here to Expand Forum to Full Width