zoukankan      html  css  js  c++  java
  • listBox和pictureBox的使用

    重要属性:
    pictureBox中SizeMode可以更改图像显示的尺寸大小。

    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; namespace ListBox { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //为了在两个方法中都能访问到 String[] path = Directory.GetFiles("E:\00","*.jpg"); private void Form1_Load(object sender, EventArgs e) { for ( int i = 0; i < path.Length; i++) { //根据路径名获取文件名称 string fileName = Path.GetFileName(path[i]); listBox1.Items.Add(fileName); } } private void listBox1_DoubleClick(object sender, EventArgs e) { //添加图片文件,需要添加全路径 pictureBox1.Image = Image.FromFile(path[listBox1.SelectedIndex]); } } }

    使用List

    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;
    
    namespace ListBox
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            //为了在两个方法中都能访问到
            List<string> list = new List<string>();
    
            private void Form1_Load(object sender, EventArgs e)
            {
                String[] path = Directory.GetFiles("E:\00", "*.jpg");
                 for ( int i = 0; i < path.Length; i++)
                {
                     //根据路径名获取文件名称
                     string fileName = Path.GetFileName(path[i]);
                     listBox1.Items.Add(fileName);
    
                     //将图片全路径添加到List泛型中;
                     list.Add(path[i]);
                }
            }
    
            private void listBox1_DoubleClick(object sender, EventArgs e)
            {
                //添加图片文件,需要添加全路径
                pictureBox1.Image = Image.FromFile(list[listBox1.SelectedIndex]);
            }
    
            private void listBox1_Click(object sender, EventArgs e)
            {
                
                pictureBox1.Image = Image.FromFile(list[listBox1.SelectedIndex]);
            }
        }
    }
    

      

  • 相关阅读:
    【转】STL中map用法详解
    【转】容器 C++ set和map
    .NET简谈面向接口编程 狼人:
    详解.NET程序集的加载规则 狼人:
    ASP.NET MVC 入门介绍 (上) 狼人:
    页面片段缓存(二) 狼人:
    改善代码设计 —— 优化物件之间的特性(Moving Features Between Objects) 狼人:
    改善代码设计 —— 组织好你的数据(Composing Data) 狼人:
    C# 中奇妙的函数联接序列的五种简单方法 狼人:
    Log4Net 全方位跟踪程序运行 狼人:
  • 原文地址:https://www.cnblogs.com/my-cat/p/7417673.html
Copyright © 2011-2022 走看看