Перейти к основному контенту
Центр разработки для Windows

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

  • Вопрос

  • 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?



    19 октября 2019 г. 20:18

Ответы

  • 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.

    • Помечено в качестве ответа MarcinLas 24 октября 2019 г. 16:09
    • Снята пометка об ответе MarcinLas 24 октября 2019 г. 16:09
    • Помечено в качестве ответа MarcinLas 24 октября 2019 г. 16:10
    21 октября 2019 г. 1:45
    Модератор