防止再打开第一个表格c#

时间:2017-08-23 04:44:02

标签: c# forms button

当我运行桌面应用程序时,首先打开form1,当单击数据网格view1的一行时,将打开form2的一个实例.form2有几个文本框,当我填充它们并单击按钮时,这些值将被显示在form1的数据网格视图2中!

以下给出了form1的代码

namespace Framework.TestCases
{
    public class SomePageTestCases
    {
        public bool TestCaseMethod1()
        {
            return (CurrentPage.Instance.Page as SomePage)?.DoSomething() ?? false;
        }
    }
}

namespace Framework
{
    public class CurrentPage
    {
        private object TopLevelControl;

        private static CurrentPage _instance;
        public static CurrentPage Instance => _instance ?? (_instance = new CurrentPage());

        private Page _page;

        public Page Page
        {
            get
            {
                if (_page == null || _page.Name != GetCurrentPageName())
                    _page = GetCurrentPage();
                return _page;
            }
        }

        private CurrentPage()
        {
            //hooks the top level control of the system under test.
            throw new NotImplementedException();
        }

        private static string GetCurrentPageName()
        {
            //return the page name of the current page from the TopLevelControl.
            throw new NotImplementedException();
        }

        private static Page GetCurrentPage()
        {
            //Searches the TopLevelControl for the current PageObject and returns it.
            throw new NotImplementedException();
        }
    }
}

namespace Framework.Base
{
    public abstract class Page
    {
        public string Name { get; private set; }
        public object PageObject { get; private set; }

        protected void LoadPage(string pageClassName)
        {
            throw new NotImplementedException();
            //Name = something;
            //PageObject = something;
        }
    }
}


namespace Framework.Pages
{
    public class SomePage : Page
    {
        public bool DoSomething()
        {
            throw new NotImplementedException();
        }

        public SomePage()
        {
            LoadPage("SomePage");
        }
    }
}

以下是form2的代码

public partial class Form1 : Form
{


    string dbconnection;

    public Form1()
    {
        InitializeComponent();

        dbconnection = @" Data Source = D:\SQLite\SQLiteStudio\DB1.db ; version=3 ";

    }
    private static Form1 f1;

    public static Form1 getInstance(){
        if (f1 == null)
        {
            f1 = new Form1();
        }
        else
            f1.BringToFront();
            return f1;

    }

    private void loadButton_Click(object sender, EventArgs e)
    {
                {
        using (SQLiteConnection con = new SQLiteConnection(dbconnection))
        {
            try
            {
                con.Open();

                if (con.State == ConnectionState.Open)
                {
                    string sql = "select MESSAGE_ID from Message";
                    SQLiteCommand command = new SQLiteCommand(sql, con);
                    SQLiteDataReader reader = command.ExecuteReader();
                    //dataGridView1.ColumnCount = 1;
                   // dataGridView1.Columns[0].Name = "Message ID";
                    DataTable ds = new DataTable();
                    ds.Load(reader);
                   dataGridView1.AutoGenerateColumns = true;
                    dataGridView1.DataSource = ds;
                    dataGridView1.Refresh();

                }
            }
            catch (Exception m)
            {
                MessageBox.Show(m.Message);
            }

        }
    }



    }

    public void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
         DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
        Form2 f2 = new Form2();

        f2.IDtext.Text = row.Cells["MESSAGE_ID"].Value.ToString();
        f2.ShowDialog();

    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

        }    
}
    }

我在form1中编写了一个名为getinstance()的方法来实现这个目的,然后打开一个新窗口。 我希望值出现在我最初加载的form1中,但数据显示在form1的新实例中。如何在我加载的初始form1中显示数据

1 个答案:

答案 0 :(得分:0)

在你的代码中将construcor更改为getinstance:

private static Form1 f1;

public Form1()
{
    InitializeComponent();
    f1 = this;
    dbconnection = @" Data Source = D:\SQLite\SQLiteStudio\DB1.db ; version=3 ";

}

public static Form1 getInstance(){
        f1.BringToFront();
        return f1;
}

注意:这不是最优雅的代码:您可以省略静态内容,只需将Form1的实例注入Form2的构造函数中,如下所示:

   ...
   Form2 f2 = new Form2(this);
   ...

并在表格2中:

   Form1 f1x;

   Public Form2(Form1 parentForm)
   {
       InitializeComponents;
       f1x = form
   }

并省略

 Form1 f1x = Form1.getInstance();