本文介绍了如何在此代码中从表单文本框中检索所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取所有值,例如length和min length,字符串形式的范围,文本框的范围,请告诉我

i want to take all values like length and min length , range of string form form textbox, plz tell how

001.namespace ssp.PwdGen
002.{
003.  using System;
004.  using System.Security.Cryptography;
005.  using System.Text;
006. 
007.  public class PasswordGenerator
008.  {
009.    public PasswordGenerator()
010.    {
011.      this.Minimum         = DefaultMinimum;
012.      this.Maximum         = DefaultMaximum;
013.      this.ConsecutiveCharacters = false;
014.      this.RepeatCharacters    = true;
015.      this.ExcludeSymbols      = false;
016.      this.Exclusions        = null;
017. 
018.      rng = new RNGCryptoServiceProvider();
019.    }    
020.     
021.    protected int GetCryptographicRandomNumber(int lBound, int uBound)
022.    {  
023.      // Assumes lBound >= 0 && lBound < uBound
024. 
025.      // returns an int >= lBound and < uBound
026. 
027.      uint urndnum;  
028.      byte[] rndnum = new Byte[4];  
029.      if (lBound == uBound-1) 
030.      {
031.        // test for degenerate case where only lBound can be returned
032. 
033.        return lBound;
034.      }
035.                                 
036.      uint xcludeRndBase = (uint.MaxValue -
037.        (uint.MaxValue%(uint)(uBound-lBound)));  
038.       
039.      do
040.      {   
041.        rng.GetBytes(rndnum);   
042.        urndnum = System.BitConverter.ToUInt32(rndnum,0);   
043.      } while (urndnum >= xcludeRndBase);  
044.       
045.      return (int)(urndnum % (uBound-lBound)) + lBound;
046.    }
047. 
048.    protected char GetRandomCharacter()
049.    {       
050.      int upperBound = pwdCharArray.GetUpperBound(0);
051. 
052.      if ( true == this.ExcludeSymbols )
053.      {
054.        upperBound = PasswordGenerator.UBoundDigit;
055.      }
056. 
057.      int randomCharPosition = GetCryptographicRandomNumber(
058.        pwdCharArray.GetLowerBound(0), upperBound);
059. 
060.      char randomChar = pwdCharArray[randomCharPosition];
061. 
062.      return randomChar;
063.    }
064.     
065.    public string Generate()
066.    {
067.      // Pick random length between minimum and maximum  
068. 
069.      int pwdLength = GetCryptographicRandomNumber(this.Minimum,
070.        this.Maximum);
071. 
072.      StringBuilder pwdBuffer = new StringBuilder();
073.      pwdBuffer.Capacity = this.Maximum;
074. 
075.      // Generate random characters
076. 
077.      char lastCharacter, nextCharacter;
078. 
079.      // Initial dummy character flag
080. 
081.      lastCharacter = nextCharacter = ''\n'';
082. 
083.      for ( int i = 0; i < pwdLength; i++ )
084.      {
085.        nextCharacter = GetRandomCharacter();
086. 
087.        if ( false == this.ConsecutiveCharacters )
088.        {
089.          while ( lastCharacter == nextCharacter )
090.          {
091.            nextCharacter = GetRandomCharacter();
092.          }
093.        }
094. 
095.        if ( false == this.RepeatCharacters )
096.        {
097.          string temp = pwdBuffer.ToString();
098.          int duplicateIndex = temp.IndexOf(nextCharacter);
099.          while ( -1 != duplicateIndex )
100.          {
101.            nextCharacter = GetRandomCharacter();
102.            duplicateIndex = temp.IndexOf(nextCharacter);
103.          }
104.        }
105. 
106.        if ( ( null != this.Exclusions ) )
107.        {
108.          while ( -1 != this.Exclusions.IndexOf(nextCharacter) )
109.          {
110.            nextCharacter = GetRandomCharacter();
111.          }
112.        }
113. 
114.        pwdBuffer.Append(nextCharacter);
115.        lastCharacter = nextCharacter;
116.      }
117. 
118.      if ( null != pwdBuffer )
119.      {
120.        return pwdBuffer.ToString();
121.      }
122.      else
123.      {
124.        return String.Empty;
125.      }  
126.    }
127.       
128.    public string Exclusions
129.    {
130.      get { return this.exclusionSet;  }
131.      set { this.exclusionSet = value; }
132.    }
133. 
134.    public int Minimum
135.    {
136.      get { return this.minSize; }
137.      set
138.      {
139.        this.minSize = value;
140.        if ( PasswordGenerator.DefaultMinimum > this.minSize )
141.        {
142.          this.minSize = PasswordGenerator.DefaultMinimum;
143.        }
144.      }
145.    }
146. 
147.    public int Maximum
148.    {
149.      get { return this.maxSize; }
150.      set
151.      {
152.        this.maxSize = value;
153.        if ( this.minSize >= this.maxSize )
154.        {
155.          this.maxSize = PasswordGenerator.DefaultMaximum;
156.        }
157.      }
158.    }
159. 
160.    public bool ExcludeSymbols
161.    {
162.      get { return this.hasSymbols; }
163.      set { this.hasSymbols = value;}
164.    }
165. 
166.    public bool RepeatCharacters
167.    {
168.      get { return this.hasRepeating; }
169.      set { this.hasRepeating = value;}
170.    }
171. 
172.    public bool ConsecutiveCharacters
173.    {
174.      get { return this.hasConsecutive; }
175.      set { this.hasConsecutive = value;}
176.    }
177. 
178.    private const int DefaultMinimum = 6;
179.    private const int DefaultMaximum = 10;
180.    private const int UBoundDigit    = 61;
181. 
182.    private RNGCryptoServiceProvider    rng;
183.    private int       minSize;
184.    private int       maxSize;
185.    private bool        hasRepeating;
186.    private bool        hasConsecutive;
187.    private bool        hasSymbols;
188.    private string      exclusionSet;
189.    private char[] pwdCharArray = "abcdefghijklmnopqrstuvwxyzABCDEFG" +
190.      "HIJKLMNOPQRSTUVWXYZ0123456789`~!@#$%^&*()-_=+[]{}\\|;:''         \",<" +
      ".>/?".ToCharArray();                     
  }
}

推荐答案


int maxValue = 0;
if (int.TryParse(inputMaxValue.Text, out maxValue))
   {
   if (maxValue < 9999)
      {
      // Process.
      }
   else
      {
      // Error: max Value out of range
      }
   }
else
   {
   // Error: max value must be a number
   }

然后重复显示最小值和范围.

Then repeat for minimum, and range.


这篇关于如何在此代码中从表单文本框中检索所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 13:39