I have a main form which is going to use much input data typed in by the user. So I thought to make a separate form to handle the input. The closest to a solution was this thread http://www.daniweb.com/forums/thread231368.html
But I don't want the second form to be disposed off, or be closed by clicking the close icon. I like to store the input data in the second form(seems logical to me) and let the main form use them.
I don't want the input form to be visible all of the time. Just want to let it pop up when the user needs it(via menu or button, don't know yet)
This is my code so far a main form with a button and a click event and an inputdata form:

public partial class Form1 : Form
    {
        private InputData InputForm = new InputData();

        public Form1()
        {
            InitializeComponent();            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            InputForm.Show();
        }
    }

Not very much, I know. But what would be the most elegant way to handle this?
As always, any help is greatly appreciated.:)

Recommended Answers

All 4 Replies

Hi danny.
You have to handle formclosing event along with singleton - single instance of Form.

commented: Thanks! :) +5

I would treat the settings form more like a static class for this since thats more or less how you want to deal with it.

Main form:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb.twoforms
{
  public partial class frmMain : Form
  {
    public frmMain()
    {
      InitializeComponent();
    }

    private void buttonGetValues_Click(object sender, EventArgs e)
    {
      StringBuilder sb = new StringBuilder();
      sb.AppendLine("Value1: " + frmSettings.Value1);
      sb.AppendLine("Value2: " + frmSettings.Value2);
      sb.AppendLine("Value3: " + frmSettings.Value3);
      MessageBox.Show(sb.ToString());
    }

    private void buttonShowForm_Click(object sender, EventArgs e)
    {
      frmSettings.ShowForm();
    }
  }
}

Settings:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb.twoforms
{
  public partial class frmSettings : Form
  {
    static frmSettings instance;

    static frmSettings()
    {
      instance = new frmSettings();
    }

    public static string Value1
    {
      get { return instance.textBox1.Text; }
      set { instance.textBox1.Text = value; }
    }

    public static string Value2
    {
      get { return instance.textBox2.Text; }
      set { instance.textBox2.Text = value; }
    }

    public static string Value3
    {
      get { return instance.textBox3.Text; }
      set { instance.textBox3.Text = value; }
    }

    public static void ShowForm()
    {
      if (instance.Visible)
        instance.BringToFront();
      else
        instance.Show();
    }

    public frmSettings()
    {
      InitializeComponent();

      //This smokes out a bug in code if you accidently create 2 instances
      if (instance != null)
        throw new InvalidOperationException("Only one instance allowed");

      this.textBox1.Text = "default val 1";
      this.textBox2.Text = "default val 2";
      this.textBox3.Text = "default val 3";
    }

    private void frmSettings_FormClosing(object sender, FormClosingEventArgs e)
    {
      e.Cancel = true;
      this.Hide();
    }
  }
}
commented: could not do this yesterday! This code really helped me out! +5

Thanks guys, these suggestions are very helpfull.

I ha an MDI parent Form which I want it to be in the to of othe form when I click a button and when I click the button back the form should go to center, I create this code :

if (TopMost == false)
            {
                 //Dis play the form to the top
                this.TopMost = true;
            }
            else
            {
                // Display the form the ceter.
                TopMost = false;
            }

it not working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.