Multiple Forms :(

3xodus

Freshman
Joined
Dec 26, 2004
Messages
47
Location
Derbyshire, UK
*sigh* I'm almost embarrased to ask. This is in C# for Visual Studio 2005 by the way.

I've spent a few hours last night and literally *hours* trying to figure this out today, and can't for the life of me get it to work.

There's a fair amount of threads on this already, but the majority deal with accessing controls on Form2 from Form1, which I can do fine. What I'm having problems with is accessing properties on Form1 from Form2.

That first thing I thought was just:
C#:
Form1 firstform = new Form1();
 firstForm.txtBox.Text = "test";
But it dosent work, and as I understand it the reason it dosent work is that I've created another instance of Form1 and I'm accessing *that*, not the existing one.

The only other solutions I've found were for VB .NET and involved adding code to the 'constructor'. I couldn't find any information about how to get this to work for C#, is that what I should be looking for?

Any help appreciated, thankyou :)
 
3xodus said:
*sigh* I'm almost embarrased to ask. This is in C# for Visual Studio 2005 by the way.

I've spent a few hours last night and literally *hours* trying to figure this out today, and can't for the life of me get it to work.

There's a fair amount of threads on this already, but the majority deal with accessing controls on Form2 from Form1, which I can do fine. What I'm having problems with is accessing properties on Form1 from Form2.

That first thing I thought was just:
C#:
Form1 firstform = new Form1();
firstForm.txtBox.Text = "test";
But it dosent work, and as I understand it the reason it dosent work is that I've created another instance of Form1 and I'm accessing *that*, not the existing one.

The only other solutions I've found were for VB .NET and involved adding code to the 'constructor'. I couldn't find any information about how to get this to work for C#, is that what I should be looking for?

Any help appreciated, thankyou :)
short of changing the modifiers on txtBox to public or internal in the designer (not recommended in most cases) , do the following in Form1.cs:

C#:
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
 
    public Form1(string initialValue): this(){ this.txtBox.Text = initialValue; }
    public string TextBoxText
    { 
        get { return this.txtBox.Text;}
        set { this.txtBox.Text = value;}
    }
}

usage:
C#:
Form1 frm = new Form1("foobar");
frm.Show();
Form1 frm2 = new Form1();
frm2.TextBoxText = "foobar2";
frm2.Show();
 
As you say the problem with the code you tried is that you are accessing a seperate instance of the form not the original one. The answer you seek is contained within the other posts you have seen.

Take two forms MainForm and OptionsForm. If you set the contructor of the OptionsForm to accept a MainForm as a parameter, and then store a reference locally then you can access any public method or object from that MainForm. There are examples on the forum, but if you need more help please feel free to ask.
 
Joe Mamma:
Hmm, I already have the modifiers on the controls I wanted to access set to Public. I didn't know that was a bad thing but it didn't work anyway, so I'll set them back to private.

Your other solution does seem nice, but I don't think I can use that - on my Form1 there are several images and several labels which can be modified from Form2. Form2 has controls to move and resize images, and change the font, forecolor and textalignment of labels. This is why I was hoping to be able to just use firstForm.lblLabel.Location = new Point(x,y) and so on to change properties.

Out of curiosity, what is the disadvantage / bad reasons for having the modifier property "public"?

Cags:
That's what I've read yes. I've seen one or two VB.NET example on these forums about using constructors, but I can't make sense of how to use them (where is the constructor? what code can I add to it/what are good times to use the constructor? - that's what I'm about to look up though)
Maybe learning about constructors would be a good thing (I'm about to search MSDN) but it really seems to me that it would be overkill for this purpose.

I just don't understand why it's so easy to do
C#:
SecondForm frmTwo = new SecondForm();
  frmTwo.txtBox.Text = "whatever";
but when I want to access properties on the first form from the second one, I can't use the same method. It just seems odd to me :|

By the way, I just tried on Form2, Form frm = this.Owner;, and with that I can now access Form1's properties, but none of it's controls (so I can do frm.Text = "test"; but not frm.txtBox.Text = "test";. )
Am I on the right lines there or is it wishful thinking?

Thanks guys :)
 
ok . . .

create a C# windows application - ManagingControls.
add new class file. . . call it ManagedControl.cs.

put this code in ManagedControl.cs:
C#:
namespace ManagingControls
{
    public class ManagedControl 
    {
        string _name;
        System.Windows.Forms.Control _control;
        public ManagedControl(string name, System.Windows.Forms.Control control)
        {
            _name = name;
            _control = control;
        }
        public string Name
        {
            get { return _name; }
        }
        public System.Windows.Forms.Control Control
        {
          get { return _control; }
        }
    }
    
    public class ManagedControlList: List<ManagedControl>{}
    public interface IManagedControlContainer
    {
        ManagedControlList ManagedControls { get; }
    }
}

add a new form FormManager.cs.
drop a listbox, 2 textboxes, and a button on it and put this code in FormManager.cs:
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace ManagingControls
{
    public partial class FormManager : Form
    {
        public FormManager()
        {
            InitializeComponent();
        }
        public FormManager(IManagedControlContainer _container): this()
        {
            listBox1.DataSource = _container.ManagedControls;
            listBox1.DisplayMember = "Name";
            listBox1.ValueMember = "Control";
            textBox1.DataBindings.Add("Text", listBox1.SelectedValue, "Text");
            textBox2.DataBindings.Add("Text", listBox1.SelectedValue, "Left");
            this.button1.Click += button1_Click;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }    
}

drop a textbox and a button on form1.cs (the main form of the app) and put this code in form1.cs
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ManagingControls
{
    public partial class Form1 : Form, IManagedControlContainer 
    {
        public Form1()
        {
            InitializeComponent();
            button1.Click += button1_Click;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            FormManager frm = new FormManager(this);
            frm.ShowDialog();
        }
        ManagedControlList IManagedControlContainer.ManagedControls
        {
            get
            {
                ManagedControlList list = new ManagedControlList();
                list.Add(new ManagedControl(textBox1.Name, textBox1));
                return list;
            }
        }
    }
}


run the app .. . press the button on Form1. . .type some text into textbox1.

type some numbers into textbox2.

watch what happens.
 
Back
Top