摇奖程序如何去掉空行

qin1826681768 2012-04-24 07:07:38
在做一个摇奖程序,但是在从textbox输入时如果多按了一次回车键就会在listbox里多一行空行,请问如何去除空行?


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int i,j;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
j = 0;
}

private void button1_Click(object sender, EventArgs e)
{
string str = textBox1.Text.ToString();
string news = str.Replace( " ","" ); //去除全部空格
for (i = 0; i < listBox1.Items.Count;i++ )
{

if (news == listBox1.Items[i].ToString())
{
//if (textBox1.Text == " ")
//{ listBox1.Items.RemoveAt(i); }

listBox1.Items.RemoveAt(i);
}
}
//if (textBox1.Text == " ")
//{ textBox1.Text = ""; }
//else
//{
listBox1.Items.Add(news);
textBox1.Text = null;
i = listBox1.Items.Count;
//}

}

private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Interval = 100;
}

private void timer1_Tick(object sender, EventArgs e)
{
j++;
if (j < i)
{

label2.Text = listBox1.Items[j].ToString();

}

else
j = 0;

}

private void button3_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode==Keys.Enter)
{
string str = textBox1.Text.ToString();
string news = str.Replace(" ", "");
for (i = 0; i < listBox1.Items.Count; i++)
{

if (news == listBox1.Items[i].ToString())
{
listBox1.Items.RemoveAt(i);
}
}

listBox1.Items.Add(news);
textBox1.Text = null;
//i = listBox1.Items.Count;
}
}
}
}
...全文
88 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
orochiheart 2012-04-24
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]
C# code
string news = str.Trim( new char[] { '\r','\n'} )
[/Quote]
嗯不错 学习了!!
烈火蜓蜻 2012-04-24
  • 打赏
  • 举报
回复
string news = str.Trim( new char[] { '\r','\n'} )
orochiheart 2012-04-24
  • 打赏
  • 举报
回复
那再试试
string s1 = s.Replace("\n","");

string s1 = s.Replace("\r","");
orochiheart 2012-04-24
  • 打赏
  • 举报
回复
搞错了 应该是这样

string s = textBox1.Text.ToString();
string s1 = s.Replace("\r\n","");
orochiheart 2012-04-24
  • 打赏
  • 举报
回复
观察你取出来的字符串 看看里面多了什么 比如 \r \n \r\n
然后用Replace 替换成空就可以了

string s = textBox1.Text.ToString();
string s1 = Replace(s,"");


mizuho_2006 2012-04-24
  • 打赏
  • 举报
回复
判断如果有两个\r字符的话就去掉一个

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧