Combox selectitem添加了额外的字母

时间:2017-02-24 20:43:42

标签: c# winforms

我是C#的新手,并试图了解事情是如何运作的。所以我创建了两个表单。第一个有一个文本框,第二个有一个带有按钮的组合框,可以向该计算机发送提供帮助。从文本框中添加计算机名称。单击“确定”按钮后,它会将我的所有计算机名称加载到组合框中。

string[] computerlist = txtComputers.Text.Split(new[]{'\n'}, StringSplitOptions.RemoveEmptyEntries); 
frmHome _frmhome = new frmHome();
_frmhome.cbComputerList.Items.AddRange(computerlist);
_frmhome.ShowDialog();
_frmhome.Dispose();

当我从Dropbox中选择一台计算机并单击Offer_help按钮时,会出现提供远程窗口,说它尝试连接到该用户但是然后失败。

private void Offerhelp_Click(object sender, EventArgs e)
 {
    CompName = cbComputerList.SelectedItem.ToString();
    var _offerhelp = new ProcessStartInfo();
    _offerhelp.FileName = "msra.exe";
    _offerhelp.Arguments = String.Format("/offerRA" + " " + CompName);
    Process.Start(_offerhelp);
  }

我尝试在调试模式下运行,我看到" CompName"变量是

"/offerRA Lab1\r"

如果我删除" \ r"它确实有效。

谁能告诉我为什么会这样?另外,有没有办法我可以为所选项创建一个新类,并使其成为一个全局变量,所以我可以使用它说如果我创建4-5个表单并在所有表单中使用该计算机名称?

提前致谢。

1 个答案:

答案 0 :(得分:1)

你的行

string[] computerlist = txtComputers.Text.Split(new[]{'\n'}, StringSplitOptions.RemoveEmptyEntries);

是问题吗? \n是换行符,\r是回车符。根据操作系统/程序,您可以使用\r\n来确定“NewLine”。

使用

string[] computerlist = txtComputers.Text.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

Environment.NewLine将返回正确的字符。

编辑:就评论而言,请在表单中添加静态属性:

class MyForm
{
    public static string SelectedComputer { get; set;}
}

然后你可以通过

在任何地方引用它
MyForm.SelectedComputer

cbComputerList.SelectedIndexChanged事件中设置此变量。只需检查以确保该值大于0然后设置它。