Advertisement
Koragg

Random Numbers Generator [C# {Windows Forms}]

Nov 13th, 2018
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 26.87 KB | None | 0 0
  1. //https://stackoverflow.com/questions/25700992/do-not-allow-enter-key-to-make-a-new-line-in-multiline-textbox-c-sharp --> Disable ENTER in multiline textboxes.
  2. //https://stackoverflow.com/questions/463299/how-do-i-make-a-textbox-that-only-accepts-numbers --> Make textboxes accept only numbers.
  3. //https://social.msdn.microsoft.com/Forums/en-US/da1b8805-2a9f-4bbc-a7c2-aa5e3c829d39/getfocus-event-for-textbox-in-cnet?forum=csharplanguage --> Get focus of textbox.
  4. //https://docs.microsoft.com/en-us/dotnet/api/system.random.next?view=netframework-4.7.2#System_Random_Next_System_Int32_System_Int32_ --> Get random numbers in range.
  5. //https://social.msdn.microsoft.com/Forums/en-US/d4175788-7bfc-4f6d-8eb6-e48b80000fe1/how-to-detect-if-checkbox-was-checked?forum=csharpgeneral --> Get checkbox state.
  6. //https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.combobox.selectedindexchanged?view=netframework-4.7.2 --> Event for combobox selection changed.
  7. //https://social.msdn.microsoft.com/Forums/vstudio/en-US/f4cf2450-4485-41af-9403-e2fc30e126d5/split-string-to-int?forum=csharpgeneral --> Split String to Int array.
  8. //http://www.csharp-examples.net/sort-array/ --> Sort array in ascending order.
  9. //https://stackoverflow.com/questions/5430016/better-way-to-sort-array-in-descending-order --> Sort array in descending order.
  10. //https://stackoverflow.com/questions/14297853/how-to-get-random-values-from-array-in-c-sharp/14297876#14297876 --> Shuffle array elements.
  11. //https://stackoverflow.com/questions/19011948/how-to-add-scrollbars-in-c-sharp-form --> Add scrollbars to textbox.
  12. //https://social.msdn.microsoft.com/Forums/windows/en-US/1bbfc2b0-f099-4572-be70-e70a27ec2eed/how-to-set-a-default-value-for-a-combo-box?forum=winforms --> Set default selection of combobox.
  13. //https://stackoverflow.com/questions/5104175/how-do-you-change-the-text-in-the-titlebar-in-windows-forms --> Set Form name at top left.
  14. //https://stackoverflow.com/questions/10223477/change-default-icon --> Set Form icon at top left and taskbar.
  15. //https://stackoverflow.com/questions/4284497/changing-the-default-icon-in-a-windows-forms-application --> Set app .exe file icon.
  16.  
  17. using System;
  18. using System.Data;
  19. using System.Linq;
  20. using System.Windows.Forms;
  21.  
  22. namespace RandomNumbersGenerator
  23. {
  24.     public partial class Form1 : Form
  25.     {
  26.         /// <summary>
  27.         /// Required designer variable.
  28.         /// </summary>
  29.         private System.ComponentModel.IContainer components = null;
  30.  
  31.         /// <summary>
  32.         /// Clean up any resources being used.
  33.         /// </summary>
  34.         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  35.         protected override void Dispose(bool disposing)
  36.         {
  37.             if (disposing && (components != null))
  38.             {
  39.                 components.Dispose();
  40.             }
  41.             base.Dispose(disposing);
  42.         }
  43.  
  44.         #region Windows Form Designer generated code
  45.  
  46.         /// <summary>
  47.         /// Required method for Designer support - do not modify
  48.         /// the contents of this method with the code editor.
  49.         /// </summary>
  50.         private void InitializeComponent()
  51.         {
  52.             System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
  53.             this.numbersAmountLabel = new System.Windows.Forms.Label();
  54.             this.numbersAmountInputBox = new System.Windows.Forms.TextBox();
  55.             this.rangeFromLabel = new System.Windows.Forms.Label();
  56.             this.rangeFromInputBox = new System.Windows.Forms.TextBox();
  57.             this.rangeToLabel = new System.Windows.Forms.Label();
  58.             this.rangeToInputBox = new System.Windows.Forms.TextBox();
  59.             this.includeFromCheckBox = new System.Windows.Forms.CheckBox();
  60.             this.includeToCheckBox = new System.Windows.Forms.CheckBox();
  61.             this.resultTypeLabel = new System.Windows.Forms.Label();
  62.             this.resultTypeComboBox = new System.Windows.Forms.ComboBox();
  63.             this.generateButton = new System.Windows.Forms.Button();
  64.             this.resultBox = new System.Windows.Forms.TextBox();
  65.             this.clearButton = new System.Windows.Forms.Button();
  66.             this.SuspendLayout();
  67.             //
  68.             // numbersAmountLabel
  69.             //
  70.             this.numbersAmountLabel.AutoSize = true;
  71.             this.numbersAmountLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
  72.             this.numbersAmountLabel.Location = new System.Drawing.Point(31, 31);
  73.             this.numbersAmountLabel.Name = "numbersAmountLabel";
  74.             this.numbersAmountLabel.Size = new System.Drawing.Size(122, 40);
  75.             this.numbersAmountLabel.TabIndex = 0;
  76.             this.numbersAmountLabel.Text = "Choose amount\r\nof numbers:";
  77.             //
  78.             // numbersAmountInputBox
  79.             //
  80.             this.numbersAmountInputBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
  81.             this.numbersAmountInputBox.Location = new System.Drawing.Point(159, 31);
  82.             this.numbersAmountInputBox.MaxLength = 2;
  83.             this.numbersAmountInputBox.Multiline = true;
  84.             this.numbersAmountInputBox.Name = "numbersAmountInputBox";
  85.             this.numbersAmountInputBox.Size = new System.Drawing.Size(40, 40);
  86.             this.numbersAmountInputBox.TabIndex = 1;
  87.             this.numbersAmountInputBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.disableEnterKeyNewLine);
  88.             this.numbersAmountInputBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.makeSureOnlyNumbersAreAccepted);
  89.             //
  90.             // rangeFromLabel
  91.             //
  92.             this.rangeFromLabel.AutoSize = true;
  93.             this.rangeFromLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  94.             this.rangeFromLabel.Location = new System.Drawing.Point(334, 40);
  95.             this.rangeFromLabel.Name = "rangeFromLabel";
  96.             this.rangeFromLabel.Size = new System.Drawing.Size(50, 20);
  97.             this.rangeFromLabel.TabIndex = 2;
  98.             this.rangeFromLabel.Text = "From:";
  99.             //
  100.             // rangeFromInputBox
  101.             //
  102.             this.rangeFromInputBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
  103.             this.rangeFromInputBox.Location = new System.Drawing.Point(381, 31);
  104.             this.rangeFromInputBox.MaxLength = 2;
  105.             this.rangeFromInputBox.Multiline = true;
  106.             this.rangeFromInputBox.Name = "rangeFromInputBox";
  107.             this.rangeFromInputBox.Size = new System.Drawing.Size(40, 40);
  108.             this.rangeFromInputBox.TabIndex = 3;
  109.             this.rangeFromInputBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.disableEnterKeyNewLine);
  110.             this.rangeFromInputBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.makeSureOnlyNumbersAreAccepted);
  111.             //
  112.             // rangeToLabel
  113.             //
  114.             this.rangeToLabel.AutoSize = true;
  115.             this.rangeToLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
  116.             this.rangeToLabel.Location = new System.Drawing.Point(428, 40);
  117.             this.rangeToLabel.Name = "rangeToLabel";
  118.             this.rangeToLabel.Size = new System.Drawing.Size(31, 20);
  119.             this.rangeToLabel.TabIndex = 4;
  120.             this.rangeToLabel.Text = "To:";
  121.             //
  122.             // rangeToInputBox
  123.             //
  124.             this.rangeToInputBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
  125.             this.rangeToInputBox.Location = new System.Drawing.Point(457, 31);
  126.             this.rangeToInputBox.MaxLength = 2;
  127.             this.rangeToInputBox.Multiline = true;
  128.             this.rangeToInputBox.Name = "rangeToInputBox";
  129.             this.rangeToInputBox.Size = new System.Drawing.Size(40, 40);
  130.             this.rangeToInputBox.TabIndex = 5;
  131.             this.rangeToInputBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.disableEnterKeyNewLine);
  132.             this.rangeToInputBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.makeSureOnlyNumbersAreAccepted);
  133.             //
  134.             // includeFromCheckBox
  135.             //
  136.             this.includeFromCheckBox.AutoSize = true;
  137.             this.includeFromCheckBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
  138.             this.includeFromCheckBox.Location = new System.Drawing.Point(348, 77);
  139.             this.includeFromCheckBox.Name = "includeFromCheckBox";
  140.             this.includeFromCheckBox.Size = new System.Drawing.Size(70, 20);
  141.             this.includeFromCheckBox.TabIndex = 6;
  142.             this.includeFromCheckBox.Text = "Include";
  143.             this.includeFromCheckBox.UseVisualStyleBackColor = true;
  144.             //
  145.             // includeToCheckBox
  146.             //
  147.             this.includeToCheckBox.AutoSize = true;
  148.             this.includeToCheckBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
  149.             this.includeToCheckBox.Location = new System.Drawing.Point(434, 77);
  150.             this.includeToCheckBox.Name = "includeToCheckBox";
  151.             this.includeToCheckBox.Size = new System.Drawing.Size(70, 20);
  152.             this.includeToCheckBox.TabIndex = 7;
  153.             this.includeToCheckBox.Text = "Include";
  154.             this.includeToCheckBox.UseVisualStyleBackColor = true;
  155.             //
  156.             // resultTypeLabel
  157.             //
  158.             this.resultTypeLabel.AutoSize = true;
  159.             this.resultTypeLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
  160.             this.resultTypeLabel.Location = new System.Drawing.Point(228, 35);
  161.             this.resultTypeLabel.Name = "resultTypeLabel";
  162.             this.resultTypeLabel.Size = new System.Drawing.Size(85, 18);
  163.             this.resultTypeLabel.TabIndex = 8;
  164.             this.resultTypeLabel.Text = "Result type:";
  165.             //
  166.             // resultTypeComboBox
  167.             //
  168.             this.resultTypeComboBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
  169.             this.resultTypeComboBox.FormattingEnabled = true;
  170.             this.resultTypeComboBox.Items.AddRange(new object[] {
  171.             "Randomize",
  172.             "Sort ascending",
  173.             "Sort descending"});
  174.             this.resultTypeComboBox.Location = new System.Drawing.Point(209, 56);
  175.             this.resultTypeComboBox.Name = "resultTypeComboBox";
  176.             this.resultTypeComboBox.Size = new System.Drawing.Size(121, 24);
  177.             this.resultTypeComboBox.TabIndex = 10;
  178.             this.resultTypeComboBox.SelectedIndex = 0;
  179.             this.resultTypeComboBox.SelectedIndexChanged += new System.EventHandler(this.resultTypeSelection);
  180.             //
  181.             // generateButton
  182.             //
  183.             this.generateButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
  184.             this.generateButton.Location = new System.Drawing.Point(117, 148);
  185.             this.generateButton.Name = "generateButton";
  186.             this.generateButton.Size = new System.Drawing.Size(301, 50);
  187.             this.generateButton.TabIndex = 11;
  188.             this.generateButton.Text = "Generate";
  189.             this.generateButton.UseVisualStyleBackColor = true;
  190.             this.generateButton.Click += new System.EventHandler(this.generateRandomNumbers);
  191.             //
  192.             // resultBox
  193.             //
  194.             this.resultBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
  195.             this.resultBox.Location = new System.Drawing.Point(12, 252);
  196.             this.resultBox.Multiline = true;
  197.             this.resultBox.Name = "resultBox";
  198.             this.resultBox.ReadOnly = true;
  199.             this.resultBox.ScrollBars = System.Windows.Forms.ScrollBars.Both;
  200.             this.resultBox.Size = new System.Drawing.Size(509, 173);
  201.             this.resultBox.TabIndex = 12;
  202.             //
  203.             // clearButton
  204.             //
  205.             this.clearButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
  206.             this.clearButton.Location = new System.Drawing.Point(216, 206);
  207.             this.clearButton.Name = "clearButton";
  208.             this.clearButton.Size = new System.Drawing.Size(104, 37);
  209.             this.clearButton.TabIndex = 13;
  210.             this.clearButton.Text = "Clear All";
  211.             this.clearButton.UseVisualStyleBackColor = true;
  212.             this.clearButton.Click += new System.EventHandler(this.clearEverything);
  213.             //
  214.             // Form1
  215.             //
  216.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  217.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  218.             this.ClientSize = new System.Drawing.Size(533, 437);
  219.             this.Controls.Add(this.clearButton);
  220.             this.Controls.Add(this.resultBox);
  221.             this.Controls.Add(this.generateButton);
  222.             this.Controls.Add(this.resultTypeComboBox);
  223.             this.Controls.Add(this.resultTypeLabel);
  224.             this.Controls.Add(this.includeToCheckBox);
  225.             this.Controls.Add(this.includeFromCheckBox);
  226.             this.Controls.Add(this.rangeToInputBox);
  227.             this.Controls.Add(this.rangeToLabel);
  228.             this.Controls.Add(this.rangeFromInputBox);
  229.             this.Controls.Add(this.rangeFromLabel);
  230.             this.Controls.Add(this.numbersAmountInputBox);
  231.             this.Controls.Add(this.numbersAmountLabel);
  232.             this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  233.             this.KeyPreview = true;
  234.             this.Name = "Form1";
  235.             this.Text = "Random Numbers Generator";
  236.             this.ResumeLayout(false);
  237.             this.PerformLayout();
  238.  
  239.         }
  240.  
  241.         #endregion
  242.        
  243.         public Form1()
  244.         {
  245.             InitializeComponent();
  246.         }
  247.  
  248.         private void disableEnterKeyNewLine(object sender, KeyEventArgs e)
  249.         {
  250.             if (e.KeyCode == Keys.Enter)
  251.             {
  252.                 e.SuppressKeyPress = true;
  253.             }
  254.         }
  255.  
  256.         private void makeSureOnlyNumbersAreAccepted(object sender, KeyPressEventArgs e)
  257.         {
  258.             if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
  259.             {
  260.                 e.Handled = true;
  261.                 if (((this.numbersAmountInputBox.Focused) && (this.numbersAmountInputBox.Text.Length != 2)) || ((this.rangeFromInputBox.Focused) && (this.rangeFromInputBox.Text.Length != 2)) || ((this.rangeToInputBox.Focused) && (this.rangeToInputBox.Text.Length != 2)))
  262.                 {
  263.                     this.resultBox.Clear();
  264.                     this.resultBox.Text += "Please enter only whole positive integer numbers in all blank input spaces!";
  265.                 }
  266.             }
  267.             else
  268.             {
  269.                 this.resultBox.Clear();
  270.             }
  271.             this.generateButtonPressed = false;
  272.         }
  273.  
  274.         int amountNumsGlobal = 0;
  275.         int[] defaultArray;
  276.         bool generateButtonPressed = false;
  277.  
  278.         private void resultTypeSelection(object sender, EventArgs e)
  279.         {
  280.             bool amountNumsIsOK = Int32.TryParse(this.numbersAmountInputBox.Text, out int amountNums);
  281.             bool fromNumIsOK = Int32.TryParse(this.rangeFromInputBox.Text, out int fromNum);
  282.             bool toNumIsOK = Int32.TryParse(this.rangeToInputBox.Text, out int toNum);
  283.  
  284.             if ((amountNumsIsOK) && (fromNumIsOK) && (toNumIsOK))
  285.             {
  286.                 if (generateButtonPressed == true)
  287.                 {
  288.                     if (this.resultTypeComboBox.SelectedIndex == 0)
  289.                     {
  290.                         if (this.resultBox.Text != null)
  291.                         {
  292.                             defaultArray = new int[amountNumsGlobal];
  293.                             string values = this.resultBox.Text;
  294.                             string[] tokens = values.Split(' ');
  295.  
  296.                             for (int i = 0; i < tokens.Length - 1; i++)
  297.                             {
  298.                                 defaultArray[i] = Int32.Parse(tokens[i]);
  299.                             }
  300.  
  301.                             int[] shuffledDefaultArray = defaultArray.OrderBy(n => Guid.NewGuid()).ToArray();
  302.  
  303.                             this.resultBox.Clear();
  304.                             for (int i = 0; i < shuffledDefaultArray.Length; i++)
  305.                             {
  306.                                 this.resultBox.Text += shuffledDefaultArray[i] + " ";
  307.                             }
  308.                         }
  309.                         else
  310.                         {
  311.                             this.resultBox.Clear();
  312.                             for (int i = 0; i < defaultArray.Length; i++)
  313.                             {
  314.                                 this.resultBox.Text += defaultArray[i] + " ";
  315.                             }
  316.                         }
  317.                     }
  318.                     else if (this.resultTypeComboBox.SelectedIndex == 1)
  319.                     {
  320.                         int[] ascendingArray = new int[amountNums];
  321.                         string values = this.resultBox.Text;
  322.                         string[] tokens = values.Split(' ');
  323.  
  324.                         for (int i = 0; i < tokens.Length - 1; i++)
  325.                         {
  326.                             ascendingArray[i] = Int32.Parse(tokens[i]);
  327.                         }
  328.                         Array.Sort(ascendingArray);
  329.  
  330.                         this.resultBox.Clear();
  331.                         for (int i = 0; i < ascendingArray.Length; i++)
  332.                         {
  333.                             this.resultBox.Text += ascendingArray[i] + " ";
  334.                         }
  335.                     }
  336.                     else if (this.resultTypeComboBox.SelectedIndex == 2)
  337.                     {
  338.                         int[] descendingArray = new int[amountNums];
  339.                         string values = this.resultBox.Text;
  340.                         string[] tokens = values.Split(' ');
  341.  
  342.                         for (int i = 0; i < tokens.Length - 1; i++)
  343.                         {
  344.                             descendingArray[i] = Int32.Parse(tokens[i]);
  345.                         }
  346.                         Array.Sort(descendingArray);
  347.                         Array.Reverse(descendingArray);
  348.  
  349.                         this.resultBox.Clear();
  350.                         for (int i = 0; i < descendingArray.Length; i++)
  351.                         {
  352.                             this.resultBox.Text += descendingArray[i] + " ";
  353.                         }
  354.                     }
  355.                 }
  356.                 else
  357.                 {
  358.                     this.resultBox.Clear();
  359.                     this.resultBox.Text += "Please make sure that the 'Generate' button has been pressed!";
  360.                 }
  361.             }
  362.             else
  363.             {
  364.                 this.resultBox.Clear();
  365.                 this.resultBox.Text += "Please make sure that all fields have valid input in them!";
  366.             }
  367.         }
  368.  
  369.         private void generateRandomNumbers(object sender, EventArgs e)
  370.         {
  371.             if ((this.numbersAmountInputBox.Text != null) && (this.rangeFromInputBox.Text != null) && (this.rangeToInputBox.Text != null))
  372.             {
  373.                 bool amountNumsIsOK = Int32.TryParse(this.numbersAmountInputBox.Text, out int amountNums);
  374.                 bool fromNumIsOK = Int32.TryParse(this.rangeFromInputBox.Text, out int fromNum);
  375.                 bool toNumIsOK = Int32.TryParse(this.rangeToInputBox.Text, out int toNum);
  376.  
  377.                 this.resultBox.Clear();
  378.  
  379.                 if ((amountNumsIsOK) && (fromNumIsOK) && (toNumIsOK))
  380.                 {
  381.                     amountNumsGlobal = amountNums;
  382.                     int[] array = new int[amountNums];
  383.                     for (int i = 0; i < amountNums; i++)
  384.                     {
  385.                         array[i] = 100;
  386.                     }
  387.                     Random random = new Random();
  388.                     int temp = 0;
  389.                     int min = 0;
  390.                     int max = 0;
  391.  
  392.                     if ((this.includeFromCheckBox.Checked) && (this.includeToCheckBox.Checked))
  393.                     {
  394.                         min = fromNum;
  395.                         max = toNum + 1;
  396.                     }
  397.                     else if ((this.includeFromCheckBox.Checked) && (!this.includeToCheckBox.Checked))
  398.                     {
  399.                         min = fromNum;
  400.                         max = toNum;
  401.                     }
  402.                     else if ((!this.includeFromCheckBox.Checked) && (this.includeToCheckBox.Checked))
  403.                     {
  404.                         min = fromNum + 1;
  405.                         max = toNum + 1;
  406.                     }
  407.                     else if ((!this.includeFromCheckBox.Checked) && (!this.includeToCheckBox.Checked))
  408.                     {
  409.                         min = fromNum + 1;
  410.                         max = toNum;
  411.                     }
  412.                    
  413.                     if (min >= max)
  414.                     {
  415.                         this.resultBox.Clear();
  416.                         this.resultBox.Text += "Please make sure that the borders are set correctly: From < To!";
  417.                     }
  418.                     else if ((min < max) && (max - min < amountNums))
  419.                     {
  420.                         this.resultBox.Clear();
  421.                         this.resultBox.Text += "Please make sure that there are enough numbers inside the borders: To - From >= Amount!";
  422.                     }
  423.                     else if ((min < max) && (max - min >= amountNums))
  424.                     {
  425.                         if (this.resultTypeComboBox.SelectedIndex == 0)
  426.                         {
  427.                             for (int i = 0; i < array.Length; i++)
  428.                             {
  429.                                 temp = random.Next(min, max);
  430.                                 while (isDuplicate(temp, array))
  431.                                 {
  432.                                     temp = random.Next(min, max);
  433.                                 }
  434.                                 array[i] = temp;
  435.                             }
  436.                         }
  437.                         else if (this.resultTypeComboBox.SelectedIndex == 1)
  438.                         {
  439.                             for (int i = 0; i < array.Length; i++)
  440.                             {
  441.                                 temp = random.Next(min, max);
  442.                                 while (isDuplicate(temp, array))
  443.                                 {
  444.                                     temp = random.Next(min, max);
  445.                                 }
  446.                                 array[i] = temp;
  447.                             }
  448.                             Array.Sort(array);
  449.                         }
  450.                         else if (this.resultTypeComboBox.SelectedIndex == 2)
  451.                         {
  452.                             for (int i = 0; i < array.Length; i++)
  453.                             {
  454.                                 temp = random.Next(min, max);
  455.                                 while (isDuplicate(temp, array))
  456.                                 {
  457.                                     temp = random.Next(min, max);
  458.                                 }
  459.                                 array[i] = temp;
  460.                             }
  461.                             Array.Sort(array);
  462.                             Array.Reverse(array);
  463.                         }
  464.  
  465.                         for (int i = 0; i < array.Length; i++)
  466.                         {
  467.                             this.resultBox.Text += array[i] + " ";
  468.                         }
  469.  
  470.                         defaultArray = new int[amountNumsGlobal];
  471.                         string values = this.resultBox.Text;
  472.                         string[] tokens = values.Split(' ');
  473.  
  474.                         for (int i = 0; i < tokens.Length - 1; i++)
  475.                         {
  476.                             defaultArray[i] = Int32.Parse(tokens[i]);
  477.                         }
  478.                     }
  479.                 }
  480.                 else
  481.                 {
  482.                     this.resultBox.Clear();
  483.                     this.resultBox.Text += "Please enter only whole positive integer numbers in all blank input spaces!";
  484.                 }
  485.  
  486.                 generateButtonPressed = true;
  487.             }
  488.         }
  489.  
  490.         private static bool isDuplicate(int tmp, int[] arr)
  491.         {
  492.             foreach (var item in arr)
  493.             {
  494.                 if (item == tmp)
  495.                 {
  496.                     return true;
  497.                 }
  498.             }
  499.             return false;
  500.         }
  501.  
  502.         private void clearEverything(object sender, EventArgs e)
  503.         {
  504.             this.numbersAmountInputBox.Clear();
  505.             this.resultTypeComboBox.SelectedIndex = 0;
  506.             this.rangeFromInputBox.Clear();
  507.             this.includeFromCheckBox.Checked = false;
  508.             this.rangeToInputBox.Clear();
  509.             this.includeToCheckBox.Checked = false;
  510.             this.resultBox.Clear();
  511.             this.generateButtonPressed = false;
  512.         }
  513.  
  514.         private System.Windows.Forms.Label numbersAmountLabel;
  515.         private System.Windows.Forms.TextBox numbersAmountInputBox;
  516.         private System.Windows.Forms.Label rangeFromLabel;
  517.         private System.Windows.Forms.TextBox rangeFromInputBox;
  518.         private System.Windows.Forms.Label rangeToLabel;
  519.         private System.Windows.Forms.TextBox rangeToInputBox;
  520.         private System.Windows.Forms.CheckBox includeFromCheckBox;
  521.         private System.Windows.Forms.CheckBox includeToCheckBox;
  522.         private System.Windows.Forms.Label resultTypeLabel;
  523.         private System.Windows.Forms.ComboBox resultTypeComboBox;
  524.         private System.Windows.Forms.Button generateButton;
  525.         private System.Windows.Forms.TextBox resultBox;
  526.         private Button clearButton;
  527.     }
  528. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement