S
S
Suren2018-01-16 17:11:11
OOP
Suren, 2018-01-16 17:11:11

Windows Forms, how do I access form elements from another class?

Let's say I have a method like this

public async void Updated()
        {
            if (!string.IsNullOrEmpty(UpdateMagazinTB.Text) && !string.IsNullOrWhiteSpace(UpdateMagazinTB.Text) &&
                !string.IsNullOrEmpty(StoreNameTB.Text) && !string.IsNullOrWhiteSpace(StoreNameTB.Text))
            {


                SqlCommand sqlCommand = new SqlCommand("UPDATE [Stores] SET [StoreName][email protected] WHERE [StoresId][email protected]", sqlConnection);

                sqlCommand.Parameters.AddWithValue("Id", UpdateMagazinTB.Text);
                sqlCommand.Parameters.AddWithValue("Name", StoreNameTB.Text);

                await sqlCommand.ExecuteNonQueryAsync();
            }
        }

It updates the database with information about the Store by its ID.
The information itself is taken from the Form element (TextBox).
So, what is the essence of the question, how can I do this method through the Interface by implementing it in another class, after which this method can be transferred to the main class of the form handler?
As I have not tried to do what I just didn’t invent, but everything stops on the fact that from another class I can’t reach the Form Elements (Windows Forms) in any way.
Is there any way to work with form elements from other classes?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jeffrey, 2018-01-17
@Hardch

Change the Modifiers property of the TextBox to public, then in the method that should receive the value of the TextBox, pass an instance of the form and get the value through it

public void ShowTextBoxValue(Form1 form)
{
    MessageBox.Show(form.textBox1.Text);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question