目录
????????将数字的字符串表示形式转换为它的等效 32 位有符号整数。 一个指示转换是否成功的返回值。
public static bool TryParse (string? s, out int result);
参数s String
包含要转换的数字的字符串。
result Int32
当此方法返回时,如果转换成功,则包含与 s 中所包含的数字等效的 32 位无符号整数值;如果转换失败,则包含零。 如果参数为 null 或 Empty,格式不正确,或表示小于 Int32.MinValue 或大于 Int32.MaxValue 的数字,则转换失败。s 此参数未经初始化即进行传递;最初在 result 中提供的任何值都会被覆盖。
返回Boolean
如果 true 成功转换,则为 s;否则为 false。
// TryParse(String, Int32)
// 用异或运算法对数字加密解密
namespace _018
{
public partial class Form1 : Form
{
private GroupBox? groupBox1;
private GroupBox? groupBox2;
private Button? button1;
private TextBox? textBox3;
private TextBox? textBox2;
private TextBox? textBox1;
private Label? label3;
private Label? label2;
private Label? label1;
private TextBox? textBox4;
private Label? label4;
private Button? button2;
public Form1()
{
InitializeComponent();
Load += Form1_Load;
}
private void Form1_Load(object? sender, EventArgs e)
{
//
// button1
//
button1 = new Button
{
Location = new Point(125, 106),
Name = "button1",
Size = new Size(75, 23),
TabIndex = 6,
Text = "开始加密",
UseVisualStyleBackColor = true
};
button1.Click += Button1_Click;
//
// textBox3
//
textBox3 = new TextBox
{
Location = new Point(100, 77),
Name = "textBox3",
Size = new Size(100, 23),
TabIndex = 5
};
//
// textBox2
//
textBox2 = new TextBox
{
Location = new Point(100, 48),
Name = "textBox2",
Size = new Size(100, 23),
TabIndex = 4
};
//
// textBox1
//
textBox1 = new TextBox
{
Location = new Point(100, 19),
Name = "textBox1",
Size = new Size(100, 23),
TabIndex = 3
};
//
// label3
//
label3 = new Label
{
AutoSize = true,
Location = new Point(16, 83),
Name = "label3",
Size = new Size(43, 17),
TabIndex = 2,
Text = "加密后数字:"
};
//
// label2
//
label2 = new Label
{
AutoSize = true,
Location = new Point(16, 54),
Name = "label2",
Size = new Size(43, 17),
TabIndex = 1,
Text = "输入加密数字:"
};
//
// label1
//
label1 = new Label
{
AutoSize = true,
Location = new Point(16, 25),
Name = "label1",
Size = new Size(43, 17),
TabIndex = 0,
Text = "输入数字:"
};
//
// textBox4
//
textBox4 = new TextBox
{
Location = new Point(99, 22),
Name = "textBox4",
Size = new Size(100, 23),
TabIndex = 9
};
//
// label4
//
label4 = new Label
{
AutoSize = true,
Location = new Point(16, 28),
Name = "label4",
Size = new Size(43, 17),
TabIndex = 8,
Text = "解密后数字:"
};
//
// button2
//
button2 = new Button
{
Location = new Point(124, 54),
Name = "button2",
Size = new Size(75, 23),
TabIndex = 7,
Text = "开始解密",
UseVisualStyleBackColor = true
};
button2.Click += Button2_Click;
//
// groupBox1
//
groupBox1 = new GroupBox
{
Location = new Point(12, 12),
Name = "groupBox1",
Size = new Size(205, 140),
TabIndex = 0,
TabStop = false,
Text = "加密:"
};
groupBox1.Controls.Add(button1);
groupBox1.Controls.Add(textBox3);
groupBox1.Controls.Add(textBox2);
groupBox1.Controls.Add(textBox1);
groupBox1.Controls.Add(label3);
groupBox1.Controls.Add(label2);
groupBox1.Controls.Add(label1);
groupBox1.SuspendLayout();
//
// groupBox2
//
groupBox2 = new GroupBox
{
Location = new Point(12, 156),
Name = "groupBox2",
Size = new Size(205, 80),
TabIndex = 0,
TabStop = false,
Text = "解密:"
};
groupBox2.Controls.Add(textBox4);
groupBox2.Controls.Add(label4);
groupBox2.Controls.Add(button2);
groupBox2.SuspendLayout();
//
// Form1
//
AutoScaleDimensions = new SizeF(7F, 17F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(224, 251);
Controls.Add(groupBox2);
Controls.Add(groupBox1);
Name = "Form1";
StartPosition = FormStartPosition.CenterScreen;
Text = "Form1";
groupBox1.ResumeLayout(false);
groupBox1.PerformLayout();
groupBox2.ResumeLayout(false);
groupBox2.PerformLayout();
ResumeLayout(false);
}
/// <summary>
/// 加密
/// </summary>
private void Button1_Click(object? sender, EventArgs e)
{
if (int.TryParse(textBox1!.Text, out int P_int_Num) //内联变量并判断输入是否是数值
&& int.TryParse(textBox2!.Text, out int P_int_Key))
{
textBox3!.Text = (P_int_Num ^ P_int_Key).ToString(); //加密数值
}
else
{
MessageBox.Show("请输入数值", "出现错误!"); //提示输入信息不正确
}
}
/// <summary>
/// 解密
/// </summary>
private void Button2_Click(object? sender, EventArgs e)
{
if (int.TryParse(textBox3!.Text, out int P_int_Key) //内联变量并判断输入是否是数值
&& int.TryParse(textBox2!.Text, out int P_int_Encrypt))
{
textBox4!.Text = (P_int_Encrypt ^ P_int_Key).ToString(); //解密数值
}
else
{
MessageBox.Show("请输入数值", "出现错误!"); //提示输入信息不正确
}
}
}
}
?