本文中的输入框中的字符串是逗号隔开的,你可以换成其他特别的字符串.
本篇中的亮点:
1. 里面有一个玻璃样式按钮,用XAML制作
2. WPF下TextBox允许多行文本的设置
3. 使用Hashtable剔除重复字符串(比如电话号码)
运行时的样子:(为了保护隐私,做了虚化及其他处理)
WPF下TextBox允许多行文本的设置:
需要:
(1)将TextWrapping设为"Wrap"
(2)将AcceptsReturn设为"True"
(3)将VerticalScrollBarVisibility设置为"Visible"
具体参见这里:http://msdn2.microsoft.com/en-us/library/ms742157.aspx
下面XAML代码和C#代码使用Hashtable实现剔除以逗号隔开的字符串:
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="#FFFFFFFF"
x:Name="DocumentRoot"
x:Class="UntitledProject1.Scene1"
Width="640" Height="480">
关键的C#代码:
void GetTelNo(object sender, RoutedEventArgs e)
{
string tels = txtBoxTel.Text;
string[] telArr = tels.Split(new char[] { ','});
txtBoxTelCount.Text = telArr.Length.ToString();
Hashtable ht = new Hashtable();
Hashtable htRepeat = new Hashtable();
for (int i = 0; i < telArr.Length; i++)
{
try
{
ht.Add(telArr[i], telArr[i]);
}
catch
{
htRepeat.Add(i.ToString(), telArr[i]);
}
}
StringBuilder sb = new StringBuilder();
ICollection valueColl = ht.Values;
foreach (string s in valueColl)
{
sb.Append(s + ",");
}
StringBuilder sbRepeat = new StringBuilder();
ICollection valueCollRepeat = htRepeat.Values;
foreach (string s in valueCollRepeat)
{
sbRepeat.Append(s + ",");
}
txtBoxTel.Text = sb.ToString().TrimEnd(new char[]{','});
txtBoxRepeatTel.Text = sbRepeat.ToString().TrimEnd(new char[] { ',' });
}
完整的代码下载:(无法上传源码,试了几次都不行,唉....)
附: Window Forms的代码:
// Form1.Designer.cs
namespace BrawDraw.Com.HashtableFaxNoReplace
{
partial class Form1
{
///
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的资源。
///
/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
///
private void InitializeComponent()
{
this.btnGetTelNo = new System.Windows.Forms.Button();
this.txtBoxTel = new System.Windows.Forms.TextBox();
this.txtBoxRepeatTel = new System.Windows.Forms.TextBox();
this.txtBoxTelCount = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// btnGetTelNo
//
this.btnGetTelNo.Location = new System.Drawing.Point(489, 3);
this.btnGetTelNo.Name = "btnGetTelNo";
this.btnGetTelNo.Size = new System.Drawing.Size(57, 23);
this.btnGetTelNo.TabIndex = 0;
this.btnGetTelNo.Text = "转换";
this.btnGetTelNo.UseVisualStyleBackColor = true;
this.btnGetTelNo.Click += new System.EventHandler(this.btnGetTelNo_Click);
//
// txtBoxTel
//
this.txtBoxTel.Location = new System.Drawing.Point(5, 3);
this.txtBoxTel.MaxLength = 32767000;
this.txtBoxTel.Multiline = true;
this.txtBoxTel.Name = "txtBoxTel";
this.txtBoxTel.Size = new System.Drawing.Size(478, 272);
this.txtBoxTel.TabIndex = 1;
//
// txtBoxRepeatTel
//
this.txtBoxRepeatTel.Location = new System.Drawing.Point(5, 281);
this.txtBoxRepeatTel.Multiline = true;
this.txtBoxRepeatTel.Name = "txtBoxRepeatTel";
this.txtBoxRepeatTel.Size = new System.Drawing.Size(478, 190);
this.txtBoxRepeatTel.TabIndex = 2;
//
// txtBoxTelCount
//
this.txtBoxTelCount.Location = new System.Drawing.Point(490, 33);
this.txtBoxTelCount.Name = "txtBoxTelCount";
this.txtBoxTelCount.Size = new System.Drawing.Size(56, 21);
this.txtBoxTelCount.TabIndex = 3;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(558, 472);
this.Controls.Add(this.txtBoxTelCount);
this.Controls.Add(this.txtBoxRepeatTel);
this.Controls.Add(this.txtBoxTel);
this.Controls.Add(this.btnGetTelNo);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button btnGetTelNo;
private System.Windows.Forms.TextBox txtBoxTel;
private System.Windows.Forms.TextBox txtBoxRepeatTel;
private System.Windows.Forms.TextBox txtBoxTelCount;
}
}
// Form1.cs
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace BrawDraw.Com.HashtableFaxNoReplace
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnGetTelNo_Click(object sender, EventArgs e)
{
string tels = txtBoxTel.Text;
string[] telArr = tels.Split(new char[] { '/r' });
txtBoxTelCount.Text = telArr.Length.ToString();
Hashtable ht = new Hashtable();
Hashtable htRepeat = new Hashtable();
for (int i = 0; i < telArr.Length; i++)
{
try
{
// 这里将电话区号0755去掉,以便后面将所有Fax号码加上0755
ht.Add(telArr[i], telArr[i].Replace("0755", "").Replace("/n", ""));
}
catch
{
htRepeat.Add(i.ToString(), telArr[i].Replace("/n", ""));
}
}
StringBuilder sb = new StringBuilder();
ICollection valueColl = ht.Values;
foreach (string s in valueColl)
{
// 加上电话区号0755
sb.Append("0755" + s + ",");
}
StringBuilder sbRepeat = new StringBuilder();
ICollection valueCollRepeat = htRepeat.Values;
foreach (string s in valueCollRepeat)
{
sbRepeat.Append(s + ",");
}
txtBoxTel.Text = sb.ToString().TrimEnd(new char[] { ',' });
txtBoxRepeatTel.Text = sbRepeat.ToString().TrimEnd(new char[] { ',' });
}
}
}
// Program.cs
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace BrawDraw.Com.HashtableFaxNoReplace
{
static class Program
{
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}