【问题标题】:Can you help me on textchanged ans copy你能帮我解决textchanged ans copy吗
【发布时间】:2020-02-26 18:24:03
【问题描述】:

您好,当有多个文件时,我希望能够在每次文件迭代时更改我的 txtFiles 文本框的文本。

即使目录中有多个文件,我也可以一次只复制一个文件。

我声明了 textchanged 事件。 我已经声明了 txtFiles_TextChanged 函数,但我不知道该放什么来使 txtFiles 文本框的文本随着文件的每次迭代而改变。

我声明了 txtFiles.Text = ConvertToString (files [i]); 更改文本框中的文本。

我还希望一次只能复制一个文件,但它会清除所有 txt 文件。

你如何解决这个问题?

string sourceDir = 
@"C:\Users\PORTABLEHP\Documents";
string destDir = 
@"C:\Users\PORTABLEHP\Documents\
xgen";
public évent EventHandler 
textchanged;


private void btnCopy(object 
sender,EventArgs e)
{
try
{
string [] txtList = Directory.
GetFiles(sourceDir,"*.txt");


foreach(string f in txtList)
{
try
{
string fName = f.Substring
(sourceDir.Length + 1);

string [] files = new string 
[sourceDir.Length];

progressBar1.Value = 1;
progressBar1.Minimum = 0;
progressBar1.Maximum = 
files.Length;

for(int i = 1;i < files.Length; 
i++)
{
progressBar1.PerformStep();
//To have the text change from the 
textbox
txtFiles.Text = ConvertToString
(files[i]);

File.Copy(Path.Combine
(sourceDir,fName), Path.Combine
(destDir,fName), true);
}
}
catch(IOException copyerror)
{
MessageBox.Show(copyerror.Message);
}
}
}
catch (DirectoryNotFoundException 
dirnotfound)
{
MessageBox.Show
(dirnotfound.Message);

private void txtFiles_TextChanged
(object sender,EventArgs e)
{ 
// I don't know ?
}

【问题讨论】:

  • 这是无法阅读的。你应该缩进你的代码并使其可读——这只是礼貌,因为你在寻求帮助

标签: c# textchanged


【解决方案1】:

所以如果我没看错的话,你需要对你的代码做一些改动

改变

txtFiles.Text = ConvertToString(files[i]);

txtFiles.Text = $"{ Path.GetFileName(f)} was copied";    //f was declared as string in your first loop that contains the file path

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 2022-12-15
    • 2011-01-24
    • 2020-09-03
    • 2021-08-14
    相关资源
    最近更新 更多