Search code examples
c#uppercasetoupper

How to get the first 4 letters to be uppercase letter in the next Textbox?


New to programing and its a user/password generator. I want to have the first 4 letters in every name from the left textbox and the result should be 4 uppercase letters (i) same 4 uppercase letter / password generated

ex:

Adair Ewing
Zorro Irving

(Pusching generatbutton)
(Result)

ADAI0ASAI / dkfnwkef
ZOOR1ZOOR / woeknfwe

My code:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonGenerat_Click(object sender, EventArgs e)
        {
            char[] radbytning = new char[]{'\r', '\n'};
            string[] name = textBoxLeft.Text.Split(radbytning, StringSplitOptions.RemoveEmptyEntries);
            string result = "";
            for (int i = 0; i <name.Length; i++)
            {
                result += string.Format("{0}{1}{0} / {2}\r\n", name[i].Substring(0, 4), i, GeneratPassword());
            }

            textBoxRight.Text = result;

        }

        private static string GeneratPassword() 
        {
            string result = "";
            String tecken = "abcdefghijklmnopqrstuvxyzåäö1234567890ABCDEFGHIJKLMNOPQRSTUVXYZÅÄÖ";
            Random random = new Random();
            for(int i = 0; i< 8; i++)
            {
                result += tecken[random.Next(tecken.Length)].ToString();
                System.Threading.Thread.Sleep(10);
            }
            return result;
        }

          public string FirstLetterToUpper(string)
        {
            if (str == null)
                return null;

            if (str.Length > 1)
                return char.ToUpper(str[0]) + str.Substring(1);

            return str;
        }
    }
}

Solution

  • This line of code throws an error because there is no variable attached to the "string" you will be passing public string FirstLetterToUpper(string)

    However, I don't think you need this function at all and you don't even call it in your code.

    I think you just need to change the following line of code to get what you want and delete the Function I mention above:

    result += string.Format("{0}{1}{0} / {2}\r\n", name[i].Substring(0, 4), i, GeneratPassword());
    

    Change to:

    result += string.Format("{0}{1}{0} / {2}\r\n", name[i].Substring(0, 4).ToUpper(), i, GeneratPassword());
    

    That then gives an output

    ADAI0ADAI / CMldQQY7
    

    ZORR0ZORR / e3sbdxJQ

    I think this is what you are getting at, and that you have multiple typos in this part of your question!