他们说的是对的!只是没有看清你写的代码!你的str_char 是静态的就不能直接那样访问的要写成:
this.rtbStochastic.Text = Stochastic.str_char(10,true);//通过类去访问
另外你的数组赋值写的不够好!最好用循环写进去:
int Setnum = 65;//A的Ascii码值是65
char Getletter;//获取Ascii对应的字母
char[] Pattern = new char[26];
for (int i = 0; i < 26; i++)
{
Getletter = (char)Setnum; //把Ascii值转换为对应的字母
Pattern[i] = Getletter;
Setnum = Setnum + 1;
}
下面的是我对你的程序做了改动后的代码希望对你有帮助:
namespace KinroStudy
{
public partial class Stochastic : Form
{
public Stochastic()
{
InitializeComponent();
}
#region & 成生随机数
public static string str_char(int Length, bool Sleep)
{
if (Sleep)
{
System.Threading.Thread.Sleep(3);//阻止线程指定时间
}
#region & 成生数组值
int Setnum = 65;//A的Ascii码值是65
char Getletter;//获取Ascii对应的字母
char[] Pattern = new char[26];
for (int i = 0; i < 26; i++)
{
Getletter = (char)Setnum;
Pattern[i] = Getletter;
Setnum = Setnum + 1;
}
#endregion
string Result = "";
int GetLength = Pattern.Length;
System.Random myrandom = new Random(~unchecked((int)DateTime.Now.Ticks));
for (int j = 0; j < Length; j++)
{
int Rnd = myrandom.Next(0, GetLength);
Result += Pattern[Rnd];
}
return Result;
}
#endregion
private void btngo_Click(object sender, EventArgs e)
{
int Setlength =this.textBox1.Text.ToString().Trim().Length;
if (Setlength > 26)
{
MessageBox.Show("请输入1-26的数字!");
this.textBox1.Clear();
this.textBox1.Focus();
}
else
{
this.rtbStochastic.Text = Stochastic.str_char(Setlength, true);
}
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
char key = e.KeyChar;
if (!char.IsDigit(key) && key != 8)
{
e.Handled = true;
;
}
}
}
}