A
A
Anton Seredny2017-09-21 01:17:11
C++ / C#
Anton Seredny, 2017-09-21 01:17:11

Where to declare ListView so that you can change it anywhere in the program?

There is a program - by clicking on the button opens the folder selection window. After a successful selection, it takes files from this folder and shoves their names into the ListView.
But the point is that in my code the program ends up running only once... What's wrong? If you move the initialization of the Listview to another place, then you can not access it to add items, because the leaf variable is out of scope ...
I understand that this is a nonsense problem, but I can not find a solution.
KAgMRv8s4G1Z3m.jpg

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        public void MyButtons()
        {
            Button b1 = new Button();
            b1.Location = new System.Drawing.Point(this.ClientRectangle.Width / 2 - 100 / 2, this.ClientRectangle.Height / 2 - 70/ 2);
            b1.Size = new Size(100, 70);
            b1.TabIndex = 0;
            b1.Text = "Выбрать папку";
            b1.UseVisualStyleBackColor = true;

            //Вешаем обработчик на кнопку
            b1.Click += new EventHandler(b1_click);
            Controls.Add(b1);
        }

        public void b1_click(object sender, EventArgs e)
        {

            FolderBrowserDialog FBD = new FolderBrowserDialog();
            if (FBD.ShowDialog() == DialogResult.OK)
            {
                // Инициализация и настроки ListView
                ListView ListView1 = new ListView();
                ListView1.Location = new System.Drawing.Point(12, 12);
                ListView1.Name = "ListView1";
                ListView1.Size = new System.Drawing.Size(245, 200);
                ListView1.BackColor = System.Drawing.Color.DarkOrange;
                ListView1.ForeColor = System.Drawing.Color.Black;
                Controls.Add(ListView1);

                // Добавляем файлы из выбранной директории в ListView
                System.IO.DirectoryInfo DI = new System.IO.DirectoryInfo(FBD.SelectedPath);
                foreach (System.IO.FileInfo FI in DI.GetFiles())
                {
                    ListView1.Items.Add(FI.Name);
                }
            }
           
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            MyButtons();
        }

    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
PaksinME, 2017-09-21
@smidl

I apologize for the lack of tags, I'm writing from the phone, but something like this. Should work
///
public partial class Form1 : Form
{
ListView ListView1
public Form1()
{
InitializeComponent();
}
/////
private void Form1_Load(object sender, EventArgs e)
{
ListView1 = new ListView();
ListView1.Location = new System.Drawing.Point(12, 12);
ListView1.Name = "ListView1";
ListView1.Size = new System.Drawing.Size(245, 200);
ListView1.BackColor = System.Drawing.Color.DarkOrange;
ListView1.ForeColor = System.Drawing.Color.Black;
Controls.Add(ListView1);
///
Remove from public void b1_click Initialization and settings of ListView

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question