using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private ContextMenuStrip contextMenuStrip;
private List<string> nameList = new List<string>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
InitializeDataGridViewWithButtonColumn();
// 初始化上下文菜单(下拉框)
contextMenuStrip = new ContextMenuStrip();
// 为菜单项添加点击事件
contextMenuStrip.ItemClicked += ContextMenuStrip_ItemClicked;
}
private void button1_Click(object sender, EventArgs e)
{
// 假设dataGridView1已经有一些列定义好了
int rowIndex = dataGridView1.Rows.Add();
Random rand = new Random();
int randomNumber = rand.Next(1, 50);
dataGridView1.Rows[rowIndex].Cells[0].Value = 292-rowIndex;
dataGridView1.Rows[rowIndex].Cells[1].Value = DateTime.Now.ToString("yyyy-MM-dd");
dataGridView1.Rows[rowIndex].Cells[2].Value = DateTime.Now.ToString("HH:mm:ss");
dataGridView1.Rows[rowIndex].Cells[4].Value = "TestName("+randomNumber+")";
nameList.Add("TestName(" + randomNumber + ")");
StartSort();
}
private void InitializeDataGridViewWithButtonColumn()
{
// 创建一个DataGridViewButtonColumn的实例
DataGridViewButtonColumn btnColumn = new DataGridViewButtonColumn();
btnColumn.Name = "actionColumn"; // 设置列名
btnColumn.HeaderText = "操作"; // 设置列头文本
btnColumn.UseColumnTextForButtonValue = true; // 设置为true时,将Text属性用作按钮的显示文本(但请注意,实际效果可能因.NET版本而异)
btnColumn.Width = 30; // 设置列宽
dataGridView1.Columns.Insert(3, btnColumn);
dataGridView1.CellContentClick += dataGridView1_CellContentClick;
}
/// <summary>
/// 处理DataGridView的CellContentClick事件,以检测何时点击了按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
// 检查点击的列是否是按钮列
if (dataGridView1.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
{
// 显示(下拉框)
Point screenPoint = dataGridView1.PointToScreen(dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Location);
contextMenuStrip.Show(screenPoint);
}
}
/// <summary>
/// 点击下拉框内容触发该函数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
string clickedItemText = e.ClickedItem.Text;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[4].Value.ToString() == clickedItemText) // 假设名称在第5列(索引为4)
{
// 找到匹配的行后,定位到该行
dataGridView1.CurrentCell = row.Cells[4]; // 定位到第一列,你也可以选择其他列
dataGridView1.FirstDisplayedScrollingRowIndex = row.Index; // 确保该行在视图中可见
break;
}
}
}
/// <summary>
/// 开始排序
/// </summary>
private void StartSort()
{
ExtractNum();
}
/// <summary>
/// 提取数字
/// </summary>
private void ExtractNum()
{
// 使用LINQ和正则表达式来提取括号内的数字,忽略不包含数字的字符串
int[] OnlyNumber = nameList.Where(s => Regex.IsMatch(s, @"\((\d+)\)")) // 首先过滤掉不包含(数字)的字符串
.Select(s => int.Parse(Regex.Match(s, @"\((\d+)\)").Groups[1].Value)) // 然后提取数字
.ToArray(); // 最后将结果转换为数组
NumberSort(OnlyNumber);
}
/// <summary>
/// 数字排序
/// </summary>
/// <param name="OnlyNumber"></param>
private void NumberSort(int[] OnlyNumber)
{
Array.Sort(OnlyNumber);//sort:是排序的意思,对这个一维数组进行排序
contextMenuStrip.Items.Clear();
for (int q = 0; q < OnlyNumber.Length; q++)//利用for循环输出结果
{
contextMenuStrip.Items.Add("TestName(" + OnlyNumber[q] + ")" );
}
}
}
}