0

I want to handle and control a Class Library project in C# with another application.

I intend to follow below steps,

  1. Create a Class Library Project
  2. Add windows form inside this Class Library Project
  3. Create another project but this time a Windows Form Application

  4. Open the Windows form (created in step 2) inside that Class library project through Windows Form Application (Created in step 3)

.

I have previously used UI automation to handle WPF or Windows form applications but how can i handle Class Library project?

Need guidance please.

EDIT:

I have opened the Class Library Form by adding the dlls in Windows Form Application and then creating the object and using the show method to display form like below,

        ClassLibTestProject.Form1 f = new ClassLibTestProject.Form1();
        f.Show();

Now, I want to change the text in the textbox which is present in ClassLibrary project. The name of the textbox is textbox1. I want to do something like this textbox1.text ="Text changed from Windows Form Application"; But how should I get the handle of this textbox in Windows Form Application?

Faran Saleem
  • 404
  • 1
  • 7
  • 31
  • Add references to the assemblies required by windows form to the library project. – Fabio Jun 13 '19 at 04:54
  • I want to open the form inside class library project from the 2nd application – Faran Saleem Jun 13 '19 at 04:59
  • 1
    what you mean by "inside class library"? – Fabio Jun 13 '19 at 05:07
  • I did add the dlls and was able to show the form from Class Library in my Windows Form Project. Now i want to get the handle of the textbox in class library form and change its text through windows form project. How can i achieve it? – Faran Saleem Jun 13 '19 at 06:13
  • If form is in class library then TextBox would be accessible from class library as well. But if you mean that some code in Winforms project need to update text of form's textbox from class library, then instead of accessing textbox, create a method on the form class, which Winforms project can call through form instance ( something like this: `formFromClassLibrary.ChangeText("new text")`). – Fabio Jun 13 '19 at 06:20
  • The textbox is in class library project and I am trying to update the text of it from Windows Form Application – Faran Saleem Jun 13 '19 at 06:23
  • Are you using this Solution to self-train in UI Automation? Otherwise, why do you need to *find* the TextBox? Can't you just have the library method that shows the Form also return the handle of the Control you need (for example)? Can you explain a little better your context? – Jimi Jun 13 '19 at 06:23
  • Please see my edit – Faran Saleem Jun 13 '19 at 06:33

1 Answers1

1

Do not expose form controls to "outside world", instead provide public method which can be called by form consumers.

Inside method you can update you control.

In library project

public class Form1
{
    public void UdpateTextBoxWith(string newText)
    {
        textbox1.Text = newText;
    }
}

In Winforms application

var form = new ClassLibTestProject.Form1();
form.Show();

form.UdpateTextBoxWith("New text");
Fabio
  • 31,528
  • 4
  • 33
  • 72