Advertisement
dragnscalearmor

C# Day 2 / 100

Feb 17th, 2020
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.71 KB | None | 0 0
  1. // Reverses an input string and displays it in a text box.
  2. // WinForms - create a new form with these controls:
  3. // textboxes:  textInput, textOutput
  4. // buttons:  buttonCopy, buttonClear, buttonProcess
  5.  
  6. using System;
  7. using System.Windows.Forms;
  8.  
  9. namespace Reverse_A_String
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.  
  18.         private void ButtonProcess_Click(object sender, EventArgs e)
  19.         {
  20.             if (textInput.Text.Length < 2)
  21.             {
  22.                 MessageBox.Show("Enter at least two characters and try again.");
  23.                 return;
  24.             }
  25.  
  26.             string str = textInput.Text;
  27.             textOutput.Text = Reverse(str);
  28.             labelStatus.Text = "Processed";
  29.         }
  30.  
  31.         public static string Reverse(string str)
  32.         {
  33.             char[] c = str.ToCharArray();
  34.             Array.Reverse(c);
  35.             str = string.Concat(c);
  36.             return str;
  37.         }
  38.  
  39.         private void ButtonClear_Click(object sender, EventArgs e)
  40.         {
  41.             textInput.Clear();
  42.             textOutput.Clear();
  43.             labelStatus.Text = ".....";
  44.             textInput.Focus();
  45.         }
  46.  
  47.         private void ButtonCopy_Click(object sender, EventArgs e)
  48.         {
  49.             try
  50.             {
  51.                 Clipboard.SetText(textOutput.Text);
  52.                 labelStatus.Text = "Copied";
  53.             }
  54.             catch (Exception)
  55.             {
  56.                 labelStatus.Text = "Error";
  57.             }
  58.         }
  59.  
  60.         private void TextOutput_TextChanged(object sender, EventArgs e)
  61.         {
  62.             labelStatus.Text = ".....";
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement