Click here to Skip to main content
15,902,299 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have created 2 Forms in a Windows Form Application.

I have putted textboxes and comboboxes on Form2. I want to call the Comboboxes and Textboxes in the methods of Form1. Basically, I want to use the components that are on Form 2 in the methods of Form 1.

How can I call components that are on Form 2 in the methods of Form 1? Is it possible?
Posted

"Don't" is the simple answer.

You can, but it is very, very strongly advised that you don't.

If you want to access the contents of controls on Form2, then use public properties to get and set them indirectly. That way the desig of Form2 is not locked in stone for all time.

If you want to use the same group of controls on two different forms, then create them as a UserControl (with appropriate public properties) and use an instance of that on each form.
 
Share this answer
 
Comments
JacoBosch 31-Jan-12 7:34am    
Thanx OrignalGriff

I want to find a record in a datagridview by entering a value in a Textbox and it will find the specefic record in the datagridview. I have written the code on Form 1 and it is working 100%. My datagridview and textbox is on Form 1.

foreach (DataGridViewRow Row in dtgIngenico.Rows)
{
if (Row.Cells["TerminalID"].Value.ToString() == txtTerminalID.Text)
{
Row.Selected = true;
break;
}

How can I change the code to enter the TerminalID in a textbox on Form 2 and it must find the record in the datagridview on Form 1. The problem that I have is that it is not finding the datagridview in Form 2.

Thanx for your help.

Regards
OriginalGriff 31-Jan-12 7:46am    
The best way is to create an event in Form2 which Form1 subscribes to when it creates the Form2 instance.
In the Form1 handler, it accesses Form2 via a public property of the instance to get the textbox value, and searches the datagridview.
This assumes that you want the data displayed on Form1. Otherwise, the event handler passes it back to Form2 via another public property.
JacoBosch 31-Jan-12 8:10am    
OriginalGriff, I have never used or created an instance of a Form. Do you have an article that I can read or an example?
OriginalGriff 31-Jan-12 8:27am    
Um.
Yes, you have.

Form1 form = new Form1();

Creates an instance of Form1, and stores it in a variable called "form"
JacoBosch 31-Jan-12 8:41am    
Aaaah, now I get you. I have created an instance of a form globally and not in an event.

I will try that. Thanx
This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

—SA
 
Share this answer
 
Comments
JacoBosch 1-Feb-12 3:19am    
Hi SAKryukov. Can you please have a look at Solution 4 below.
JacoBosch 1-Feb-12 4:12am    
I found the solution. Thanx for your help
yes it is possible
See the example

C#
Form2 f = new Form2();
           f.Show();
           Control[] c= f.Controls.Find("comboBox1", false);
           ComboBox cb = (ComboBox)c[0];
           cb.Width = 500;
 
Share this answer
 
I have written the following in Form2

//Form2
 public string getTerminalID
        {
            get {return txtTerminalID.Text;}
        }

private void btnFind_Click_1(object sender, EventArgs e)
        {
                     this.Close();
        }


//Main Form

public void getTerminalID(string record)
        {
            foreach (DataGridViewRow Row in dtgIngenico.Rows)
            {
                if (Row.Cells["TerminalID"].Value.ToString() == record)
                {
                    Row.Selected = true;
                    break;
                }
            }
            
        }

private void btnFind_Click(object sender, EventArgs e)
{
    frmFilter frmfil = new frmFilter();          
    frmfil.ShowDialog();
    findrecord(frmfil.getTerminalID);
}
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900