0

In my application, I have a mainform which is the MdiParent of all other forms. On login, by default this form is opened and from menustrip in this form we are navigating to other forms like this:

public partial class MainMenuForm : Form
{
    public MainMenuForm()
    {
        InitializeComponent();
        this.WindowState = FormWindowState.Maximized;
    }

    private void humanResourceToolStripMenuItem_Click(object sender, EventArgs e)
    {
        HumanResourceForm humanresourceform = new HumanResourceForm();
        humanresourceform.MdiParent = this;
        humanresourceform.Show();
    }
}

Inside the HumanResourceForm I have a button which will navigate to another form say EmployeeTransferForm:

private void button1_Click(object sender, EventArgs e)
{
    Administraror.Humanresource.EmployeeTransferForm emptranfrm = new Administraror.Humanresource.EmployeeTransferForm();
    emptranfrm.ShowDialog();
}

Now my problem is inside the EmployeeTransferForm I want to get some values from the controls of HumanResourceForm. Also the user should not be allowed to close the HumanResourceForm when the EmployeeTransferForm is open or active.

I also want to get the Text property of a TextBox of HumanResourceForm in EmployeeTransferForm like:

public partial class EmpLoctnChangeForm : Form 
{
    public EmpLoctnChangeForm( )
    {
        InitializeComponent();
    }

    private void EmpLoctnChangeForm_Load(object sender, EventArgs e)
    {
        intemppk = humanresourceform.txtpk.text;
    }
}

Expecting some nice advice from all.
Thanks in advance.

jsbeckr
  • 1,183
  • 1
  • 10
  • 24
Sreenath Ganga
  • 696
  • 4
  • 19
  • 48
  • You can use one of those: 1. Pass HumanResourceForm control values to EmployeeTransferForm constructor. 2. Create properties in EmployeeTransferForm and set them before showing form – Renatas M. May 20 '12 at 19:02

3 Answers3

0

You can create a public static variable to access it from other places:

public static HumanResourceForm Instance;

Then set the value in your constructor:

Instance = this;

Then in your EmpLoctnChangeForm:

intemppk = HumanResourceForm.Instance.txtpk.Test;
Jason Goemaat
  • 28,692
  • 15
  • 86
  • 113
0

I would suggest you to define some events in EmployeeTransferForm class (form where you want to get the properties' values) and implement them in Humanresources (form that you want to access the properties of). I would not recommend passing the whole Form object in object oriented architecture.

So, the code form the EmployeeTransferForm can look like this:

public class EmployeeTransferForm: Form
{
    public delegate Text GetTextHandler();

    GetTextHandler getSampleTextFromTextBox = null;
    public event GetTextHandler GetSampleTextFromTextBox
    {
        add { getSampleTextFromTextBox += value; }
        remove { getSampleTextFromTextBox -= value; }
    }

    //the rest of the code in the class
    //...
}

And for the Humanresources, like this:

class Humanresources : Form
{
    private void button1_Click(object sender, EventArgs e)
    {
        Administraror.Humanresource.EmployeeTransferForm emptranfrm = new Administraror.Humanresource.EmployeeTransferForm();
        emptranfrm.GetSampleTextFromTextBox += new EmployeeTransferForm.GetTextHandler(GetSampleTextFromTextBox_EventHandler);
        emptranfrm.ShowDialog();
    }


    Text GetSampleTextFromTextBox_EventHandler()
    {
        return myTextBox.Text;
    }

    //the rest of the code in the class
    //...
}
Lukasz M
  • 5,635
  • 2
  • 22
  • 29
0

Perhaps look at implementing some sort of 'ViewController', which is responsible for these 'cross-view' communications and form creation. This approach would better encapsulate the process of creating forms (views) and sharing data between them. It would provide for less coupling of views, for example if you wanted to change how the view displays information, you could modify one form without having to modify another Form which depends on the controls on it being externally visible, and named correctly, or even existing at all.

This view controller can be passed as a constructor argument to views, or if appropriate you could implement it as a system-wide singleton (since in this instance there is unlikely to be more than 1 view controller for the application context).

An example interface might look like;

interface IViewController<T>
    where T : Form
{ 
    void ShowForm();
    void ShowFormModal();
}

And you can start to create specialised ViewControllers for different scenarios;

public IEmployeeViewController : IViewController<EmployeeForm>
{
    void ShowForm(string intemppk);
}

This is really a very basic suggestion as to an architecture, which is more complex than I can describe in a single post, but it might set you on the right track.

RJ Lohan
  • 6,497
  • 3
  • 34
  • 54