Advertisement
TPiddock

GUI troubles Optimised

Apr 29th, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Windows.Forms;
  10.  
  11. namespace DSemulator
  12. {
  13.     public partial class GUI : Form
  14.     {
  15.     Integer screenWidth;
  16.     Integer screenHeight;
  17.     TextBox speedBox;
  18.     TextBox lengthBox;
  19.     TextBox amountBox;
  20.     TextBox faultBox;
  21.     Button startButton;
  22.     ComboBox laneBox;
  23.     Graphics g;
  24.  
  25.         public GUI(Mainentry mainEntry)
  26.         {
  27.             InitializeComponent();
  28.  
  29.  
  30.         //Creating a constant integer for screen width/height prevents repeated calls to
  31.         // Screen.PrimaryScreen.WorkingArea - optimizing the number of calculations done
  32.         screenWidth = Screen.PrimaryScreen.WorkingArea.Width ;
  33.         screenHeight = Screen.PrimaryScreen.WorkingArea.Height;
  34.  
  35.  
  36.             g = this.CreateGraphics();
  37.  
  38.             int X = (screenWidth / 8);
  39.             int Y = (screenHeight / 4) * 3;
  40.            
  41.             speedBox = new TextBox();
  42.             speedBox.Name = "speedBox";
  43.             speedBox.MaxLength = 5;
  44.             speedBox.Location = new Point(X, Y);
  45.             speedBox.KeyPress += input_KeyPress;
  46.  
  47.             lengthBox = new TextBox();
  48.             lengthBox.Name = "lengthBox";
  49.             lengthBox.MaxLength = 5;
  50.             lengthBox.Location = new Point(X, Y + 50);
  51.             lengthBox.KeyPress += input_KeyPress;
  52.  
  53.             amountBox = new TextBox();
  54.             amountBox.Name = "amountBox";
  55.             amountBox.MaxLength = 2;
  56.             amountBox.Location = new Point(X, Y + 100);
  57.             amountBox.KeyPress += amountBox_KeyPress;
  58.  
  59.             faultBox = new TextBox();
  60.             faultBox.Name = "faultBox";
  61.             faultBox.MaxLength = 2;
  62.             faultBox.Location = new Point(X, Y + 150);
  63.             faultBox.KeyPress += amountBox_KeyPress;
  64.  
  65.             laneBox = new ComboBox();
  66.             laneBox.Name = "laneBox";
  67.             for (int i = 1; i <= 3; i++)
  68.                 laneBox.Items.Add("Drivelane " + i);
  69.             laneBox.SelectedIndex = 0;
  70.             laneBox.Location = new Point(X, Y - 50);
  71.  
  72.             startButton = new Button();
  73.             startButton.Name = "startButton";
  74.             startButton.Text = "Start";
  75.             startButton.BackColor = Color.Green;
  76.             startButton.Size = new Size(100, 50);
  77.             startButton.Font = new Font("Times New Roman", 20);
  78.             startButton.Location = new Point((screenWidth / 3), (screenHeight / 4) * 3);
  79.  
  80.             this.Controls.Add(speedBox);
  81.             this.Controls.Add(lengthBox);
  82.             this.Controls.Add(amountBox);
  83.             this.Controls.Add(faultBox);
  84.             this.Controls.Add(laneBox);
  85.             this.Controls.Add(startButton);
  86.  
  87.             startButton.Click += startButton_Click;
  88.         }
  89.  
  90.         void startButton_Click(object sender, EventArgs e)
  91.         {
  92.             Control[] temp = this.Controls.Find("speedBox", false);
  93.             float speed = float.Parse(((TextBox)temp[0]).Text);
  94.             ((TextBox)temp[0]).Text = String.Empty;
  95.  
  96.             temp = this.Controls.Find("lengthBox", false);
  97.             float length = float.Parse(((TextBox)temp[0]).Text);
  98.             ((TextBox)temp[0]).Text = String.Empty;
  99.  
  100.             temp = this.Controls.Find("amountBox", false);
  101.             int amount = int.Parse(((TextBox)temp[0]).Text);
  102.             ((TextBox)temp[0]).Text = String.Empty;
  103.  
  104.             temp = this.Controls.Find("faultBox", false);
  105.             int fault = int.Parse(((TextBox)temp[0]).Text);
  106.             ((TextBox)temp[0]).Text = String.Empty;
  107.  
  108.             temp = this.Controls.Find("laneBox", false);
  109.             int lane = ((ComboBox)temp[0]).SelectedIndex + 1;
  110.  
  111.             //call some method in another object
  112.         }
  113.  
  114.         void amountBox_KeyPress(object sender, KeyPressEventArgs e)
  115.         {
  116.             Byte backspace = 0x8;
  117.             if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == Convert.ToChar(backspace))
  118.                 e.Handled = false;
  119.             else
  120.                 e.Handled = true;
  121.         }
  122.  
  123.         void input_KeyPress(object sender, KeyPressEventArgs e)
  124.         {
  125.             Byte backspace = 0x8;
  126.             if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == Convert.ToChar(backspace) || e.KeyChar == ',')
  127.                 e.Handled = false;  //accept character
  128.             else
  129.                 e.Handled = true;   //decline character
  130.         }
  131.  
  132.          private void GUI_Load(object sender, EventArgs e)
  133.         {
  134.     }
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement