Advertisement
JustAJ

GUI troubles

Apr 29th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.90 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.         TextBox speedBox;
  16.         TextBox lengthBox;
  17.         TextBox amountBox;
  18.         TextBox faultBox;
  19.         Button startButton;
  20.         ComboBox laneBox;
  21.         Graphics g;
  22.  
  23.         public GUI(Mainentry mainEntry)
  24.         {
  25.             InitializeComponent();
  26.             g = this.CreateGraphics();
  27.  
  28.             this.Paint += delegate { paintManualUI(); };
  29.             this.Paint += delegate { paintManualTextBoxes((Screen.PrimaryScreen.WorkingArea.Width / 8), (Screen.PrimaryScreen.WorkingArea.Height / 4) * 3); };
  30.         }
  31.  
  32.         private void paintManualUI()
  33.         {
  34.             int X = (Screen.PrimaryScreen.WorkingArea.Width / 8);
  35.             int Y = (Screen.PrimaryScreen.WorkingArea.Height / 4) * 3;
  36.            
  37.             speedBox = new TextBox();
  38.             speedBox.Name = "speedBox";
  39.             speedBox.MaxLength = 5;
  40.             speedBox.Location = new Point(X, Y);
  41.             speedBox.KeyPress += input_KeyPress;
  42.  
  43.             lengthBox = new TextBox();
  44.             lengthBox.Name = "lengthBox";
  45.             lengthBox.MaxLength = 5;
  46.             lengthBox.Location = new Point(X, Y + 50);
  47.             lengthBox.KeyPress += input_KeyPress;
  48.  
  49.             amountBox = new TextBox();
  50.             amountBox.Name = "amountBox";
  51.             amountBox.MaxLength = 2;
  52.             amountBox.Location = new Point(X, Y + 100);
  53.             amountBox.KeyPress += amountBox_KeyPress;
  54.  
  55.             faultBox = new TextBox();
  56.             faultBox.Name = "faultBox";
  57.             faultBox.MaxLength = 2;
  58.             faultBox.Location = new Point(X, Y + 150);
  59.             faultBox.KeyPress += amountBox_KeyPress;
  60.  
  61.             laneBox = new ComboBox();
  62.             laneBox.Name = "laneBox";
  63.             for (int i = 1; i <= 3; i++)
  64.                 laneBox.Items.Add("Drivelane " + i);
  65.             laneBox.SelectedIndex = 0;
  66.             laneBox.Location = new Point(X, Y - 50);
  67.  
  68.             startButton = new Button();
  69.             startButton.Name = "startButton";
  70.             startButton.Text = "Start";
  71.             startButton.BackColor = Color.Green;
  72.             startButton.Size = new Size(100, 50);
  73.             startButton.Font = new Font("Times New Roman", 20);
  74.             startButton.Location = new Point((Screen.PrimaryScreen.WorkingArea.Width / 3), (Screen.PrimaryScreen.WorkingArea.Height / 4) * 3);
  75.  
  76.             this.Controls.Add(speedBox);
  77.             this.Controls.Add(lengthBox);
  78.             this.Controls.Add(amountBox);
  79.             this.Controls.Add(faultBox);
  80.             this.Controls.Add(laneBox);
  81.             this.Controls.Add(startButton);
  82.  
  83.             startButton.Click += startButton_Click;
  84.         }
  85.  
  86.         void startButton_Click(object sender, EventArgs e)
  87.         {
  88.             Control[] temp = this.Controls.Find("speedBox", false);
  89.             float speed = float.Parse(((TextBox)temp[0]).Text);
  90.             ((TextBox)temp[0]).Text = String.Empty;
  91.  
  92.             temp = this.Controls.Find("lengthBox", false);
  93.             float length = float.Parse(((TextBox)temp[0]).Text);
  94.             ((TextBox)temp[0]).Text = String.Empty;
  95.  
  96.             temp = this.Controls.Find("amountBox", false);
  97.             int amount = int.Parse(((TextBox)temp[0]).Text);
  98.             ((TextBox)temp[0]).Text = String.Empty;
  99.  
  100.             temp = this.Controls.Find("faultBox", false);
  101.             int fault = int.Parse(((TextBox)temp[0]).Text);
  102.             ((TextBox)temp[0]).Text = String.Empty;
  103.  
  104.             temp = this.Controls.Find("laneBox", false);
  105.             int lane = ((ComboBox)temp[0]).SelectedIndex + 1;
  106.  
  107.             //call some method in another object
  108.         }
  109.  
  110.         void amountBox_KeyPress(object sender, KeyPressEventArgs e)
  111.         {
  112.             Byte backspace = 0x8;
  113.             if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == Convert.ToChar(backspace))
  114.                 e.Handled = false;
  115.             else
  116.                 e.Handled = true;
  117.         }
  118.  
  119.         void input_KeyPress(object sender, KeyPressEventArgs e)
  120.         {
  121.             Byte backspace = 0x8;
  122.             if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == Convert.ToChar(backspace) || e.KeyChar == ',')
  123.                 e.Handled = false;  //accept character
  124.             else
  125.                 e.Handled = true;   //decline character
  126.         }
  127.  
  128.         private void paintManualTextBoxes(int X, int Y)
  129.         {
  130.             TextBox speedText = new TextBox();
  131.             speedText.BorderStyle = BorderStyle.None;
  132.             speedText.Text = "Speed";
  133.             speedText.ReadOnly = true;
  134.             speedText.Location = new Point(X - 50, Y);
  135.  
  136.             TextBox lengthText = new TextBox();
  137.             lengthText.BorderStyle = BorderStyle.None;
  138.             lengthText.Text = "Length";
  139.             lengthText.ReadOnly = true;
  140.             lengthText.Location = new Point(X - 50, Y + 50);
  141.             lengthText.Size = new Size(40, 8);
  142.  
  143.             TextBox faultText = new TextBox();
  144.             faultText.BorderStyle = BorderStyle.None;
  145.             faultText.Text = "Faulty Detection";
  146.             faultText.ReadOnly = true;
  147.             faultText.Location = new Point(X - 93, Y + 100);
  148.  
  149.             TextBox amountText = new TextBox();
  150.             amountText.BorderStyle = BorderStyle.None;
  151.             amountText.Text = "Amount";
  152.             amountText.ReadOnly = true;
  153.             amountText.Location = new Point(X - 50, Y + 150);
  154.  
  155.             this.Controls.Add(speedText);
  156.             this.Controls.Add(lengthText);
  157.             this.Controls.Add(faultText);
  158.             this.Controls.Add(amountText);
  159.         }
  160.  
  161.         private void GUI_Load(object sender, EventArgs e)
  162.         {}
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement