【问题标题】:string input from textbox if/else来自文本框的字符串输入 if/else
【发布时间】:2013-07-14 12:19:18
【问题描述】:

我正在尝试将来自文本框的输入用作字符串。问题是我需要 它仅在填充框时使用它。我的目标是允许用户在框中输入用户名,如果框为空,我希望它尽可能使用我的全局静态字符串。 如果有更好的想法,我会支持他们

我的程序使用 USERNAME 变量来获取用户名,但我希望用户能够在需要时在框中输入用户名。

string username = (Environment.GetEnvironmentVariable("USERNAME")); 

谢谢

public partial class Form1 : Form
    {

//My original strings, This is what i need to fix

       static string config = File.ReadAllText("config.ini");
       static string username = (Environment.GetEnvironmentVariable("USERNAME"));
       static string Backups = config + @"\" + username + @"\" + "Backups" + @"\";
       static string items;

        public Form1()
        {
            InitializeComponent();

        }

// How would i re purpose the string depending on the if?
  if (!String.IsNullOrEmpty(toolStripTextBox1.Text))
            {
                toolStripTextBox1.Text = username;
                MessageBox.Show(username);

            }
            else
            {
                string username = (Environment.GetEnvironmentVariable("USERNAME"));

            }

【问题讨论】:

  • 为什么这不起作用?对我来说看起来不错......
  • 你在做同样的仪式吗??

标签: c# .net windows .net-3.5


【解决方案1】:

如果toolStripTextBox1 不为空,则您将username 字符串中的值放入toolStripTextBox1 .. ? 在我看来,来自 IF 和 Else 的值都是相同的,即从您的配置文件中获取的用户名值。

所以我不确定我是否正确理解了你的问题,但我猜你想要这样的东西:

 if (!String.IsNullOrEmpty(toolStripTextBox1.Text))
    {
      username = toolStripTextBox1.Text;
     MessageBox.Show(username); // will be the username the user enters in the textbox
     }
      else
    {
      MessageBox.Show(username); // will be the username taken from your config file
    }

【讨论】:

    【解决方案2】:

    如果我理解你的正确,这就是你的问题。

     if (!String.IsNullOrEmpty(toolStripTextBox1.Text))
                {
                    toolStripTextBox1.Text = username;
    

    应该是

    string username = toolStripTextBox1.Text;
    

    【讨论】:

      猜你喜欢
      • 2013-02-05
      • 2018-03-25
      • 2014-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-14
      • 2021-07-06
      • 2018-03-09
      相关资源
      最近更新 更多