4 Replies - 307 Views - Last Post: 23 August 2012 - 02:25 AM Rate Topic: -----

#1 cmd promt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-August 12

Clear All Textbox

Posted 21 August 2012 - 03:06 PM

Hi guys, long time reader first time poster, I need to know how to do a clear all the text in the text box's within a key maker made in VB 2010. Now it has 4 text box's, password length, number of passwords, paste to clip board option, and password preview box, now I want to be able to "clear all text" box's with a "clear all text" button which I have so when I press that "clear all text" button it will clear everything in those box's to start fresh. I just need to know where to put that code within the key maker code.

I have put the key maker code below:


Thank you if you can help, I'm new to VB and with limited knowledge of programing, learning.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace WindowsFormsApplication1
{
    //Password types
    enum iGenType { 
        Alpha,
        Numeric,
        AlphaNumeric 
    };

    public partial class Form1 : Form
    {
        private iGenType GenType;
        //Random chars to pick a password from
        private const string iAlpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        private const string iNumeric = "0123456789";
        private const string iAlphaNumeric = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        
        public Form1()
        {
            InitializeComponent();
        }

        private void ShowError(string message)
        {
            MessageBox.Show("Invaild value " + message,
                "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private static string MakePassword(int Length, iGenType PasswordType, Random Rnd)
        {
            StringBuilder sb = new StringBuilder();

            for (int x = 0; x < Length; x++)
            {
                //Build alpha only password
                if (PasswordType == iGenType.Alpha)
                {
                    //Pull out a random number
                    int RndNum = Rnd.Next(iAlpha.Length - 1);
                    //Build the random string.
                    sb.Append(iAlpha[RndNum]);
                }
                //Build Numeric only password
                if (PasswordType == iGenType.Numeric)
                {
                    //Pull out a random number
                    int RndNum = Rnd.Next(iNumeric.Length - 1);
                    //Build the random string.
                    sb.Append(iNumeric[RndNum]);
                }
                //Build AlphaNumeric password
                if (PasswordType == iGenType.AlphaNumeric)
                {
                    //Pull out a random number
                    int RndNum = Rnd.Next(iAlphaNumeric.Length - 1);
                    //Build the random string.
                    sb.Append(iAlphaNumeric[RndNum]);
                }
            }
            //Display random string
            return sb.ToString();
        }
        private void r1_CheckedChanged(object sender, EventArgs e)
        {
            GenType = iGenType.Alpha;
        }

        private void r3_CheckedChanged(object sender, EventArgs e)
        {
            GenType = iGenType.AlphaNumeric;
        }

        private void r2_CheckedChanged(object sender, EventArgs e)
        {
            GenType = iGenType.Numeric;
        }

        private void cmdMake_Click(object sender, EventArgs e)
        {
            int iLen = 0;
            int iPasswords = 0;

            try
            {
                iLen = Convert.ToInt32(txtLength.Text);
                if (iLen == 0) return;
            }

            catch
            {
                txtLength.Text = "8";
                ShowError("password length");
                return;
            }

            try
            {
                iPasswords = Convert.ToInt32(txtKeys.Text);
            }

            catch
            {
                txtKeys.Text = "10";
                ShowError("number of passwords");
                return;
            }

            Random Rnd = new Random();
            //Make passwords
            LstPws.Items.Clear();

            //Build password list
            for (int Counter = 0; Counter < iPasswords; Counter++)
            {
                LstPws.Items.Add(MakePassword(iLen, GenType, Rnd));
            }
        }

        private void cmdSave_Click(object sender, EventArgs e)
        {
            if (LstPws.Items.Count == 0)
            {
                MessageBox.Show("Nothing to save.",
                    "Nothing In List", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Title = "Save Passwords";
                sfd.Filter = "Text Files(*.txt)|*.txt";

                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    //Create the file
                    using (StreamWriter sw = new StreamWriter(sfd.FileName))
                    {
                        foreach (string Item in LstPws.Items)
                        {
                            //Write each password to the file
                            sw.WriteLine(Item);
                        }
                        sw.Close();
                    }
                }
            }
        }

        private void cmdAbout_Click(object sender, EventArgs e)
        {
            MessageBox.Show(this.Text + " Created 2010",
                "About", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void cmdExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void LstPws_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Show password in text box
            txtPws.Text = LstPws.SelectedItem.ToString();
            
        }

        private void label2_Click(object sender, EventArgs e)
        {
        
        }    
        }
        private void cmdCpy_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtPws.Text))
            {
                //Copy password to clipboard.
                Clipboard.SetText(txtPws.Text);
            }
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void txtLength_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtKeys_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            

        private void txtPws_TextChanged(object sender, EventArgs e)
        {

        }
    }



Is This A Good Question/Topic? 0
  • +

Replies To: Clear All Textbox

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 1957
  • View blog
  • Posts: 8,700
  • Joined: 29-May 08

Re: Clear All Textbox

Posted 21 August 2012 - 03:12 PM

This isn't VB6! It's not even VB.net! It's C#
I'll move it across.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1931
  • View blog
  • Posts: 5,757
  • Joined: 05-May 12

Re: Clear All Textbox

Posted 21 August 2012 - 04:00 PM

You'll be in for a really hard time learning VB, if you are writing code in C#.

Anyway, it looks like your code above doesn't even compile. button1_Click() seems to be missing a closing curly brace.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1931
  • View blog
  • Posts: 5,757
  • Joined: 05-May 12

Re: Clear All Textbox

Posted 21 August 2012 - 04:08 PM

The correct way to do this is to have your button click handler call a function call ClearAllTextBoxes(). And then in your ClearAllTextBoxes() function, you would set the Text property of each of your TextBoxes to an empty string.

Or you could just go the route most WinForms developers end up going and have the body of their click handler set the Text property of each of your Textboxes to an empty string. But an angry Klingon will come hunt me down if I don't dissuade you from going down this path. So please, consider writing the ClearAllTextBoxes() function.

If you wanted to be "cool" and write code that would work generically, you could iterate through all the controls recursively looking for Textboxes to clear, but that will probably have to wait until later once you've got more experience with whichever language you are learning.
Was This Post Helpful? 0
  • +
  • -

#5 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 192
  • Joined: 25-April 11

Re: Clear All Textbox

Posted 23 August 2012 - 02:25 AM

You could do something like this, but you have to put it into the function Skydiver told you to create.

foreach(Control control in this.Controls)
{
    if(control.GetType() == typeof(TextBox))
    {
        control.Text = "";
    }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1