破坏GUI元素

时间:2017-01-31 06:01:00

标签: c# winforms visual-studio visual-studio-2015

我是从python迁移的C#编程的新手。我制作了两个Windows窗体应用程序,当第一个菜单按钮被点击第一个W.F.A时,它通过菜单按钮连接到单个第三个应用程序。出现,当第二个按钮被点击时,第二个W.F.A.出现。 现在发生的事情是,当点击第一个按钮时,它会加载所需的窗体,但在此之后,当单击第二个按钮时,第二个窗口形式与第一个窗口重叠,而不会破坏第一个窗口。我需要的是,无论何时加载新的窗体,它都应该破坏以前的窗体。我希望,我清楚自己。我能做些什么来实现这个目标? 这是我的代码?

//something.Designer.cs
namespace Demo
{
 partial class Demo
 {
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    /// 

    private void InitializeComponent()
    {
        this.menuStrip1 = new System.Windows.Forms.MenuStrip();
        this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.enterDataToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.showDataToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
        this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.label9 = new System.Windows.Forms.Label();
        this.menuStrip1.SuspendLayout();
        this.SuspendLayout();
        // 
        // menuStrip1
        // 
        this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.fileToolStripMenuItem});
        this.menuStrip1.Location = new System.Drawing.Point(0, 0);
        this.menuStrip1.Name = "menuStrip1";
        this.menuStrip1.Size = new System.Drawing.Size(426, 24);
        this.menuStrip1.TabIndex = 0;
        this.menuStrip1.Text = "menuStrip1";
        // 
        // fileToolStripMenuItem
        // 
        this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.enterDataToolStripMenuItem,
        this.showDataToolStripMenuItem,
        this.toolStripSeparator1,
        this.exitToolStripMenuItem});
        this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
        this.fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20);
        this.fileToolStripMenuItem.Text = "File";
        // 
        // enterDataToolStripMenuItem
        // 
        this.enterDataToolStripMenuItem.Name = "enterDataToolStripMenuItem";
        this.enterDataToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
        this.enterDataToolStripMenuItem.Text = "Enter Data";
        this.enterDataToolStripMenuItem.Click += new System.EventHandler(this.enterDataToolStripMenuItem_Click);
        // 
        // showDataToolStripMenuItem
        // 
        this.showDataToolStripMenuItem.Name = "showDataToolStripMenuItem";
        this.showDataToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
        this.showDataToolStripMenuItem.Text = "Show Data";
        this.showDataToolStripMenuItem.Click += new System.EventHandler(this.showDataToolStripMenuItem_Click);
        // 
        // toolStripSeparator1
        // 
        this.toolStripSeparator1.Name = "toolStripSeparator1";
        this.toolStripSeparator1.Size = new System.Drawing.Size(149, 6);
        // 
        // exitToolStripMenuItem
        // 
        this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
        this.exitToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
        this.exitToolStripMenuItem.Text = "Exit";
        this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click);
        // 
        // label9
        // 
        this.label9.AutoSize = true;
        this.label9.Font = new System.Drawing.Font("Microsoft Sans Serif", 72F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.label9.Location = new System.Drawing.Point(131, 118);
        this.label9.Name = "label9";
        this.label9.Size = new System.Drawing.Size(170, 108);
        this.label9.TabIndex = 1;
        this.label9.Text = "ZK";
        // 
        // Demo
        // 
        this.BackColor = System.Drawing.SystemColors.HotTrack;
        this.ClientSize = new System.Drawing.Size(426, 324);
        this.Controls.Add(this.label9);
        this.Controls.Add(this.menuStrip1);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
        this.MainMenuStrip = this.menuStrip1;
        this.Name = "Demo";
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.Text = "ZK";
        this.menuStrip1.ResumeLayout(false);
        this.menuStrip1.PerformLayout();
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    private void showData()
    {
        this.label9.Text = "";
        this.label8 = new System.Windows.Forms.Label();
        this.studentData = new System.Windows.Forms.DataGridView();
        this.studentID = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.gNo = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.studName = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.fathName = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.mobNum = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.dOBirth = new System.Windows.Forms.DataGridViewTextBoxColumn();
        ((System.ComponentModel.ISupportInitialize)(this.studentData)).BeginInit();
        this.SuspendLayout();
        // 
        // label8
        // 
        this.label8.AutoSize = true;
        this.label8.Font = new System.Drawing.Font("Microsoft Sans Serif", 48F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.label8.Location = new System.Drawing.Point(267, 35);
        this.label8.Name = "label8";
        this.label8.Size = new System.Drawing.Size(116, 73);
        this.label8.TabIndex = 0;
        this.label8.Text = "ZK";
        this.label8.TextAlign = System.Drawing.ContentAlignment.TopCenter;
        // 
        // studentData
        // 
        this.studentData.AllowUserToAddRows = false;
        this.studentData.BackgroundColor = System.Drawing.SystemColors.Highlight;
        this.studentData.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.studentData.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.Raised;
        this.studentData.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.studentData.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        this.studentID,
        this.gNo,
        this.studName,
        this.fathName,
        this.mobNum,
        this.dOBirth});
        this.studentData.EditMode = System.Windows.Forms.DataGridViewEditMode.EditProgrammatically;
        this.studentData.Location = new System.Drawing.Point(12, 144);
        this.studentData.Name = "studentData";
        this.studentData.ReadOnly = true;
        this.studentData.RowHeadersVisible = false;
        this.studentData.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
        this.studentData.Size = new System.Drawing.Size(618, 365);
        this.studentData.TabIndex = 1;
        // 
        // studentID
        // 
        this.studentID.HeaderText = "Student ID";
        this.studentID.Name = "studentID";
        this.studentID.ReadOnly = true;
        // 
        // gNo
        // 
        this.gNo.HeaderText = "Gr. No.";
        this.gNo.Name = "gNo";
        this.gNo.ReadOnly = true;
        // 
        // studName
        // 
        this.studName.HeaderText = "Student Name";
        this.studName.Name = "studName";
        this.studName.ReadOnly = true;
        // 
        // fathName
        // 
        this.fathName.HeaderText = "Father Name";
        this.fathName.Name = "fathName";
        this.fathName.ReadOnly = true;
        // 
        // mobNum
        // 
        this.mobNum.HeaderText = "Mobile No";
        this.mobNum.Name = "mobNum";
        this.mobNum.ReadOnly = true;
        // 
        // dOBirth
        // 
        this.dOBirth.HeaderText = "Date Of Birth";
        this.dOBirth.Name = "dOBirth";
        this.dOBirth.ReadOnly = true;
        // 
        // Demo
        // 
        this.BackColor = System.Drawing.SystemColors.HotTrack;
        this.ClientSize = new System.Drawing.Size(642, 521);
        this.Controls.Add(this.studentData);
        this.Controls.Add(this.label8);
        this.ForeColor = System.Drawing.SystemColors.Desktop;
        this.Name = "Demo";
        this.ShowIcon = false;
        this.Text = "ZK";
        this.Load += new System.EventHandler(this.fillTable);
        ((System.ComponentModel.ISupportInitialize)(this.studentData)).EndInit();
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    private void EnterData()
    {
        this.label9.Text = "";
        this.label1 = new System.Windows.Forms.Label();
        this.label2 = new System.Windows.Forms.Label();
        this.label3 = new System.Windows.Forms.Label();
        this.label4 = new System.Windows.Forms.Label();
        this.label5 = new System.Windows.Forms.Label();
        this.label6 = new System.Windows.Forms.Label();
        this.label7 = new System.Windows.Forms.Label();
        this.grNo = new System.Windows.Forms.TextBox();
        this.studentName = new System.Windows.Forms.TextBox();
        this.fatherName = new System.Windows.Forms.TextBox();
        this.mobNo = new System.Windows.Forms.TextBox();
        this.dob = new System.Windows.Forms.DateTimePicker();
        this.saveBtn = new System.Windows.Forms.Button();
        this.status = new System.Windows.Forms.Label();
        this.SuspendLayout();
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 48F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.label1.Location = new System.Drawing.Point(234, 24);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(116, 73);
        this.label1.TabIndex = 0;
        this.label1.Text = "ZK";
        this.label1.TextAlign = System.Drawing.ContentAlignment.TopCenter;
        // 
        // label2
        // 
        this.label2.AutoSize = true;
        this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.label2.Location = new System.Drawing.Point(106, 195);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(70, 24);
        this.label2.TabIndex = 2;
        this.label2.Text = "Gr. No.";
        // 
        // label3
        // 
        this.label3.AutoSize = true;
        this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.label3.Location = new System.Drawing.Point(225, 111);
        this.label3.Name = "label3";
        this.label3.Size = new System.Drawing.Size(125, 29);
        this.label3.TabIndex = 3;
        this.label3.Text = "Enter Data";
        // 
        // label4
        // 
        this.label4.AutoSize = true;
        this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.label4.Location = new System.Drawing.Point(106, 358);
        this.label4.Name = "label4";
        this.label4.Size = new System.Drawing.Size(110, 24);
        this.label4.TabIndex = 4;
        this.label4.Text = "Date of Birth";
        // 
        // label5
        // 
        this.label5.AutoSize = true;
        this.label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.label5.Location = new System.Drawing.Point(106, 317);
        this.label5.Name = "label5";
        this.label5.Size = new System.Drawing.Size(102, 24);
        this.label5.TabIndex = 5;
        this.label5.Text = "Mobile No.";
        // 
        // label6
        // 
        this.label6.AutoSize = true;
        this.label6.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.label6.Location = new System.Drawing.Point(106, 279);
        this.label6.Name = "label6";
        this.label6.Size = new System.Drawing.Size(133, 24);
        this.label6.TabIndex = 6;
        this.label6.Text = "Father\'s Name";
        // 
        // label7
        // 
        this.label7.AutoSize = true;
        this.label7.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.label7.Location = new System.Drawing.Point(106, 236);
        this.label7.Name = "label7";
        this.label7.Size = new System.Drawing.Size(143, 24);
        this.label7.TabIndex = 7;
        this.label7.Text = "Student\'s Name";
        // 
        // grNo
        // 
        this.grNo.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.grNo.Location = new System.Drawing.Point(274, 195);
        this.grNo.Name = "grNo";
        this.grNo.Size = new System.Drawing.Size(236, 24);
        this.grNo.TabIndex = 8;
        // 
        // studentName
        // 
        this.studentName.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.studentName.Location = new System.Drawing.Point(274, 236);
        this.studentName.Name = "studentName";
        this.studentName.Size = new System.Drawing.Size(236, 24);
        this.studentName.TabIndex = 9;
        // 
        // fatherName
        // 
        this.fatherName.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.fatherName.Location = new System.Drawing.Point(274, 279);
        this.fatherName.Name = "fatherName";
        this.fatherName.Size = new System.Drawing.Size(236, 24);
        this.fatherName.TabIndex = 10;
        // 
        // mobNo
        // 
        this.mobNo.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.mobNo.Location = new System.Drawing.Point(274, 317);
        this.mobNo.Name = "mobNo";
        this.mobNo.Size = new System.Drawing.Size(236, 24);
        this.mobNo.TabIndex = 11;
        // 
        // dob
        // 
        this.dob.CalendarFont = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.dob.Location = new System.Drawing.Point(274, 361);
        this.dob.Name = "dob";
        this.dob.Size = new System.Drawing.Size(236, 20);
        this.dob.TabIndex = 12;
        // 
        // saveBtn
        // 
        this.saveBtn.FlatAppearance.BorderColor = System.Drawing.SystemColors.ActiveCaptionText;
        this.saveBtn.FlatAppearance.BorderSize = 5;
        this.saveBtn.FlatAppearance.MouseDownBackColor = System.Drawing.SystemColors.ButtonHighlight;
        this.saveBtn.FlatAppearance.MouseOverBackColor = System.Drawing.SystemColors.Desktop;
        this.saveBtn.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.saveBtn.Location = new System.Drawing.Point(413, 399);
        this.saveBtn.Name = "saveBtn";
        this.saveBtn.Size = new System.Drawing.Size(97, 36);
        this.saveBtn.TabIndex = 13;
        this.saveBtn.Text = "Save";
        this.saveBtn.UseVisualStyleBackColor = true;
        this.saveBtn.Click += new System.EventHandler(this.saveBtn_Click);
        // 
        // status
        // 
        this.status.AutoSize = true;
        this.status.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.status.Location = new System.Drawing.Point(11, 153);
        this.status.Name = "status";
        this.status.Size = new System.Drawing.Size(165, 20);
        this.status.TabIndex = 14;
        this.status.Text = "                                       ";
        // 
        // Demo
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.AutoSize = true;
        this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
        this.BackColor = System.Drawing.SystemColors.HotTrack;
        this.ClientSize = new System.Drawing.Size(620, 463);
        this.Controls.Add(this.status);
        this.Controls.Add(this.saveBtn);
        this.Controls.Add(this.dob);
        this.Controls.Add(this.mobNo);
        this.Controls.Add(this.fatherName);
        this.Controls.Add(this.studentName);
        this.Controls.Add(this.grNo);
        this.Controls.Add(this.label7);
        this.Controls.Add(this.label6);
        this.Controls.Add(this.label5);
        this.Controls.Add(this.label4);
        this.Controls.Add(this.label3);
        this.Controls.Add(this.label2);
        this.Controls.Add(this.label1);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
        this.Name = "Demo";
        this.Text = "ZK";
        //this.Load += new System.EventHandler(this.Demo_Load);
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.Label label4;
    private System.Windows.Forms.Label label5;
    private System.Windows.Forms.Label label6;
    private System.Windows.Forms.Label label7;
    private System.Windows.Forms.TextBox grNo;
    private System.Windows.Forms.TextBox studentName;
    private System.Windows.Forms.TextBox fatherName;
    private System.Windows.Forms.TextBox mobNo;
    private System.Windows.Forms.DateTimePicker dob;
    private System.Windows.Forms.Button saveBtn;
    private System.Windows.Forms.Label status;
    private System.Windows.Forms.Label label8;
    private System.Windows.Forms.DataGridView studentData;
    private System.Windows.Forms.DataGridViewTextBoxColumn studentID;
    private System.Windows.Forms.DataGridViewTextBoxColumn gNo;
    private System.Windows.Forms.DataGridViewTextBoxColumn studName;
    private System.Windows.Forms.DataGridViewTextBoxColumn fathName;
    private System.Windows.Forms.DataGridViewTextBoxColumn mobNum;
    private System.Windows.Forms.DataGridViewTextBoxColumn dOBirth;
    private System.Windows.Forms.MenuStrip menuStrip1;
    private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem enterDataToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem        showDataToolStripMenuItem;
    private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
    private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem;
    private System.Windows.Forms.Label label9;
}
}


 //something.cs
 using System;
 using System.Collections.Generic;
 using System.Collections.Specialized;
 using System.Windows.Forms;
 using System.Data.SqlClient;

namespace Demo
{
public partial class Demo : Form
{
    protected class Details
    {
        private List<NameValueCollection> detail = new List<NameValueCollection>();

        public void addDetails(string id,string gr,string sName,string fName,string mob,string dob)
        {
            NameValueCollection dict = new NameValueCollection();
            dict["id"] = id;
            dict["GrNo"] = gr;
            dict["sName"] = sName;
            dict["fName"] = fName;
            dict["mob"] = mob;
            dict["dob"] = dob;
            detail.Add(dict);
        }

        public List<NameValueCollection> getDetails()
        {
            return detail;
        }
    }

    public Demo()
    {
        InitializeComponent();
    }

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

    private void saveBtn_Click(object sender, EventArgs e)
    {
        if(grNo.Text != "")
        {
            if(studentName.Text != "")
            {
                if(fatherName.Text != "")
                {
                    if(mobNo.Text != "")
                    {
                        bool isDone = saveAction(grNo.Text, studentName.Text, fatherName.Text, mobNo.Text, dob.Value);
                        if(isDone)
                        {
                            MessageBox.Show("Record Successfully added!");
                        }
                    }
                }
            }
        }
    }

    private bool saveAction(string gr, string sName, string fName, string mob, DateTime dob)
    {
        string sqlDetails = "Data Source=DESKTOP-DBJ8T78\\sql;Initial Catalog=Demo;User ID=sa;Password=sample";
        SqlConnection dbHandle = new SqlConnection(sqlDetails);
        try
        {
            dbHandle.Open();
            string sql = "SELECT * FROM studentDetails WHERE GrNo='"+gr+"'";
            SqlCommand result = new SqlCommand(sql, dbHandle);
            SqlDataReader row = result.ExecuteReader();
            if(!row.Read())
            {
                row.Close();
                result.Dispose();
                sql = "INSERT INTO studentDetails (GrNo,StudentName,FatherName,MobileNo,DOB) VALUES ('" + gr + "','" + sName + "','" + fName + "','" + mob + "','" + dob + "')";
                result = new SqlCommand(sql, dbHandle);
                result.ExecuteNonQuery();
            }
            else
            {
                status.Text = "Gr. No. '" + gr + "' Already Exists!";
                row.Close();
                result.Dispose();
                dbHandle.Close();
                return false;
            }
            row.Close();
            result.Dispose();
            dbHandle.Close();
            return true;
        }
        catch(Exception e)
        {
            status.Text = "Connection to the Database Failed! Please Contact Administrator for further details. \n" + e;
            return false;
        }
    }

    private void fillTable(object sender,EventArgs ev)
    {
        string sqlDetails = "Data Source=DESKTOP-DBJ8T78\\sql;Initial Catalog=Demo;User ID=sa;Password=sample";
        SqlConnection dbHandle = new SqlConnection(sqlDetails);
        try
        {
            dbHandle.Open();
            string sql = "SELECT * FROM studentDetails";
            SqlCommand result = new SqlCommand(sql, dbHandle);
            SqlDataReader row = result.ExecuteReader();
            Details student = new Details();
            while(row.Read())
            {
                student.addDetails(row.GetValue(0).ToString(),row.GetValue(1).ToString(), row.GetValue(2).ToString(), row.GetValue(3).ToString(), row.GetValue(4).ToString(), row.GetValue(5).ToString());
            }
            List<NameValueCollection> sDetails = student.getDetails();
            foreach(NameValueCollection data in sDetails)
            {
                this.studentData.Rows.Add(data["id"], data["GrNo"], data["sName"], data["fName"], data["mob"], data["dob"]);
            }
            row.Close();
            result.Dispose();
            dbHandle.Close(); 
        }
        catch(Exception e)
        {
            MessageBox.Show("Some Error Occured! Please Contact Administrator.\n" + e);
        }
    }

    private void enterDataToolStripMenuItem_Click(object sender, EventArgs e)
    {
        this.EnterData();
    }

    private void showDataToolStripMenuItem_Click(object sender, EventArgs e)
    {
        this.showData();
    }
}
}

2 个答案:

答案 0 :(得分:0)

我明白你要说的是什么 每个表格在收盘后都有一个返回值。当您调用MessageBox.Show()然后按“确定”按钮关闭它时,您可以检查返回值(DialogResult)并执行一些操作:

if (MessageBox.Show("done") == DialogResult.OK)
    this.Close();

答案 1 :(得分:0)

看来你所做的是创建其他winforms作为原始Form1的一部分。您应该做的是为EnterData和SaveData创建 new 表单。

右键单击项目名称,然后选择&#34;添加&#34; - &GT; &#34;新项目......&#34;从弹出菜单中。这将带您进入“添加新项”对话框。选择&#34; Winfows表格&#34;。在名称框中,将其命名为EnterForm或SaveForm,无论您正在执行哪个操作。然后,每个表单的初始化将在他们自己的类中进行。

然后,当单击工具栏时,您将调用单个表单:

enterForm = new EnterForm();
if (saveForm != null)
{
    saveForm.Close();
    saveForm = null;
}

enterForm.Show();

这只是一个基本的失败。我建议您通过一些基本的Winforms培训来熟悉范例。