none
ComboBox get folders. What's wrong? RRS feed

  • Question

  • Hello people of goodwill,

    It's my first WFApp. 
    I try to get list of folders in combobox dropdown. But first user has to type number of project e.g. 17296.

    Then programme assign a filed : 

     private void ProjNumbInput(object sender, EventArgs e)
            {
                _projectNumber = aProjNumbInput.Text;
                if (!_projectNumber.All(char.IsDigit))
                    MessageBox.Show("Project number cannot contain letters");
                string[] dir = Directory.GetDirectories(@"D:\", _projectNumber + "*");
                _path = dir[0] + "\\example1\\example2";
            }

    Then from combobox all folders in "example2" should be visible. Should be..  When I start app  combobox is empty. I've seen all examples of combobox on yt and google but none of them used fields.

    private void UserInputText_Load(object sender, EventArgs e)
            {
                if (_path != null)
                {
                    DirectoryInfo dirInfo = new DirectoryInfo(_path);
                    DirectoryInfo[] folders = dirInfo.GetDirectories();
                    aDropDown.DataSource = folders;
                }
            }
    Does anyone see a mistake there?



    Saturday, October 19, 2019 8:18 PM

Answers

  • Hi MarcinLas,

    Have you defined the path "_path" as a global variable?

        string _projectNumber;
        string _path;
    
        private void btProjNumbInput_Click(object sender, EventArgs e)
        {
            _projectNumber = aProjNumbInput.Text;
            if (!_projectNumber.All(char.IsDigit))
                MessageBox.Show("Project number cannot contain letters");
            string[] dir = Directory.GetDirectories(@"D:\", _projectNumber + "*");
            _path = dir[0] + "\\example1\\example2";
        }
    
        private void btFill_Click(object sender, EventArgs e)
        {
            if (_path != null)
            {
                DirectoryInfo dirInfo = new DirectoryInfo(_path);
                DirectoryInfo[] folders = dirInfo.GetDirectories();
                aDropDown.DataSource = folders;
            }
        }

    The test result,

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by MarcinLas Thursday, October 24, 2019 4:09 PM
    • Unmarked as answer by MarcinLas Thursday, October 24, 2019 4:09 PM
    • Marked as answer by MarcinLas Thursday, October 24, 2019 4:10 PM
    Monday, October 21, 2019 1:45 AM
    Moderator