Write a program for sorting an array. Declare a single dimensional array and accept 5 integer values from the user. Then sort the input in ascending order and display the output.
In C# Windows form
public partial class MainForm : Form
{
int[] Numbers = new int[5];
int currentNumber = 0;
public MainForm()
{
InitializeComponent();
}
private void AddButton_Click(object sender, EventArgs e)
{
if(currentNumber ==4)
{
AddButton.Enabled = false;
}
Numbers[currentNumber] = int.Parse(EnterTextBox.Text);
currentNumber++;
UserNumberTextBox.Text += EnterTextBox.Text;
UserNumberTextBox.Text += " ";
EnterTextBox.Clear();
}
private void SortButton_Click(object sender, EventArgs e)
{
SortValueTextBox.Clear();
IEnumerable<int> sortNumbers = Numbers.OrderBy(x => x);
foreach (var number in sortNumbers)
{
SortValueTextBox.Text += number;
SortValueTextBox.Text += " ";
}
}
private void ClearButton_Click(object sender, EventArgs e)
{
currentNumber = 0;
UserNumberTextBox.Clear();
AddButton.Enabled = true;
}
}
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.EnterTextBox = new System.Windows.Forms.TextBox();
this.UserNumberTextBox = new System.Windows.Forms.TextBox();
this.AddButton = new System.Windows.Forms.Button();
this.SortButton = new System.Windows.Forms.Button();
this.SortValueTextBox = new System.Windows.Forms.TextBox();
this.EnterNumberLabel = new System.Windows.Forms.Label();
this.SortedNumbersLabel = new System.Windows.Forms.Label();
this.UserNumberLabel = new System.Windows.Forms.Label();
this.ClearButton = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// EnterTextBox
//
this.EnterTextBox.Location = new System.Drawing.Point(12, 78);
this.EnterTextBox.Name = "EnterTextBox";
this.EnterTextBox.Size = new System.Drawing.Size(184, 20);
this.EnterTextBox.TabIndex = 0;
//
// UserNumberTextBox
//
this.UserNumberTextBox.Location = new System.Drawing.Point(12, 28);
this.UserNumberTextBox.Name = "UserNumberTextBox";
this.UserNumberTextBox.ReadOnly = true;
this.UserNumberTextBox.Size = new System.Drawing.Size(337, 20);
this.UserNumberTextBox.TabIndex = 1;
//
// AddButton
//
this.AddButton.Location = new System.Drawing.Point(12, 115);
this.AddButton.Name = "AddButton";
this.AddButton.Size = new System.Drawing.Size(89, 23);
this.AddButton.TabIndex = 2;
this.AddButton.Text = "AddNumber";
this.AddButton.UseVisualStyleBackColor = true;
this.AddButton.Click += new System.EventHandler(this.AddButton_Click);
//
// SortButton
//
this.SortButton.Location = new System.Drawing.Point(251, 115);
this.SortButton.Name = "SortButton";
this.SortButton.Size = new System.Drawing.Size(75, 23);
this.SortButton.TabIndex = 3;
this.SortButton.Text = "Sort";
this.SortButton.UseVisualStyleBackColor = true;
this.SortButton.Click += new System.EventHandler(this.SortButton_Click);
//
// SortValueTextBox
//
this.SortValueTextBox.Location = new System.Drawing.Point(220, 78);
this.SortValueTextBox.Name = "SortValueTextBox";
this.SortValueTextBox.ReadOnly = true;
this.SortValueTextBox.Size = new System.Drawing.Size(129, 20);
this.SortValueTextBox.TabIndex = 4;
//
// EnterNumberLabel
//
this.EnterNumberLabel.AutoSize = true;
this.EnterNumberLabel.Location = new System.Drawing.Point(13, 55);
this.EnterNumberLabel.Name = "EnterNumberLabel";
this.EnterNumberLabel.Size = new System.Drawing.Size(88, 13);
this.EnterNumberLabel.TabIndex = 5;
this.EnterNumberLabel.Text = "Enter the number";
//
// SortedNumbersLabel
//
this.SortedNumbersLabel.AutoSize = true;
this.SortedNumbersLabel.Location = new System.Drawing.Point(217, 55);
this.SortedNumbersLabel.Name = "SortedNumbersLabel";
this.SortedNumbersLabel.Size = new System.Drawing.Size(81, 13);
this.SortedNumbersLabel.TabIndex = 6;
this.SortedNumbersLabel.Text = "Sorted numbers";
//
// UserNumberLabel
//
this.UserNumberLabel.AutoSize = true;
this.UserNumberLabel.Location = new System.Drawing.Point(12, 12);
this.UserNumberLabel.Name = "UserNumberLabel";
this.UserNumberLabel.Size = new System.Drawing.Size(72, 13);
this.UserNumberLabel.TabIndex = 7;
this.UserNumberLabel.Text = "Your numbers";
//
// ClearButton
//
this.ClearButton.Location = new System.Drawing.Point(107, 115);
this.ClearButton.Name = "ClearButton";
this.ClearButton.Size = new System.Drawing.Size(89, 23);
this.ClearButton.TabIndex = 8;
this.ClearButton.Text = "Clear Array";
this.ClearButton.UseVisualStyleBackColor = true;
this.ClearButton.Click += new System.EventHandler(this.ClearButton_Click);
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(371, 166);
this.Controls.Add(this.ClearButton);
this.Controls.Add(this.UserNumberLabel);
this.Controls.Add(this.SortedNumbersLabel);
this.Controls.Add(this.EnterNumberLabel);
this.Controls.Add(this.SortValueTextBox);
this.Controls.Add(this.SortButton);
this.Controls.Add(this.AddButton);
this.Controls.Add(this.UserNumberTextBox);
this.Controls.Add(this.EnterTextBox);
this.Name = "MainForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Main";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox EnterTextBox;
private System.Windows.Forms.TextBox UserNumberTextBox;
private System.Windows.Forms.Button AddButton;
private System.Windows.Forms.Button SortButton;
private System.Windows.Forms.TextBox SortValueTextBox;
private System.Windows.Forms.Label EnterNumberLabel;
private System.Windows.Forms.Label SortedNumbersLabel;
private System.Windows.Forms.Label UserNumberLabel;
private System.Windows.Forms.Button ClearButton;
}
Comments
Leave a comment