为什么下面的代码解密按钮后没有输出到文本框 using System; using System.Windows.Forms; namespace VigenereCipher { public partial class MainForm : Form { public MainForm() { InitializeComponent(); } private void encryptButton_Click(object sender, EventArgs e) { string text = inputTextBox.Text; string key = keyTextBox.Text; string cipherText = Encrypt(text, key); outputTextBox.Text = cipherText; } private void decryptButton_Click(object sender, EventArgs e) { string text = inputTextBox.Text; string key = keyTextBox.Text; string plainText = Decrypt(text, key); outputTextBox.Text = plainText; } static string Encrypt(string text, string key) { string cipherText = ""; int keyIndex = 0; for (int i = 0; i < text.Length; i++) { char c = text[i]; int offset = key[keyIndex] - 'A'; char encryptedChar = (char)((c + offset) % 256); cipherText += encryptedChar; keyIndex = (keyIndex + 1) % key.Length; } return cipherText; } static string Decrypt(string text, string key) { string plainText = ""; int keyIndex = 0; for (int i = 0; i < text.Length; i++) { char c = text[i]; int offset = key[keyIndex] - 'A'; char decryptedChar = (char)((c - offset + 256) % 256); plainText += decryptedChar; keyIndex = (keyIndex + 1) % key.Length; } return plainText; } } }
时间: 2024-01-18 19:02:04 浏览: 28
这段代码实现了维吉尼亚密码的加密和解密功能,其中Encrypt函数实现了加密,Decrypt函数实现了解密。该程序界面中有三个文本框,分别是输入文本框、密钥文本框和输出文本框。当用户在输入文本框中输入明文并在密钥文本框中输入密钥后,点击加密按钮,程序会使用维吉尼亚密码对明文进行加密,并将结果输出到输出文本框中。当用户在输入文本框中输入密文并在密钥文本框中输入密钥后,点击解密按钮,程序会使用维吉尼亚密码对密文进行解密,并将结果输出到输出文本框中。
相关问题
将上述程序填写到下面的程序里using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { } } }
好的,将上述C# Switch程序填写到Windows窗体应用程序中的button1_Click事件中,可以这样写:
```csharp
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine("请选择一个数字(1~3):");
int num = int.Parse(Console.ReadLine());
switch (num)
{
case 1:
MessageBox.Show("您选择了数字1。");
break;
case 2:
MessageBox.Show("您选择了数字2。");
break;
case 3:
MessageBox.Show("您选择了数字3。");
break;
default:
MessageBox.Show("您输入的数字不在范围内。");
break;
}
}
}
}
```
这个程序会在窗体中添加一个按钮,当用户点击该按钮时,会弹出一个提示框,要求用户输入一个数字。然后使用switch语句根据用户的选择输出相应的信息。如果用户输入的数字不在1~3的范围内,则输出一个默认的提示信息。
c# System.Windows.Forms.Application.OpenForms 获取应用的最上面的窗体
C#中可以使用`System.Windows.Forms.Application.OpenForms`属性获取当前应用程序中所有已打开的窗体,其中索引为0的元素就是应用程序中最上层的窗体,也就是当前活动窗体。
以下是示例代码:
```csharp
using System;
using System.Windows.Forms;
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (Application.OpenForms.Count > 0)
{
Form topForm = Application.OpenForms[0];
MessageBox.Show("最上层窗体的名称是:" + topForm.Name);
}
else
{
MessageBox.Show("当前应用程序中没有打开的窗体!");
}
}
}
}
```
在上面的代码中,我们使用了`Application.OpenForms`属性获取当前应用程序中所有已打开的窗体。然后,我们判断该属性返回的窗体数量是否大于0,如果大于0,则取出索引为0的元素,也就是最上层的窗体,并使用MessageBox.Show方法显示该窗体的名称。如果该属性返回的窗体数量为0,则显示一个提示信息。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)