界面

代码
using System;
using System.Windows.Forms;
namespace CaseConverterTool
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitializeUI();
}
private void InitializeUI()
{
// 设置窗口标题和大小
this.Text = "英文字母大小写转换工具";
this.Size = new System.Drawing.Size(600, 400);
this.StartPosition = FormStartPosition.CenterScreen;
// 创建输入标签
Label inputLabel = new Label();
inputLabel.Text = "请输入需要转换的文本:";
inputLabel.Location = new System.Drawing.Point(20, 20);
inputLabel.AutoSize = true;
this.Controls.Add(inputLabel);
// 创建输入文本框
TextBox inputTextBox = new TextBox();
inputTextBox.Multiline = true;
inputTextBox.ScrollBars = ScrollBars.Vertical;
inputTextBox.Location = new System.Drawing.Point(20, 45);
inputTextBox.Size = new System.Drawing.Size(540, 100);
inputTextBox.Name = "inputTextBox";
this.Controls.Add(inputTextBox);
// 创建按钮面板
Panel buttonPanel = new Panel();
buttonPanel.Location = new System.Drawing.Point(20, 160);
buttonPanel.Size = new System.Drawing.Size(540, 40);
buttonPanel.Name = "buttonPanel";
this.Controls.Add(buttonPanel);
// 创建转大写按钮
Button toUpperButton = new Button();
toUpperButton.Text = "转大写";
toUpperButton.Location = new System.Drawing.Point(10, 5);
toUpperButton.Size = new System.Drawing.Size(120, 30);
toUpperButton.Name = "toUpperButton";
toUpperButton.Click += (sender, e) =>
{
if (this.Controls["inputTextBox"] is TextBox input &&
this.Controls["outputTextBox"] is TextBox output)
{
output.Text = input.Text.ToUpper();
}
};
buttonPanel.Controls.Add(toUpperButton);
// 创建转小写按钮
Button toLowerButton = new Button();
toLowerButton.Text = "转小写";
toLowerButton.Location = new System.Drawing.Point(150, 5);
toLowerButton.Size = new System.Drawing.Size(120, 30);
toLowerButton.Name = "toLowerButton";
toLowerButton.Click += (sender, e) =>
{
if (this.Controls["inputTextBox"] is TextBox input &&
this.Controls["outputTextBox"] is TextBox output)
{
output.Text = input.Text.ToLower();
}
};
buttonPanel.Controls.Add(toLowerButton);
// 创建大小写互换按钮
Button swapCaseButton = new Button();
swapCaseButton.Text = "大小写互换";
swapCaseButton.Location = new System.Drawing.Point(290, 5);
swapCaseButton.Size = new System.Drawing.Size(120, 30);
swapCaseButton.Name = "swapCaseButton";
swapCaseButton.Click += (sender, e) =>
{
if (this.Controls["inputTextBox"] is TextBox input &&
this.Controls["outputTextBox"] is TextBox output)
{
output.Text = SwapCase(input.Text);
}
};
buttonPanel.Controls.Add(swapCaseButton);
// 创建复制结果按钮
Button copyButton = new Button();
copyButton.Text = "复制结果";
copyButton.Location = new System.Drawing.Point(430, 5);
copyButton.Size = new System.Drawing.Size(100, 30);
copyButton.Name = "copyButton";
copyButton.Click += (sender, e) =>
{
if (this.Controls["outputTextBox"] is TextBox output && !string.IsNullOrEmpty(output.Text))
{
Clipboard.SetText(output.Text);
MessageBox.Show("已复制到剪贴板!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
};
buttonPanel.Controls.Add(copyButton);
// 创建输出标签
Label outputLabel = new Label();
outputLabel.Text = "转换结果:";
outputLabel.Location = new System.Drawing.Point(20, 210);
outputLabel.AutoSize = true;
this.Controls.Add(outputLabel);
// 创建输出文本框
TextBox outputTextBox = new TextBox();
outputTextBox.Multiline = true;
outputTextBox.ScrollBars = ScrollBars.Vertical;
outputTextBox.Location = new System.Drawing.Point(20, 235);
outputTextBox.Size = new System.Drawing.Size(540, 100);
outputTextBox.Name = "outputTextBox";
outputTextBox.ReadOnly = true;
this.Controls.Add(outputTextBox);
}
/// <summary>
/// 实现大小写互换功能
/// </summary>
private string SwapCase(string input)
{
char[] chars = input.ToCharArray();
for (int i = 0; i < chars.Length; i++)
{
if (char.IsUpper(chars[i]))
{
chars[i] = char.ToLower(chars[i]);
}
else if (char.IsLower(chars[i]))
{
chars[i] = char.ToUpper(chars[i]);
}
// 非字母字符不做处理
}
return new string(chars);
}
}
}