使用“SelectedItem.Tostring()”时出现奇怪的空格; [英] Strange space appears when using "SelectedItem.Tostring()";
问题描述
编辑:请阅读结尾如果这篇文章
您好,
我有一个小到目前为止,我的代码存在问题:我目前正在使用命令
txtnume.Text = listbox1.SelectedItem.ToString();其中 txtnume 是文本框和 listbox1 是一个列表框。
这么好,到目前为止。
但是,如果我试图复制来自的文本txtnume,我会得到很多空间:\而且我真的不知道如何摆脱它。
以下是一些截图:
& http://tinypic.com/r/t4uzb9/8< - image 1
& http://tinypic.com/r/2dtnm0m/8< - 图像2
private void listBox2_SelectedIndexChanged(object sender,EventArgs e)
{
ListBox l = sender作为ListBox;
if(l.SelectedIndex!= -1)
{
listBox1.SelectedIndex = l.SelectedIndex;
listBox2.SelectedIndex = l.SelectedIndex;
txtnume.Text =;
txtprenume.Text =;
txtnume.Text = listBox1.SelectedItem.ToString();
txtprenume.Text = listBox2.SelectedItem.ToString();
}
}
这是我用来编写所选代码的代码项目进入我的文本框。我还需要在 - >的文本框中写下该项目。 image2
你能帮帮我吗?
祝福,
SnuKies
编辑:
我的完整代码:
http://www.codeshare.io/N1K72 [ ^ ]
EDIT2:我已经解决了这个问题:我将名称存储在字符串类型的nvchar(50)中,这会导致我的问题。现在,我已经将类型更改为ntext,我还有另一个问题:cmd.ExecuteNonQuery();< - 每次我尝试删除项目时都会出错。错误的文本是:数据类型ntext和varchar在等于运算符中不兼容。
如果您的问题是任何前导或尾随空格被注入通过键盘等任何东西 - >我建议在使用之前修剪文本值。
string numeVal = txtnume.Text.Trim();
如果值为空,则Trim将最少留下你的是String.Empty。 :)
EDIT: PLEASE read the end if this article
Hello,
I have a small problem with my code, so far: I'm currently using the command
"txtnume.Text = listbox1.SelectedItem.ToString(); " where txtnume is the name of a textbox and listbox1 is a listbox.
So good, so far.
But, if I'll try to copy the text from txtnume, I'll get a lot of space :\ and I really do not know how to get rid of it.
Here are some screenshots:
&http://tinypic.com/r/t4uzb9/8 <- image 1
&http://tinypic.com/r/2dtnm0m/8 <- image 2
private void listBox2_SelectedIndexChanged(object sender, EventArgs e) { ListBox l = sender as ListBox; if (l.SelectedIndex != -1) { listBox1.SelectedIndex = l.SelectedIndex; listBox2.SelectedIndex = l.SelectedIndex; txtnume.Text = ""; txtprenume.Text = ""; txtnume.Text = listBox1.SelectedItem.ToString(); txtprenume.Text = listBox2.SelectedItem.ToString(); } }
this is the code i've been using to write the selected item into my textbox. I also need to write that item in the textbox from -> image2
Could you help me, please?
Best wishes,
SnuKies
EDIT:
My full code:
http://www.codeshare.io/N1K72[^]
EDIT2: I've solved that problem: I'd store the name in a string-type nvchar(50) and that would cause me that problem. Now, that i've changed the type to ntext, I have another problem: cmd.ExecuteNonQuery();<- it gives me an error everytime i try to delete an item. The error's text is: "The data types ntext and varchar are incompatible in the equal to operator."
If your issue is any leading or trailing spaces that get injected by anything like keyboard, etc. -> I would recommend trimming the text values before using them.
string numeVal = txtnume.Text.Trim();
If the value is empty, the least the Trim will leave you with is String.Empty. :)
这篇关于使用“SelectedItem.Tostring()”时出现奇怪的空格;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!