views:

63

answers:

3

Hi all,

I've got a form with multiple text boxes which are file paths for the program to import data from. Currently they are checked for non-zero length by the following:

    //this code imports the files required by the user, as specified in the
    //file path text boxes
    private void btImport_Click(object sender, EventArgs e)
    {
        bool hasPath = false;
        foreach (TextBox box in this.gbPaths.Controls.OfType<TextBox>().Where(tb => tb.Text.Length > 0))
        {
            hasPath = true;
            //import code
        }//end foreach

        if (!hasPath)
        {
            MessageBox.Show("You must enter at least one file path.");
        }//end if
    }//end import code

What I'm wondering is can I replace the //import code part with something like:

if(tb.Name = "txtAvF") then...

or similar, or do I have to do it outside of the foreach loop? Thanks in advance. Let me know if I need to clarify anything.

A: 

If you want to check to see if the TextBox is one of the ones on the form (which I think you are), then you are == which (taken from MSDN)

the operator == tests for reference equality by determining if two references indicate the same object

So this is what you're looking for:

if(box == textBox1 && !string.IsNullOrEmpty(box.Text))
{
      // Import Textbox1
}
else if(box == textBox2 && !string.IsNullOrEmpty(box.Text))
{
      // Import Textbox2
}
else if (box == textBox3....)
PostMan
Thanks for your input PostMan, but the foreach statement just goes through each of the textboxes on the form and determines if they are not empty; what I need to do now is determine which box is currently being queried for zero length, and if it has text in it, run specific import code for the file belonging to that textbox. Problem being, I don't know how to determine which textbox is currently being queried.
mispecialist
Well that code is testing to see if the reference for `box` is the same reference as `txtAvf`, did you want something more dynamic?
PostMan
What it needs to do is go:Is testbox 1 empty? No? Then import file 1 (path stored in textbox 1)Is textbox 2 empty? No? Then import file 2Is textbox 3 empty? Yes? Move on...Is textbox 4 empty? No? Then import file 4And so on until no more textboxes exist.
mispecialist
See my edit, it's not dynamic, but it shows how you can check if the box is textbox1 (or 2, or 3 etc) and then you can use the `box` however you need.
PostMan
Awesome, thanks :D
mispecialist
A: 

You should do it inside of the loop. Like this:

if (box.Name == "txtAvF")
    box.Text = "What you want";

But setting hasPath inside the loop just holds state for your last path. You should also move MessageBox code inside loop.

Xaqron
A: 

The hasPath assignment seems correct to me; it's set for any one text box, and if not set at the end of the loop, a message is displayed. This rhymes well with the text so displayed. Moving the MessageBox call into the loop would cause the dialog box to never be displayed (or errenously displayed), at least as the code is implemented now, since the OfType<>().Where() guarantees that all text boxes iterated over have at least some content.

(I would add this as a comment to @Xaqron but don't have the necessary reputation yet.)

Michael Kjörling