【问题标题】:Winforms - Listbox will not update contenets until clickedWinforms - 列表框在单击之前不会更新内容
【发布时间】:2021-01-25 18:18:22
【问题描述】:

我会尽量添加所需的信息,如果您需要任何我没有添加的额外信息,请告诉我,我会尽力提供。

我的问题的基本原理是,当我按下按钮时,它会抓取我选择的文件并将其保存到文本文件中。这很好用,我可以根据需要保存尽可能多的文件。问题在于我的列表框的列表框。我的应用程序是一个音板,我希望将带有热键的文件名显示在 几乎 工作正常的列表框中。在加载应用程序时,列表框将获取所有保存的文件并相应地显示它们一次,在添加文件时,该文件将被添加到列表框并被保存。正如我所说,这几乎可以工作,因为出于某种我不知道的原因,您必须单击列表框才能添加内容。我的代码如下:

public partial class Form1 : Form
{
    public int getNumberOfSongs()
    {
        using (Stream stream = File.Open(@"Sounds.txt", FileMode.Open))
        {
            using (StreamReader reader = new StreamReader(stream))
            {
                string line = null;
                for (int i = 0; i < 1; ++i)
                {
                    line = reader.ReadLine();   
                    int ine = Int32.Parse(line);
                    ine = ine + 1;
                    return ine;
                }
            }
        }
        return 8;
        //This is only here so it doesn't give me an error, it is never used
    }


    public void fineChanger(string newText, string fileName, int line_to_edit)
    {
        string[] arrLine = File.ReadAllLines(fileName);
        arrLine[line_to_edit] = newText;
        File.WriteAllLines(fileName, arrLine);
    }
    public void addFile()
    {
        OpenFileDialog openFileDialog = new OpenFileDialog();
        openFileDialog.InitialDirectory = "c:\\";
        openFileDialog.Filter = "WAV files (*.wav)|*.wav";
        openFileDialog.DefaultExt = ".wav";
        openFileDialog.FilterIndex = 2;
        openFileDialog.RestoreDirectory = true;
        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            //Get the path of specified file
            string filePath = openFileDialog.FileName;
            songToAdd = filePath;
            string control = filePath + "§modifier§hotkey";
            string savePath = @"Sounds.txt";
            int bruh = getNumberOfSongs();
            fineChanger(control, savePath, bruh);
            string bru = bruh.ToString();
            fineChanger(bru, savePath, 0);
            add = true;
        }
    }
    public bool add = false;
    public string songToAdd;
    public bool load = true;

    public Form1()
    {
        InitializeComponent();
    }

    public void button1_Click(object sender, EventArgs e)
    {
        addFile();
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (load == true)
        {
            listBox1.DataSource = File.ReadAllLines(@"Sounds.txt");
            load = false;
        }
        if(add == true)
        {
            listBox1.Items.Add(songToAdd);
            add = false;
        }            
    }
}

附:我仍然是 Windows 窗体的新手,而且这个应用程序还远未完成。

【问题讨论】:

  • 您实际上只是将数据添加到 SelectedIndexChanged 中的listBox1.Items。为什么不在addFile末尾做呢?
  • 不敢相信我没有想到这个,让我试试这个。
  • getNumberOfSongs - 这个方法有不少问题,

标签: c# winforms listbox


【解决方案1】:

我没有在 SelectedIndexChanged 中添加项目,而是将它们添加到外部。我在Form1_Load 中加载保存的歌曲,并在addFile() 函数中打开/保存加载的文件。 编辑代码:

using System;
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;
using System.IO;
using System.Media;
using System.Security.Cryptography.X509Certificates;
using System.Runtime;
using System.Runtime.InteropServices;
using Microsoft.VisualBasic;
using System.Diagnostics;

namespace SoundBoard
{
    public partial class Form1 : Form
    {
        public int getNumberOfSongs()
        {
            using (Stream stream = File.Open(@"Sounds.txt", FileMode.Open))
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    string line = null;
                    for (int i = 0; i < 1; ++i)
                    {
                        line = reader.ReadLine();   
                        int ine = Int32.Parse(line);
                        ine = ine + 1;
                        return ine;
                    }
                }
            }
            return 8;
            //This is only here so it doesn't give me an error, it is never used
        }
        public bool load = true;
        
        public void fineChanger(string newText, string fileName, int line_to_edit)
        {
            string[] arrLine = File.ReadAllLines(fileName);
            arrLine[line_to_edit] = newText;
            File.WriteAllLines(fileName, arrLine);
        }
        public void addFile()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = "c:\\";
            openFileDialog.Filter = "WAV files (*.wav)|*.wav";
            openFileDialog.DefaultExt = ".wav";
            openFileDialog.FilterIndex = 2;
            openFileDialog.RestoreDirectory = true;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                //Get the path of specified file
                string filePath = openFileDialog.FileName;
                string control = filePath + "§modifier§hotkey";
                string savePath = @"Sounds.txt";
                int bruh = getNumberOfSongs();
                fineChanger(control, savePath, bruh);
                string bru = bruh.ToString();
                fineChanger(bru, savePath, 0);
                listBox1.Items.Add(filePath);
            }
        }
        public bool loada = true;

        public Form1()
        {
            InitializeComponent();
        }

        public void button1_Click(object sender, EventArgs e)
        {
            addFile();
        }
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (loada == true)
            {
                listBox1.Items.Add(File.ReadAllLines(@"Sounds.txt"));
                loada = false;
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-29
    • 1970-01-01
    相关资源
    最近更新 更多