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.Diagnostics.Eventing.Reader;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int count = 0;
private void button2_MouseClick(object sender, MouseEventArgs e)
{
string radio;
string check="";
if (radioButton1.Checked == true) radio = "本科";
else radio = "研究生";
if (checkBox1.Checked == true) check += "跑步";
if (checkBox2.Checked == true) check += "跳舞";
if (checkBox3.Checked == true) check += "书法";
if (checkBox4.Checked == true) check += "象棋";
dataGridView1.Rows.Insert(count, new object[] { textBox1.Text, textBox6.Text, comboBox1.Text, textBox3.Text, textBox4.Text, textBox5.Text,radio,check});//调用Insert方法,用object数组将数据录入到表格中
count++;
textBox1.Text = ""; //录入一个后,将文本框,选项等清空
textBox6.Text = "";
comboBox1.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
checkBox1.Checked = false;
checkBox2.Checked = false;
checkBox3.Checked = false;
checkBox4.Checked = false;
radioButton1.Checked = false;
radioButton2.Checked = false;
textBox7.Text = "";
pictureBox1.Image = null;
}
private void button1_MouseClick(object sender, MouseEventArgs e)
{
string path = textBox7.Text;
if (File.Exists(@path)) { pictureBox1.Image = Image.FromFile(@path); } //找到了显示
else MessageBox.Show("图片未找到","错误"); //没找到提示
}
//这里是通过输入路径来加载图片的方法。
private void pictureBox1_DragDrop(object sender, DragEventArgs e)//DragDrop事件是在图片释放的时候发生的
{
string fileName = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString(); //获取路径并转成字符串格式
this.pictureBox1.Image = Image.FromFile(fileName); //在窗体上展现出来
}
private void pictureBox1_DragEnter(object sender, DragEventArgs e) //DragEnter事件是在图片拖拽到边缘的时候发生的
{
if (e.Data.GetDataPresent(DataFormats.FileDrop)) //如果获取到了拖拽过来文件的数据
e.Effect = DragDropEffects.Link; //e的效果就等于拖放的效果·
else e.Effect = DragDropEffects.None; //否则e的效果没有
}
private void Form1_Load(object sender, EventArgs e)
{
this.pictureBox1.AllowDrop = true; //在窗口加载时,声明图片框是允许拖拽操作的,这是第一步
}
//这里是通过拖拽的方式将图片拖拽到图片框里的。
关于拖拽操作中,获取路径的问题:
((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString()
e.Data 表示拖放操作中正在传输的数据,其中包含了与该操作相关的详细信息。通过在 e.Data 中查找与指定格式相对应的数据,可以获取有关拖动操作中传输数据的详细信息,包括文件路径或文件夹路径,从而实现对拖动操作的处理。
DataFormats.FileDrop 是枚举类型,用于指定拖放操作中传输的数据格式。它指定的数据格式是一个包含文件路径或文件夹路径的字符串数组。
GetValue(0) 返回数组中的第一个元素,也就是文件路径或文件夹路径的字符串,因为在很多情况下我们只关心第一个被拖动的文件或文件夹。所以,这段代码会获取拖放操作中第一个被拖动的文件或文件夹的路径字符串,并将其作为字符串返回。
结合我个人的拙劣见解,简单的来说,应该是:
首先e.Data获取了拖拽文件的数据,然后通过DataFormats.FileDrop格式来进一步筛选出有关路径的数据,通过(System.Array)转换并装入到Array数组,然后通过Getvalue(0)来获取第一个文件的路径,最后再转成字符串也就是ToString()。
拖拽操作中,建立链接的问题:
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Link;
else e.Effect = DragDropEffects.None;
GetDataPresent() 用于检查是否包含指定格式的数据。在这里用于检查获取的数据中有没有DataFormats.FileDrop格式的数据。换言之,就是检查有没有有关路径的数据。
如果有的话,就并将拖拽操作的效果设置为“链接”类型,告诉用户当放置操作完成时,数据不会从其源位置移动到放置位置,而是会在源位置创建一个指向数据的引用,以链接的方式替代。没有的话就不建立。
结合个人见解:也就是通过判断有没有路径数据,来决定是否允许拖拽(建立连接)。
private void button3_MouseClick(object sender, MouseEventArgs e)
{
dataGridView1.Rows.Clear();count = 0;
}
//这里是删除所有表格数据的操作
private void button4_MouseClick(object sender, MouseEventArgs e)
{
dataGridView1.Rows.Remove(dataGridView1.SelectedRows[0]);
}
//这里是删除选择表格中的行的操作
//0代表删除倒数第一个
//1代表删除倒数第二个
//以此类推