'How to store and display data from one window form to other windows form
I am new to the windows form and i am doing some exercise. I have two forms Main form and Summary form. On main form is where i type data and on the summary form will display data that i enter. The problem is i want to display current entered and previous data. Is there anyway to do it. I am new so can you show me the basic way not using data grid view or database or external file? Thank you very much. This is my MainForm.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Asignment8
{
public partial class MainForm : Form
{
Summary summary;
public MainForm()
{
InitializeComponent();
}
private void SubmitButton_Click(object sender, EventArgs e)
{
string data = FlightIdTextBox.Text + ";" + OriginTextBox.Text + ";" + DestinationTextBox.Text + ";" + DateBox.Text + ";" + TimeTextBox.Text;
if (summary == null || summary.IsDisposed)
{
summary = new Summary();
}
summary.AddData(data);
summary.Show();
}
}
}
This is my MainForm.Designer.cs:
namespace Asignment8
{
partial class MainForm
{
/// <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.flightId = new System.Windows.Forms.Label();
this.origin = new System.Windows.Forms.Label();
this.destination = new System.Windows.Forms.Label();
this.date = new System.Windows.Forms.Label();
this.time = new System.Windows.Forms.Label();
this.submitButton = new System.Windows.Forms.Button();
this.DateBox = new System.Windows.Forms.DateTimePicker();
this.FlightIdTextBox = new System.Windows.Forms.TextBox();
this.OriginTextBox = new System.Windows.Forms.TextBox();
this.DestinationTextBox = new System.Windows.Forms.TextBox();
this.TimeTextBox = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// flightId
//
this.flightId.AutoSize = true;
this.flightId.Font = new System.Drawing.Font("Microsoft Sans Serif", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.flightId.Location = new System.Drawing.Point(122, 78);
this.flightId.Name = "flightId";
this.flightId.Size = new System.Drawing.Size(86, 25);
this.flightId.TabIndex = 0;
this.flightId.Text = "Flight Id:";
//
// origin
//
this.origin.AutoSize = true;
this.origin.Font = new System.Drawing.Font("Microsoft Sans Serif", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.origin.Location = new System.Drawing.Point(122, 119);
this.origin.Name = "origin";
this.origin.Size = new System.Drawing.Size(70, 25);
this.origin.TabIndex = 1;
this.origin.Text = "Origin:";
//
// destination
//
this.destination.AutoSize = true;
this.destination.Font = new System.Drawing.Font("Microsoft Sans Serif", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.destination.Location = new System.Drawing.Point(122, 158);
this.destination.Name = "destination";
this.destination.Size = new System.Drawing.Size(115, 25);
this.destination.TabIndex = 2;
this.destination.Text = "Destination:";
//
// date
//
this.date.AutoSize = true;
this.date.Font = new System.Drawing.Font("Microsoft Sans Serif", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.date.Location = new System.Drawing.Point(122, 198);
this.date.Name = "date";
this.date.Size = new System.Drawing.Size(59, 25);
this.date.TabIndex = 3;
this.date.Text = "Date:";
//
// time
//
this.time.AutoSize = true;
this.time.Font = new System.Drawing.Font("Microsoft Sans Serif", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.time.Location = new System.Drawing.Point(122, 235);
this.time.Name = "time";
this.time.Size = new System.Drawing.Size(62, 25);
this.time.TabIndex = 4;
this.time.Text = "Time:";
//
// submitButton
//
this.submitButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.submitButton.Location = new System.Drawing.Point(357, 300);
this.submitButton.Name = "submitButton";
this.submitButton.Size = new System.Drawing.Size(79, 30);
this.submitButton.TabIndex = 5;
this.submitButton.Text = "Submit";
this.submitButton.UseVisualStyleBackColor = true;
this.submitButton.Click += new System.EventHandler(this.SubmitButton_Click);
//
// DateBox
//
this.DateBox.CalendarFont = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.DateBox.Location = new System.Drawing.Point(304, 203);
this.DateBox.Name = "DateBox";
this.DateBox.Size = new System.Drawing.Size(205, 20);
this.DateBox.TabIndex = 6;
//
// FlightIdTextBox
//
this.FlightIdTextBox.Location = new System.Drawing.Point(304, 83);
this.FlightIdTextBox.Name = "FlightIdTextBox";
this.FlightIdTextBox.Size = new System.Drawing.Size(205, 20);
this.FlightIdTextBox.TabIndex = 7;
//
// OriginTextBox
//
this.OriginTextBox.Location = new System.Drawing.Point(304, 124);
this.OriginTextBox.Name = "OriginTextBox";
this.OriginTextBox.Size = new System.Drawing.Size(205, 20);
this.OriginTextBox.TabIndex = 8;
//
// DestinationTextBox
//
this.DestinationTextBox.Location = new System.Drawing.Point(304, 163);
this.DestinationTextBox.Name = "DestinationTextBox";
this.DestinationTextBox.Size = new System.Drawing.Size(205, 20);
this.DestinationTextBox.TabIndex = 9;
//
// TimeTextBox
//
this.TimeTextBox.Location = new System.Drawing.Point(304, 240);
this.TimeTextBox.Name = "TimeTextBox";
this.TimeTextBox.Size = new System.Drawing.Size(205, 20);
this.TimeTextBox.TabIndex = 10;
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.TimeTextBox);
this.Controls.Add(this.DestinationTextBox);
this.Controls.Add(this.OriginTextBox);
this.Controls.Add(this.FlightIdTextBox);
this.Controls.Add(this.DateBox);
this.Controls.Add(this.submitButton);
this.Controls.Add(this.time);
this.Controls.Add(this.date);
this.Controls.Add(this.destination);
this.Controls.Add(this.origin);
this.Controls.Add(this.flightId);
this.Name = "MainForm";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label flightId;
private System.Windows.Forms.Label origin;
private System.Windows.Forms.Label destination;
private System.Windows.Forms.Label date;
private System.Windows.Forms.Label time;
private System.Windows.Forms.Button submitButton;
private System.Windows.Forms.DateTimePicker DateBox;
private System.Windows.Forms.TextBox FlightIdTextBox;
private System.Windows.Forms.TextBox OriginTextBox;
private System.Windows.Forms.TextBox DestinationTextBox;
private System.Windows.Forms.TextBox TimeTextBox;
}
}
This is my Summary.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Asignment8
{
public partial class Summary : Form
{
StringBuilder allData = new StringBuilder();
public Summary()
{
InitializeComponent();
}
public void AddData(string data)
{
allData.Append(data);
label1.Text = allData.ToString();
}
}
}
This is my Summary.Designer.cs:
namespace Asignment8
{
partial class Summary
{
/// <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.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(237, 135);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(0, 16);
this.label1.TabIndex = 0;
//
// Summary
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1067, 554);
this.Controls.Add(this.label1);
this.Margin = new System.Windows.Forms.Padding(4);
this.Name = "Summary";
this.Text = "Summary";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
