📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Money
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Money : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Money()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(24, 56);
this.textBox1.MaxLength = 10;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(264, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(24, 112);
this.textBox2.Multiline = true;
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(264, 72);
this.textBox2.TabIndex = 1;
this.textBox2.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 208);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(112, 32);
this.button1.TabIndex = 2;
this.button1.Text = "转换";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(208, 16);
this.label1.TabIndex = 3;
this.label1.Text = "请输入数字:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 88);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(208, 16);
this.label2.TabIndex = 4;
this.label2.Text = "转换为大写中文:";
//
// contextMenu1
//
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem2});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.Text = "关于";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
//
// menuItem2
//
this.menuItem2.Index = 1;
this.menuItem2.Text = "退出";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// Money
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(328, 266);
this.ContextMenu = this.contextMenu1;
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Money";
this.Text = "数字转换为大写中文";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Money());
}
private char 转换数字(char x)
{
string stringChnNames="零一二三四五六七八九";
string stringNumNames="0123456789";
return stringChnNames[stringNumNames.IndexOf(x)];
}
private string 转换万以下整数(string x)
{
string[] stringArrayLevelNames=new string[4] {"","十","百","千"};
string ret="";
int i;
for (i=x.Length-1;i>=0;i--)
if (x[i]=='0')
ret=转换数字(x[i])+ret;
else
ret=转换数字(x[i])+stringArrayLevelNames[x.Length-1-i]+ret;
while ((i=ret.IndexOf("零零"))!=-1)
ret=ret.Remove(i,1);
if (ret[ret.Length-1]=='零' && ret.Length>1)
ret=ret.Remove(ret.Length-1,1);
if (ret.Length>=2 && ret.Substring(0,2)=="一十")
ret=ret.Remove(0,1);
return ret;
}
private string 转换整数(string x)
{
int len=x.Length;
string ret,temp;
if (len<=4)
ret=转换万以下整数(x);
else if (len<=8)
{
ret=转换万以下整数(x.Substring(0,len-4))+"万";
temp=转换万以下整数(x.Substring(len-4,4));
if (temp.IndexOf("千")==-1 && temp!="")
ret+="零"+temp;
else
ret+=temp;
}
else
{
ret=转换万以下整数(x.Substring(0,len-8))+"亿";
temp=转换万以下整数(x.Substring(len-8,4));
if (temp.IndexOf("千")==-1 && temp!="")
ret+="零"+temp;
else
ret+=temp;
ret+="万";
temp=转换万以下整数(x.Substring(len-4,4));
if (temp.IndexOf("千")==-1 && temp!="")
ret+="零"+temp;
else
ret+=temp;
}
int i;
if ((i=ret.IndexOf("零万"))!=-1)
ret=ret.Remove(i+1,1);
while ((i=ret.IndexOf("零零"))!=-1)
ret=ret.Remove(i,1);
if (ret[ret.Length-1]=='零' && ret.Length>1)
ret=ret.Remove(ret.Length-1,1);
return ret;
}
private string 转换小数(string x)
{
string ret="";
for (int i=0;i<x.Length;i++)
ret+=转换数字(x[i]);
return ret;
}
public string NumToChn(string x)
{
if (x.Length==0)
return "";
string ret="";
if (x[0]=='-')
{
ret="负";
x=x.Remove(0,1);
}
if (x[0].ToString()==".")
x="0"+x;
if (x[x.Length-1].ToString()==".")
x=x.Remove(x.Length-1,1);
if (x.IndexOf(".")>-1)
ret+=转换整数(x.Substring(0,x.IndexOf(".")))+"点"+转换小数(x.Substring(x.IndexOf(".")+1));
else
ret+=转换整数(x);
return ret;
}
private void button1_Click(object sender, System.EventArgs e)
{
if(textBox1.Text=="")
{
MessageBox.Show(this," 请输入阿拉伯数字! ");
}
textBox2.Text=NumToChn( textBox1.Text );
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
MessageBox.Show(this,"sunboy!!! ");
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -