gpt4 book ai didi

c# - 搜索按钮上的进度条

转载 作者:行者123 更新时间:2023-11-30 15:15:29 25 4
gpt4 key购买 nike

我有这个 c# 代码来显示进度条:

{
public partial class FormPesquisaFotos : Form
{
public FormPesquisaFotos()
{
InitializeComponent();
}

private void FormPesquisaFotos_Load(object sender, EventArgs e)
{
}

private void button1_Click(object sender, EventArgs e)
{
//Mostra a barra de progresso da pesquisa
while (progressBar1.Value < 100)
progressBar1.Value += 1;
{
//Criar um objeto (instância, cópia) da classe FormResultadosFotos
FormResultadosFotos NovoForm = new FormResultadosFotos();
NovoForm.Show();
}
}
}
}

它仅在搜索结束时加载(单击按钮后)。如何让进度条在搜索开始时运行?

这是在新表单上显示结果的代码。进度条在 95% 处停止,几秒钟后显示结果。

{
public partial class FormResultadosFotos : Form
{
public FormResultadosFotos()
{
InitializeComponent();
}
private void FormFotos_Load(object sender, EventArgs e)
{
// se pretendermos pesquisar em várias pastas
List<string> diretorios = new List<string>()
{@"\\Server\folder01\folder02"};

// se pretendermos pesquisar as várias extensões
List<string> extensoes = new List<string>()
{".jpg",".bmp",".png",".tiff",".gif"};

DataTable table = new DataTable();
table.Columns.Add("Nome e formato do ficheiro (duplo clique para visualizar a imagem)");
table.Columns.Add("Caminho ( pode ser copiado Ctrl+C )");
foreach (string diretorio in diretorios)
{
var ficheiros = Directory.EnumerateFiles(diretorio, "*", SearchOption.AllDirectories).
Where(r => extensoes.Contains(Path.GetExtension(r.ToLower())));
foreach (var ficheiro in ficheiros)
{
DataRow row = table.NewRow();
row[0] = Path.GetFileName(ficheiro);
row[1] = ficheiro;
table.Rows.Add(row);
}
}
dataGridView1.DataSource = table;
dataGridView1.Columns[1].Visible = true;
}
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
FormPictureBox myForm = new FormPictureBox();
string imageName = dataGridView1.CurrentRow.Cells[1].Value.ToString();
Image img;
img = Image.FromFile(imageName);
myForm.pictureBox1.Image = img;
myForm.ShowDialog();
}
}
}

谢谢。

最佳答案

你必须在新线程上而不是在主线程上。

这是一个小例子:

private void buttonWorkerTest_Click(object sender, RoutedEventArgs e)
{
this.progressBarWorkerTest.Value = 0;
BackgroundWorker worker = new BackgroundWorker();
// Event for the method that will run on the background
worker.DoWork += this.Worker_DoWork;
// Event that will run after the BackgroundWorker finnish
worker.RunWorkerCompleted += this.Worker_RunWorkerCompleted;
worker.RunWorkerAsync();


}

private void Worker_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 1; i <= 100; i++)
{
Dispatcher.Invoke(new Action(() =>
{
this.progressBarWorkerTest.Value = i;
}));
Thread.Sleep(100);
}
}

private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// You can put the code here to open the new form and such
}

Dispacher.Invoke 是因为它在 WPF 上,对于 WinForm 只需将其更改为 this.Invoke

这个例子,当按钮被点击时它会启动 BackgroundWorker,有一个从 1 到 100 的 for,它会休眠 100 毫秒并更新进度条。

希望对你有帮助

编辑

现在在示例中包含了在 BackgroundWorker 完成时运行的事件,以防万一需要它

编辑 2:

我的建议是在背景上搜索照片时将它们全部插入 DataTable 中,因为您已经准备好搜索它们并且工作也可以在这里完成,然后只需在 FormResultadosFotos 上创建一个构造函数来接收它数据表。

据我所知,主要目标是在 FormPesquisaFotos 表单上搜索它们(这就是为什么我们在那里有后台工作人员,搜索它们并更新 ProgressBar)并在新表单 AKA FormResultadosFotos 上显示它们

// Lets create a DataTable variable to be access on the Worker_DoWork and then on the Worker_RunWorkerCompleted
private DataTable tableOfPhotos;

private void Worker_DoWork(object sender, DoWorkEventArgs e)
{
// Search for the photos here and then add them to the DataTable
this.tableOfPhotos = new DataTable();
tableOfPhotos.Columns.Add("Nome e formato do ficheiro (duplo clique para visualizar a imagem)");
tableOfPhotos.Columns.Add("Caminho ( pode ser copiado Ctrl+C )");
foreach (string diretorio in diretorios)
{
// se pretendermos pesquisar em várias pastas
List<string> diretorios = new List<string>()
{@"\\Server\folder01\folder02"};

// se pretendermos pesquisar as várias extensões
List<string> extensoes = new List<string>()
{"*.jpg","*.bmp","*.png","*.tiff","*.gif"};

var ficheiros = Directory.EnumerateFiles(diretorio, "*", SearchOption.AllDirectories).
Where(r => extensoes.Contains(Path.GetExtension(r.ToLower())));
foreach (var ficheiro in ficheiros)
{
DataRow row = tableOfPhotos.NewRow();
row[0] = Path.GetFileName(ficheiro);
row[1] = ficheiro;
tableOfPhotos.Rows.Add(row);
}
}
}

private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// You can put the code here to open the new form and such
FormResultadosFotos NovoForm = new FormResultadosFotos(this.tableOfPhotos);
NovoForm.Show();
}


// Constructor that will receive the DataTable and put it into the dataGridView1, it should be added on the Form FormResultadosFotos
Public FormResultadosFotos(DataTable table)
{
InitializeComponent();
// In here we will tell the where is the source for the dataGridView1
this.dataGridView1.DataSource = table;
}

在这里,您还可以通过在 this.dataGridView1.DataSource = table; 行上放置一个断点来查看表格带来的内容,如果表格为空,则表格中没有任何内容(也许目录上没有照片?无法访问它?不在工作,也没有任何 IDE,只是根据我头脑中的内容来回答我的问题,但您也可以将文件放在一个类似的文件中代码(如果需要):

List<string> tempList = new List<string>;
foreach (string entryExt in extensoes)
{
foreach (string entryDir in diretorios)
{
// SearchOption.AllDirectories search the directory and sub directorys if necessary
// SearchOption.TopDirectoryOnly search only the directory
tempList.AddRange(Directory.GetFiles(entryDir, entryExt, SearchOption.AllDirectories));
}
}

// Here would run all the files that it has found and add them into the DataTable
foreach (string entry in tempList)
{
DataRow row = tableOfPhotos.NewRow();
row[0] = Path.GetFileName(entry);
row[1] = entry;
tableOfPhotos.Rows.Add(row);
}

编辑 3:

改变你的代码

List<string> extensoes = new List<string>(){".jpg",".bmp",".png",".tiff",".gif"};

List<string> extensoes = new List<string>(){"*.jpg","*.bmp","*.png","*.tiff","*.gif"};

您需要在扩展名前加上 *(例如 .png)才能搜索该扩展名的文件

关于c# - 搜索按钮上的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51984123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com