且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用“SelectedItem.Tostring()”时出现奇怪的空格;

更新时间:2022-10-16 12:11:13

如果您的问题是任何前导或尾随空格被注入通过键盘等任何东西 - >我建议在使用之前修剪文本值。



  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. :)