首页 新闻 赞助 找找看

.net 如何实现右键“排列图标-->修改时间”的功能?

0
悬赏园豆:10 [已解决问题] 解决于 2010-12-29 14:15

就是用.net实现右键的一部分功能(排列图标-->修改时间)   如何实现?

问题补充: 我想在打开文件对话框时,点按修改时间排序,下次再打开此文件时,保持原来的按修改时间排序,不用我重新排序。如果我点了按名称排序,则下次打开此文件时,保持按名称排序
--中庸--的主页 --中庸-- | 菜鸟二级 | 园豆:408
提问于:2010-11-29 09:46
< >
分享
最佳答案
0

namespace WindowsFormsTest
{
partial class FormListView
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.listView1 = new System.Windows.Forms.ListView();
this.cmsOne = new System.Windows.Forms.ContextMenuStrip(this.components);
this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.修改时间ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.cmsOne.SuspendLayout();
this.SuspendLayout();
//
// listView1
//
this.listView1.ContextMenuStrip = this.cmsOne;
this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.listView1.Location = new System.Drawing.Point(0, 0);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(357, 255);
this.listView1.TabIndex = 0;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;
//
// cmsOne
//
this.cmsOne.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripMenuItem1});
this.cmsOne.Name = "cmsOne";
this.cmsOne.Size = new System.Drawing.Size(125, 26);
this.cmsOne.Text = "排列图标";
//
// toolStripMenuItem1
//
this.toolStripMenuItem1.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.修改时间ToolStripMenuItem});
this.toolStripMenuItem1.Name = "toolStripMenuItem1";
this.toolStripMenuItem1.Size = new System.Drawing.Size(124, 22);
this.toolStripMenuItem1.Text = "排列图标";
//
// 修改时间ToolStripMenuItem
//
this.修改时间ToolStripMenuItem.Name = "修改时间ToolStripMenuItem";
this.修改时间ToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
this.修改时间ToolStripMenuItem.Text = "修改时间";
this.修改时间ToolStripMenuItem.Click += new System.EventHandler(this.修改时间ToolStripMenuItem_Click);
//
// FormListView
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(357, 255);
this.Controls.Add(this.listView1);
this.Name = "FormListView";
this.Text = "FormListView";
this.Load += new System.EventHandler(this.FormListView_Load);
this.cmsOne.ResumeLayout(false);
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ContextMenuStrip cmsOne;
private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem1;
private System.Windows.Forms.ToolStripMenuItem 修改时间ToolStripMenuItem;
}
}

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace WindowsFormsTest
{
public partial class FormListView : Form
{
public FormListView()
{
InitializeComponent();
}

private void 修改时间ToolStripMenuItem_Click(object sender, EventArgs e)
{
listView1.ListViewItemSorter
= new SortByTime();
}

private void FormListView_Load(object sender, EventArgs e)
{
this.listView1.Columns.Add("测试", 80, HorizontalAlignment.Left);
this.listView1.Columns.Add("修改时间", 100, HorizontalAlignment.Center);

this.listView1.Items.AddRange(
new ListViewItem[]{new ListViewItem(new string[]{"A",DateTime.Now.ToString()}),
new ListViewItem(new string[]{"B",DateTime.Now.AddDays(-1.00).ToString()})});
}
}

public class SortByTime : IComparer
{
public int Compare(object x, object y)
{
if (DateTime.Parse(((ListViewItem)x).SubItems[1].Text) > DateTime.Parse(((ListViewItem)y).SubItems[1].Text))
{
return 1;
}
else if (DateTime.Parse(((ListViewItem)x).SubItems[1].Text) < DateTime.Parse(((ListViewItem)y).SubItems[1].Text))
{
return -1;
}
else
return 0;
}
}
}

 

至于你说的下次打开保持原来的按修改时间排序,你可以把用户最后操作的方式记在一个配置文件中,软件启动时读取配置文件。

收获园豆:10
artwl | 专家六级 |园豆:16736 | 2010-11-29 10:49
是的,如果要记录上一次的排列,就得像天行……息的方法了
Tester Chen | 园豆:1690 (小虾三级) | 2010-11-29 12:12
其他回答(2)
0

你要应用到哪里

Astar | 园豆:40805 (高人七级) | 2010-11-29 09:51
c/s程序,我想在打开文件对话框时,点按修改时间排序,下次再打开此文件时,保持原来的按修改时间排序,不用我重新排序
支持(0) 反对(0) --中庸-- | 园豆:408 (菜鸟二级) | 2010-11-29 10:00
@--中庸--:那和操作系统有关
支持(0) 反对(0) Astar | 园豆:40805 (高人七级) | 2010-11-29 10:06
@Astar:.net能调用操作系统的这个功能吗?
支持(0) 反对(0) --中庸-- | 园豆:408 (菜鸟二级) | 2010-11-29 10:16
0

①建立一个右键菜单,建立相应选项

②在选项的单击事件中添加代码:

参考文件1

参考文件2

 

Tester Chen | 园豆:1690 (小虾三级) | 2010-11-29 09:55
谢谢啊,不过,我想在打开文件对话框时,点按修改时间排序,下次再打开此文件时,保持原来的按修改时间排序,不用我重新排序。如果我点了按名称排序,则下次打开此文件时,保持按名称排序,不好意思,我在上面说的不太清楚
支持(0) 反对(0) --中庸-- | 园豆:408 (菜鸟二级) | 2010-11-29 10:11
看楼下,说得没错
支持(0) 反对(0) Tester Chen | 园豆:1690 (小虾三级) | 2010-11-29 12:12
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册