0

Just a C# beginner here that just build his first application and now wants to make some changes to make it more ideal.

Today I got three textboxes where the users type in year, month and project number. When they click GO the application opens the respective folder: L:\2019\01\20190133

I would like to make it simpler, one textbox that gets divided into three strings (year, month, PO-number).

Any ideas? Sorry for the bad format of this post, feel free to correct/shame me :)

Current code:

    private void button1_Click(object sender, System.EventArgs e)
    {
        int tbyear;
        int tbmonth;
        int tbpnr;

        tbyear = int.Parse(textBox1.Text);
        tbmonth = int.Parse(textBox2.Text);
        tbpnr = int.Parse(textBox3.Text);

        string tby = textBox1.Text;
        string tbm = textBox2.Text;
        string tbnr = textBox3.Text;
        string path = Path.Combine(tby, tbm);
        string pathnr = textBox1.Text + textBox2.Text + textBox3.Text;
        
        System.Diagnostics.Process.Start("explorer.exe", @"L:\" + path + "\\" + pathnr);
4
  • When a user types in the combined information into the single text box, how do you expect the user to format the input? What characters are allowed in the input? Commented Nov 10, 2020 at 16:22
  • @gunr2171: The input would be 20190133 in the case above. Commented Nov 10, 2020 at 16:24
  • So the first 4 characters are the year. Characters 5 and 6 are the month. The rest of the characters are the PO number. Do you know about substring? Commented Nov 10, 2020 at 16:25
  • @gunr2171: That looks like exactly what I need. Thanks! Commented Nov 10, 2020 at 16:29

3 Answers 3

3

As a very simple example of text extraction, use Substring.

string textFromInputControl = "20190133";
        
string year = textFromInputControl.Substring(0, 4); // start at index 0, 4 chars long
string month = textFromInputControl.Substring(4, 2); // start at index 4, 2 chars long
string poNumber = textFromInputControl.Substring(6); // start at index 6, all remaining chars
        
Console.WriteLine(year);     // "2019"
Console.WriteLine(month);    // "01"
Console.WriteLine(poNumber); // "33"

There are, of course, more complex ways of doing text extraction, like Regex. Use other tools when they are more appropriate.

Sign up to request clarification or add additional context in comments.

1 Comment

It works perfectly! Thanks for next-level response time and good advice. Learned a lot that I probably should have fund by myself through Google..
0

I think that it's fine as it is. If you have 3 differents inputs in 3 differents textboxes it's well designed. Imagine that one day you make a change and you don't want to join these types for generate your path: year\month\projectNumber. This would implies to change UI + Code. If you keep your input in only 1 text box, the changes only affect to code.

1 Comment

A very legitimate reason to have one text box is when you are scanning a barcode, and the barcode contains all the information in one string.
0

I'm not so sure that entering all three items into one textbox is simpler from a user's perspective, but if you want to do that here's one way:

This assumes that the entries are separated in some way (we'll go with a space character).

var parts = textbox.Text.Split(' ');
var pathToOpen = $@"L:\{parts[0]}\{parts[1]}\{parts[2]}";

The above code splits the contents of the textbox by its ' ' delimiter, then formats them into a path in the format you provided.

Edit: going by the comments, you don't want to have a delimiter. In the case that your first two parts of the path are of fixed length, you can use the Substring method to extract them:

var year = textbox.Text.Substring(0, 4);
var month = textbox.Text.Substring(4, 2);
var rest = textbox.Text.Substring(6);

1 Comment

It's not separated by anything. I prefer the separate textboxes but unfortunately the merge is a user request :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.