This is going to be somewhat a lengthy post, i do hope someone here can help me with this problem that i've already spent a gazillion brain cells on. >.<

I AM a student asking for help as i cannot seem to be making this work with my atrocious programming skills.

I am doing this project as part of my attachment for my diploma.

Meh.


What i've got so far is that i'm done with my UI,
(being very ugly and unprofessional looking, guess i'm going to have to come up with some background designs)

[IMG]http://img.photobucket.com/albums/v95/freakyphua/LoginSample.jpg[/IMG]

This is the code so far,

private void btnA_Click(object sender, EventArgs e)
        {
            if (Pwdsel == 1)
            {
                insertStringPass(btnA.Text, this.Password.SelectionStart);
            }
            else
            {
                insertStringUser(btnA.Text, this.Username.SelectionStart);
            }
        }

        private void btnB_Click_1(object sender, EventArgs e)
        {
            if (Pwdsel == 1)
            {
                insertStringPass(btnB.Text, this.Password.SelectionStart);
            }
            else
            {
                insertStringUser(btnB.Text, this.Username.SelectionStart);
            }
        }

        private void btnC_Click(object sender, EventArgs e)
        {
            if (InputCaps == 0)
            {
                if (Pwdsel == 1)
                {
                    insertStringPass(btnC.Text, this.Password.SelectionStart);
                }
                else
                {
                    insertStringUser(btnC.Text, this.Username.SelectionStart);
                }
            }
        }

        private void btnD_Click(object sender, EventArgs e)
        {
            if (Pwdsel == 1)
            {
                insertStringPass(btnD.Text, this.Password.SelectionStart);
            }
            else
            {
                insertStringUser(btnD.Text, this.Username.SelectionStart);
            }
        }

        private void btnE_Click(object sender, EventArgs e)
        {
            if (Pwdsel == 1)
            {
                insertStringPass(btnE.Text, this.Password.SelectionStart);
            }
            else
            {
                insertStringUser(btnE.Text, this.Username.SelectionStart);
            }
        }

All the buttons are similar, so putting ALL of them here would seem abit silly.

I've also added a button to switch the focus (for the lack of a better word) from the username textbox to the password textbox.

private void UserSubmit_Click(object sender, EventArgs e)
        {
            if (Username.Text.Length < 1)
            {
                MessageBox.Show("Please enter username");
            }
            else
            {
                Username.Enabled = false;
                Password.Enabled = true;
                LoginBtn.Enabled = true;
                UserSubmit.Enabled = false;
                Pwdsel = 1;
            }
        }

But this isn't how i want the program to be, i wanted the users to be able to click on the password textbox and be able to input their passwords using the buttons instead of having to press the submit button to switch the focus. (Emulating input with a keyboard, you just have to click on either textbox to switch the focus to type in your passwords/usernames, right? I'm trying to do the same thing.)

I've tried putting this in, but it just doesn't work!

private void Password_OnClick(object sender, EventArgs e)
        {
            if (Username.Text.Length < 1)
            {
                MessageBox.Show("Please enter username");
            }
            else
            {
                Username.Enabled = false;
                Password.Enabled = true;
                LoginBtn.Enabled = true;
                UserSubmit.Enabled = false;
                this.Password.Focus();
                Pwdsel = 1;
            }
        }

As to why i have to program it with a on screen keyboard, it's because i'm doing this for a smart device running windows CE.

Using the OSK.exe is not a viable option for me :X

Anyhelp is appreciated! Thanks in advance :]

Dani AI

Generated

Practical, compact fixes for the on‑screen keyboard and safer credential handling.

Consolidate all key buttons into one Click handler (as suggested) and stop relying on Control.Focus when inserting characters — track the active input field instead. Attach an Enter/GotFocus (or Click) handler to each textbox that sets a Form‑level TextBox activeBox, then have every key button insert into activeBox. That avoids the Submit workaround and survives the button briefly stealing focus.

// form scope
TextBox activeBox = null;

private void Field_Enter(object sender, EventArgs e)
{
    activeBox = sender as TextBox;
}

private void Key_Click(object sender, EventArgs e)
{
    if (activeBox == null) return;        // no field selected
    string key = (sender as Button).Text;
    activeBox.SelectedText = key;         // inserts at caret / replaces selection
    // optional: activeBox.SelectionStart = activeBox.SelectionStart; 
}

Minimize focus side‑effects by setting keyboard buttons TabStop = false or using non‑selectable controls for keys. Implement Backspace/Left/Right by manipulating SelectionStart and SelectedText (example Backspace shown below). Wire all key controls once (designer or small loop) instead of a handler per letter.

private void Backspace_Click(object sender, EventArgs e)
{
    if (activeBox == null) return;
    if (activeBox.SelectionLength > 0) { activeBox.SelectedText = ""; return; }
    int p = activeBox.SelectionStart;
    if (p == 0) return;
    activeBox.Text = activeBox.Text.Remove(p - 1, 1);
    activeBox.SelectionStart = p - 1;
}

On storage: never keep plaintext passwords. Store a per‑user salt and a derived hash (prefer PBKDF2/Rfc2898 if available; otherwise SHA‑256 with a random salt). Use a single data file with lines like username|saltBase64|hashBase64, and verify by recomputing the hash for the entered password. Keep the file in the app’s private area and consider encrypting it on device before shipping. For a demo hardcoded creds are acceptable, but replace them with the salted/hash approach before production. — this pattern keeps the keyboard intuitive and makes later security improvements straightforward.

Recommended Answers

All 6 Replies

1/. You don't need a new method for every button just call the same method and do somthing like this:

private void btnLetter_Click(object sender, EventArgs e)        
{     
    if (Pwdsel == 1)            
    {               
        insertStringPass((sender as Button).Text, this.Password.SelectionStart); 
    }
    else 
    { 
        insertStringUser((sender as Button).Text, this.Username.SelectionStart);
    }
}

2/. You should be able to use the Click or MouseClick events for the text boxes themselves rather than using the seperate submit button. I am not sure why that is not working for you. I don't think using the focus will work as that will probably be lost when a user clicks a button on your keyboard.

You could force the user to enter a user name and when that is done force them to enter a password.

Haha, that's what i've done, i added a submit button and disabled the password box to force them to enter their username first, this was also a solution to go around the problem with the focus on the password box >.<

i'm going to try the method you showed me.

thanks alot void :D!

private void LoginBtn_Click(object sender, EventArgs e)
        {
            if (Username.Text == "STEE")
            {
                TextWriter tw = new StreamWriter("usernames.txt");

                tw.WriteLine(Username.Text);

                tw.Close();

                if (Password.Text == "12345")
                {
                    TextWriter tpwd = new StreamWriter("passwords.txt");

                    tpwd.WriteLine(Password.Text);

                    tpwd.Close();

                    MessageBox.Show(Username.Text + " successfully logged on");
                }
                else
                {
                    MessageBox.Show("Incorrect Username/Password!");
                }
            }
            else if (Password.Text.Length < 1)
            {
                MessageBox.Show("Please enter password");
            }
            else
            {
                MessageBox.Show("Incorrect Username/Password!");
            }
        }

And also another problem i'm facing is i'm not sure if this is the correct way to store the credentials that the users have entered, is there an alternative method to store them and read? I've gone through MSDN and several forums but i can't seem to make heads or tails of it >.<

To be honest I don't see why you are bothering to store the data there as it will only ever work if the username is STEE and the password is 12345.

If you are going to have different passwords that can be changed you are going to have to store the information on disk. There are two ways to do that, one is a database and the other is in a file. In this case I am assuming a file is the way to go. I think you would be better of with a binary file and you may want to consider encrypting the data depending on how sensative the data is.

Have a look here for tutorials on using files:
http://www.codersource.net/csharp_read_write_binary_files.html

To be honest I don't see why you are bothering to store the data there as it will only ever work if the username is STEE and the password is 12345.

If you are going to have different passwords that can be changed you are going to have to store the information on disk. There are two ways to do that, one is a database and the other is in a file. In this case I am assuming a file is the way to go. I think you would be better of with a binary file and you may want to consider encrypting the data depending on how sensative the data is.

Have a look here for tutorials on using files:
http://www.codersource.net/csharp_read_write_binary_files.html

I'm hard coding the usernames and passwords in because this is to be used as a demo for now. :/

but minimum requirement is that it has to be able to store and read the usernames and passwords from the textfile(s).

I'll give the links you posted a read, thanks :D!

Ultimately, this is to be integrated into another program, i think the aim of giving me this project is for me to learn, as i am a attachment student (an intern in other words). This product is to be sold as a commercial device, thus i doubt that they will implement something so unprofessional looking :X, afterall, there's still encryption and security and a truck load of stuff to be added on still :/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.