You must Sign In to post a response.
  • Category: .NET

    How to access controls from one form to another in C#

    how to access form controls in another form, means i have two forms form1 and form2, in form1 it has two textbox and button i want to access textbox text and button properties in form2 how to do this in c#, in vb form1.textbox1.text it works but in c# don't work plz solve the problem
  • #754685
    Hi Mahesh,

    if u want to access form controls in another form then declare a FindControl object first:

    FindControl fc = new FindControl();

    Now if you want to access the Text property on a TextBox control of another form you simply call the syntax like:

    FindControl.Ctrl("The Form where the control is on", "the name of the control")
    For examples;

    ((TextBox)fc.Ctrl(fc.TheForm("Form1"), "te")).Text = "Hello World - Form 1";
    ((TextBox)fc.Ctrl(fc.TheForm("Form2"), "te")).Text = "Hello World - Form 2";
    ((TextBox)fc.Ctrl(fc.TheForm("Form2"), "e")).Text = "Hello World - Panel";

    if u want to disply the controls of grid in other form then follow the below link also:

    DataGridView dgv = ((DataGridView)fc.Ctrl(fc.TheForm("Form2"), "dg"));
    dgv.Rows[0].Cells[0].Value = "Hello World";

    ((DataGridView)fc.Ctrl(fc.TheForm("Form2"), "dg")).Rows[0].Cells[0].Value = "Grid View Column 1 - Hello World";
    ((DataGridView)fc.Ctrl(fc.TheForm("Form2"), "dg")).Rows[0].Cells[1].Value = "Grid View Column 2- Hello World";

    hope this code helpful for UU..


  • #754709
    Try with the object of the form as below.

    Form1 form1 = new Form1();
    form1.MyLabel.Text = "Hello Anil!";

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #754772
    You can use another method . in first form designer.cs file you can make respective control public.

    then you can find that control on any form in same project.

    using following code
    frmTest objTest= new frmTest();

    -- Suggestions are always welcome
    Umesh Bhosale

  • #755019
    Thank you

  • Sign In to post your comments