Ok so i want to write a windows form in c# that encrypts and/or decrypts words/sentence/letters with the cesar shift encryption, i think i already have some good coding but i don't really know where to put what for the form programming, when i execute it and start writing something to encrypt/decrypt it just freezes after the first letter i enter
public partial class Encrypt : Form
public string phrase;
public char legend;
public char val;
public int key;
public string encryptedstring;
static public char EncryptChar(int key, char val, char legend)
int i = Array.IndexOf(legend, val) + key;
if (i >= legend.Length)
i -= legend.Length;
static public char DecryptChar(int key, char val, char legend)
int i = Array.IndexOf(legend, val) - key;
if (i < 0)
i += legend.Length;
private void txtPhrase_TextChanged(object sender, EventArgs e)
phrase = Convert.ToString(Console.ReadLine());
I'm not sure what you're trying to do here, but anytime you change txtPhrase, it will prompt the console (the black command line window, which isn't being displayed when you're running a WinForms program. So, when you change txtPhrase, it waits for console input that will never come thus the hang.
You have two options. If you want to leave your design as it is, set
phrase = txtPhrase.Text;
Alternatively you could do away with the class variable phrase altogether, delete the entire method that is causing your program to hang and change every instance of phrase to txtPhrase.Text. This is the most correct option.
Also, your encryption method could use a little work. In particular, (a) when you're building up a long string you should use StringBuilder instead of adding a lot of strings together, and (b) you can use modular arithmetic to calculate the new positions. To teach-by-example, I've re-written the method:
Actually, you could do even better by using a Dictionary<char,int> (MSDN link) to avoid the O(n) call to Array.IndexOf(...) and instead get O(1) lookup, but that's sort of beyond the scope of what you're learning right now.
I hope that helps! You can try modifying the decrypt subroutine if you want to learn how this method works.
(N.B.: I didn't actually run anything through a compiler; there might be a bug or two, not sure. Hopefully the idea is illustrated).
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.