Hi all,

I was wondering how I would use a single text box for multiple things

eg. In one text box I would type 'createfile file.txt'

or something like 'stopprocess process' etc.

How would I do that?

Thanks

Recommended Answers

All 9 Replies

This sounds like a horrible idea but to answer your question:

private void button5_Click(object sender, EventArgs e)
    {
      string[] args;

      if (string.IsNullOrEmpty(textBox1.Text))
        return;
      else
        args = textBox1.Text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

      string cmd = args[0].ToLower();
      switch (cmd)
      {
        case "createfile":
          if (args.Length == 2)
            System.IO.File.WriteAllBytes(args[1], new byte[0]);
          else
            MessageBox.Show("Please supply a file name");
          break;
        case "stopprocess":
          if (args.Length == 2)
          {
            System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName(args[1]);
            foreach (System.Diagnostics.Process p in procs)
              p.Kill();
          }
          else
          {
            MessageBox.Show("Please supply a process name");
          }
          break;
        default:
          MessageBox.Show(string.Format("Unknown command {0}", cmd));
          break;
      }

    }

Could you write this with instead of casing but with trimming?

Thank you

Yes, you could.

How can I trim a string with another string?

ie

string msg;
string mmsg;

if(msg.StartsWith("g_start") == true){
                    string trimString = "'g_start";
                    mmsg = msg.TrimStart(trimString);
                    MessageBox.Show(mmsg);}

Something like that, yakno? Or can I trim x ammount of characters from a string

ie

msg.TrimStart(5); //Trims 5 characters off the beginning of msg

Thanks

Why bother trimming it? You want to split on a space like I did in the first example. You can use string.Replace() and substitute the command with string.empty effectively removing it. This is a roundabout way to accomplish this task though. What are you trying to do?

Basically what you did, but in my own simplified little way.

Basically I have where it sees that I typed 'g_start', trims away the 'g_start ' and runs the line to start what was behind that command.

ie

Control > g_start calc

Program > Sees g_start, runs if statement. Trims away g_start , puts the posterior of the command (calc) into mmsg, and runs System.Diagnostics.Process.Start(mmsg);

There you have it.

So has your question been answered or do you still need some assistance? If it has been answered please mark this thread as solved, if not then please clarify what you are looking for and we'll go from there.

Either way -- Good luck!

Thanks for devoting your help towards me.

My current problem -

            if(msg.StartsWith("c_start") == true){
                msg.Replace("c_start ",string.Empty);
                mmsg = msg.Trim();
                MessageBox.Show(mmsg);
            }

It doesn't even recognize c_start.

Any ideas? and maybe a fix too?

In your first example you used "g_start" and in this example you used "c_start". Is that possibly your issue? Either way you should create "delegate" methods to pass the commands down.

private void button5_Click(object sender, EventArgs e)
    {
      //Now we have `g_start createfile c:\file.txt`
      ProcessCommand(textBox1.Text);
    }

    private void ProcessCommand(string cmd)
    {
      //for this example we `g_start createfile c:\file.txt`
      if (string.IsNullOrEmpty(cmd))
        return;
      string[] split = cmd.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
      if (split.Length == 0)
        return; //they didnt have any following arguments so its not valid
      string switcher = split[0].Trim().ToLower();
      switch (switcher)
      {
        case "g_start":
          {
            List<string> lst = split.ToList();
            lst.RemoveAt(0); //remove g_start, the first element in the array
            ProcessGStart(lst.ToArray());
          }
          break;
        case "g_something":
          {
            List<string> lst = split.ToList();
            lst.RemoveAt(0); //remove g_something, the first element in the array
            ProcessGSomething(lst.ToArray());
          }
          break;
        default:
          return;
          //break;
      }
    }

    private void ProcessGStart(string[] cmd)
    {
      //At this point we have `createfile c:\file.txt`

      string switcher = cmd[0].ToLower().Trim();
      switch (switcher)
      {
        case "createfile":
          if (cmd.Length == 2)
            System.IO.File.WriteAllBytes(cmd[1], new byte[0]);
          else
            MessageBox.Show("Please supply a file name");
          break;
        case "stopprocess":
          if (cmd.Length == 2)
          {
            System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName(cmd[1]);
            foreach (System.Diagnostics.Process p in procs)
              p.Kill();
          }
          else
          {
            MessageBox.Show("Please supply a process name");
          }
          break;
        default:
          MessageBox.Show(string.Format("Unknown command {0}", cmd));
          break;
      }
    }

    private void ProcessGSomething(string[] cmd)
    {
      //This is just to show how you can delegate different methods based on the command prefix
      throw new NotImplementedException();
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.