how can i acces a control from another form

hey i, how can i acces a control (that its in Form1) from Form2...

ex. in form1 i have a textbox, and i want to change text from form2.

 




Answer this question

how can i acces a control from another form

  • Canalside Studios

    Hi

    You can do like this:

    (Application.OpenForms["frmMain"] as frmMain).CurBrowser.Navigate(MyTextBox.Text);

    and you must change your CurBrowser property 'Modifiers' to public.

    Yours

    Markku


  • zaabdullah

    Markku, thank you, but it is not what I am looking for. My code that I posted is a part of a function which is called when I press a button on my form.

    On top of that the function is a part of a different class, not Form1, but it is still in the same namespace.

    I do not think your code can be used in my situation Can it



  • Cest la vie

    thx,

    (Application.OpenForms["frmMain"] as frmMain).CurBrowser.Navigate(MyTextBox.Text);

    it just did :D



  • MrJP

    Hi,
    you can provide a property to access the control, e.g.:

    class Form1
    {
    public TextBox MyTextBox
    {
    get { return myTextBox; }
    }
    }

    in the other form:

    form1.MyTextBox.Text = "New Text";


  • Landon Parks

    Hi

    The OOP style way is make new property in form1 like this:

    public string SomeText

    {

    get {return textBox1.Text;}

    set {textBox1.Text = value;}

    }

    Now, if you make any change on form1 (rename textBox1, change it to label ect.) you don't need changed other forms.

    Yours

    Markku


  • Vinit Tyagi

    n0n4m3 wrote:
    Hi,
    you can provide a property to access the control, e.g.:

    class Form1
    {
    public TextBox MyTextBox
    {
    get { return myTextBox; }
    }
    }

    in the other form:

    form1.MyTextBox.Text = "New Text";

    It is almost what I've been looking for as well. My problem is somewhat more complicated, however.

    I generate a Tabpage at runtime and want to access (reference) all or some objects on this page. There is a candlePlot and a grid. They are all separate classes. The code I have is like this:

    TabPage tb1 = new TabPage ();
    Button btn = new Button ();
    CandlePlot cp = new CandlePlot ();
    cp.DataSource = dt; // I need to reference this to be able to change the plot at runtime
    this.costPS = new NPlot.Windows.PlotSurface2D ( );
    this.costPS.Add ( new Grid ( ) );
    this.costPS.Add ( cp );
    tb1.Controls.Add ( this.costPS );

    My goal is to try to change the DataTable at the DataSource at runtime when I need to change the plot. How can I access the CandlePlot object with possibly using your construct: {get return cp;} or something like this I've tried to play with my code after I read your post but so far--no luck.

    I won't mind to access the button btn either.

    One way I've tried to approach the problem yesterday was to try to enumerate all TabControl's pages, find the TabPage I need and check their child objects. So far I haven't gotten anywhere.

    I will appreciate any help.

    Thanks.



  • ChrisMakesMovies

    hmmm i didn't quite understand that

    let me tell exactly what my problem is

    in frmMain i have a browser named CurBrowser;

    and i want CurBrowser to navegate to a specific URL (from the textbox in frmOpen)

    and isn't there anything like calling the control in frmMain from frmOpen to be easyer something like this:

    frmOpen

    __________________________________________

    private void button1_Click(object sender, EventArgs e)

    {

    frmMain.CurBrowser.Navigate(MyTextbox.Text);

    }

    ______________________________________________________



  • hrubesh

    Something like this

    public Form1 : Form

    {

    Button btn = new Button();

    CandlePlot cp = new CandlePlot();

    public Form1()

    {

    TabPage tb1 = new TabPage();

    cp.DataSource = ...

    }

    public Button myButton

    {

    get { return btn; }

    }

    public CandlePlot myCandlePlot

    {

    get { return cp; }

    }

    }

    Yours

    Markku


  • JPC16

    Hi Alex

    OK. Then you can do so that you give name those components and then use Components collection to find them.

    Like this:

    TabPage tb1 = new TabPage();

    tb1.Name = "tabPageName";

    CandlePlot cp = new CandlePlot();

    cp.Name = "candlePlotName";

    and then on the other form you can find the button

    TabControl tc = (Application.OpenForms["yourFormName"] as yourFormType).Controls["tabControlName"] as TabControl;

    CandlePlot cp = (tc.Controls["tabPageName"] as TabPage).Controls["candlePlotName"] as CandlePlot;

    I hope this help you.

    Yours

    Markku


  • how can i acces a control from another form