zoukankan      html  css  js  c++  java
  • C# 实现模拟下载功能,被下载的文件存储在本地

    要求:左侧三个按钮,点击按钮会出现相应的文字信息和下载图标,实现下载文件的功能(实现模拟下载功能,被下载的文件存储在本地)。

    先看到三个按钮的位置:

    因为在这里是三个按钮轮流切换,所以在后台写了一个方法:

     public void AddInfo(string LabelContext, int i)
            {
                string ImagePath = Environment.CurrentDirectory;//获取当前工作目录的完全路径
                Label lb = new Label();
                lb.AutoSize = true;//控件跟着文字的大小进行变化
                lb.Location = new System.Drawing.Point(0, i);//控件的位置
                lb.Text = LabelContext;//显示的文字
                Button btn = new Button();
                btn.Height = 25;//高度
                btn.Width = 25;//宽度
                btn.Tag = LabelContext;
                //拉姆达表达式,+= 是在委托链上增加一个委托,(s, e) => 是一个lambda表达式,这个表达式创建一个委托,委托处理的主体就是 => 后面的部分。
                btn.Click += (sender, e) =>
                {
                    SaveFileDialog ofd = new SaveFileDialog();//提示用户选择文件的保存位置
                    ofd.FileName = LabelContext;//被保存的文件的名字
                    if (ofd.ShowDialog() == DialogResult.OK)//意思是当按下ok键执行下列操作
                    {
                        string strFileName = ofd.FileName;
                        File.WriteAllBytes(strFileName, File.ReadAllBytes(ImagePath + "\PDF\" + LabelContext));
                        //创建一个新的文件,找到原来的文件把内容复制下来
                        MessageBox.Show("下载成功");
                    }
                };
                btn.Location = new System.Drawing.Point(lb.Width * 2 + btn.Width, i);
                btn.BackgroundImage = Image.FromFile(ImagePath + "\Image\download.png");
                btn.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;//设置图片适应按钮大小
                panel_right.Controls.Add(lb);
                panel_right.Controls.Add(btn);
            }

    然后就要用到这个方法了,在三个按钮的点击事件里面:

    private void btn_caw_Click(object sender, EventArgs e)
            {
                panel_right.Controls.Clear();//清空面板,在右边存放的是面板控件
                int y = 0;//纵坐标
                AddInfo("China in the Worldskills.pdf", y);      
            }
     private void btn_cwc_Click(object sender, EventArgs e)
            {
                panel_right.Controls.Clear();
                int y = 0;
                AddInfo("Committee of Worldskills China.pdf", y);
            }
     private void btn_par_Click(object sender, EventArgs e)
            {
                panel_right.Controls.Clear();
                string[] str = new string[] { "the 41st Competition.pdf", "the 42nd Competition.pdf", "the 43rd Competition.pdf" };//数组
                int y = 0;
                for (int i = 0; i < 3; i++)
                {
                    AddInfo(str[i], y);//循环三次把数组的值显示出来
                    y = y + 30;
                }
            }

    下载功能就做完啦。

  • 相关阅读:
    Javascript 如何识别数组
    新手 如何搭建一个vue项目详解
    javasScript 七种数据类型
    WPF实现动画的几种方式及其小案例
    问题清单
    2020软件工程个人作业06——软件工程实践总结作业
    我的“捡漏”生涯——小黄衫篇
    2020软件工程作业05
    2020软件工程作业03
    Python: list indices must be integers or slices, not float问题
  • 原文地址:https://www.cnblogs.com/madan01/p/10557923.html
Copyright © 2011-2022 走看看