Melody Hajian
Melody Hajian

Reputation: 285

calling a method from another form c#

I have a Mainform like this:

public partial class MainForm : Form
{
    string FileName = "";

    public MainForm()
    {
        InitializeComponent();
    }

    private void goToToolStripMenuItem_Click(object sender, EventArgs e)
    {

    }

    private void menuItem_Click(object sender, EventArgs e)
    {
        ToolStripMenuItem menuItem = (ToolStripMenuItem ) sender;

        switch (menuItem.Name.Replace("ToolStripMenuItem",""))
        {
            case "new":
                MenuItemNew();
                break; 
            case "open" :
                MenuItemOpen();
                break;
            case "save":
                MenuItemSave();
                break;
            case "saveAs" :
                MenuItemSaveAs();
                break;
          case "exit" :
                MenuItemExit();
                break;
            case "selectAll" :
                MenuItemSelectAll();
                break;
        }
    }

    private void MenuItemSelectAll()
    {
        textBox.SelectAll();
    }

    private void MenuItemExit()
    {
        this.Close();
    }

    private void MenuItemSaveAs()
    {
        SaveFileDialog sfd = new SaveFileDialog();
        sfd.Filter = "Text Document |*.txt";
        if (sfd.ShowDialog() == DialogResult.OK)
        {
            StreamWriter writer = new StreamWriter(sfd.FileName);
            writer.Write(textBox.Text);
            writer.Close();
        }
    }

    private void MenuItemSave()
    {
        if (FileName == "")
        {
            MenuItemSaveAs();
        }
        else
        {
            StreamWriter writer = new StreamWriter(FileName);
            writer.Write(textBox.Text);
            writer.Close();
        }
    }

    private void MenuItemOpen()
    {
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Filter = "Text Document|*.txt";
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            FileName = ofd.FileName;
            StreamReader reader = new StreamReader(ofd.FileName);
            textBox.Text = reader.ReadToEnd();
            reader.Close();
        }
    }

    private void MenuItemNew()
    {
        if (textBox.Text == "")
        {
            textBox.Text = String.Empty;
        }
        else
            this.Show();
            Newform sistema=new Newform();
        sistema.ShowDialog();
    }
}

And a Newform like this:

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

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

    private void dontsave_click(object sender, EventArgs e)
    {

    }

    private void save_Click(object sender, EventArgs e)
    {
        this.Hide();
    }
}

When save is clicked in my new form, I need MenuItemSaveAs to open in my main form.

Upvotes: 2

Views: 208

Answers (2)

Melody, the problem really is in the design. A form should not call GUI elements in another form. This can lead to "spaghetti" code that is extremely hard to maintain. If it's the main form you're trying to manipulate in this way, you can rely on it being around, but it's still a violation of best practices. If you were to try to manipulate other forms this way, you would have to deal with the possibility the form does not exist (hasn't been created yet, or has been disposed).

If code needs to manipulate the GUI of a form, said code should be within that form.

Upvotes: 1

Hogan
Hogan

Reputation: 70513

first

 public void MenuItemSaveAs()

then

private void save_Click(object sender, EventArgs e)
{
    this.Hide();   
    ((MainForm)Parent).MenuItemSaveAs()
}

This assumes that your form is a child of MainForm. If it isn't then you will need to pass a reference to MainForm into the constructor of the form and save it locally. (There is also a way to always find the main form of the current program but I don't think this is an elegant way to solve the problem.)

Upvotes: 2

Related Questions