• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#:设置控件样式(待补充)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;

namespace MyCommanHelper
{
    public class ControlStyleHelper
    {
    #region 字段变量
        // Fields
        public static int currentCol = -1;
        public static bool sort;

    #endregion

    #region 网格视图
        /// <summary>
        /// 初始化网格视图样式
        /// </summary>
        /// <param name="dataGridView1">网格控件</param>
        /// <param name="isReadOnly">是否只读</param>
        /// <param name="isForbidSortColumn">是否禁止排序</param>
        public static void InitDataGridView(DataGridView dataGridView1, bool isReadOnly, bool isForbidSortColumn)
        {
            dataGridView1.RowHeadersDefaultCellStyle.BackColor = SystemColors.GradientActiveCaption;
            dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.GradientActiveCaption;
            dataGridView1.BackgroundColor = Color.White;
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            style.SelectionBackColor = Color.FromArgb(0x80, 0x80, 0xff);
            style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dataGridView1.DefaultCellStyle = style;
            dataGridView1.ClearSelection();
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.ReadOnly = isReadOnly;
            if (isForbidSortColumn)
            {
                for (int i = 0; i < dataGridView1.Columns.Count; i++)
                {
                    dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
                }
            }
        }

    #endregion

    #region 对话框
        /// <summary>
        /// 初始化对话框模式样式
        /// </summary>
        /// <param name="frm"></param>
        public static void InitFormShowDialogStyle(Form frm)
        {
            frm.ShowInTaskbar = false;
            frm.MaximizeBox = false;
            frm.MinimizeBox = false;
            frm.ShowIcon = false;
            frm.StartPosition = FormStartPosition.CenterScreen;
            frm.FormBorderStyle = FormBorderStyle.FixedDialog;
            frm.ShowDialog();
        }

        /// <summary>
        /// 初始化对话框非模式样式
        /// </summary>
        /// <param name="frm"></param>
        public static void InitFormShowStyle(Form frm)
        {
            frm.ShowInTaskbar = true;
            frm.MaximizeBox = false;
            frm.MinimizeBox = false;
            frm.ShowIcon = false;
            frm.StartPosition = FormStartPosition.CenterScreen;
            frm.FormBorderStyle = FormBorderStyle.FixedDialog;
            frm.Show();
        }

    #endregion

    #region 列表视图

        /// <summary>
        /// 初始化列表视图样式
        /// </summary>
        /// <param name="listView1">列表视图控件</param>
        /// <param name="isCheckBox">是否显示复选框</param>
        public static void InitListViewStyle(ListView listView1, bool isCheckBox)
        {
            listView1.View = View.Details;
            listView1.ShowItemToolTips = true;
            listView1.GridLines = true;
            listView1.FullRowSelect = true;
            listView1.CheckBoxes = isCheckBox;
        }

        /// <summary>
        /// 对列排序并标示标识
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void SortListViewColumn(object sender, ColumnClickEventArgs e)
        {
            string str3;
            char ch = '▼';
            string str = ch.ToString().PadLeft(4, ' ');
            ch = '▲';
            string str2 = ch.ToString().PadLeft(4, ' ');
            ListView view = sender as ListView;
            if (!sort)
            {
                sort = true;
                str3 = view.Columns[e.Column].Text.TrimEnd(new char[] { '▼', '▲', ' ' });
                view.Columns[e.Column].Text = str3 + str2;
            }
            else if (sort)
            {
                sort = false;
                str3 = view.Columns[e.Column].Text.TrimEnd(new char[] { '▼', '▲', ' ' });
                view.Columns[e.Column].Text = str3 + str;
            }
            if (e.Column == (view.ListViewItemSorter as ListViewColumnSorter).SortColumn)
            {
                if ((view.ListViewItemSorter as ListViewColumnSorter).Order == SortOrder.Ascending)
                {
                    (view.ListViewItemSorter as ListViewColumnSorter).Order = SortOrder.Descending;
                }
                else
                {
                    (view.ListViewItemSorter as ListViewColumnSorter).Order = SortOrder.Ascending;
                }
            }
            else
            {
                (view.ListViewItemSorter as ListViewColumnSorter).SortColumn = e.Column;
                (view.ListViewItemSorter as ListViewColumnSorter).Order = SortOrder.Ascending;
            }
            ((ListView)sender).Sort();
        }

        /// <summary>
        /// 内部类
        /// Nested Types
        /// </summary>
        public class ListViewColumnSorter : IComparer
        {

            #region 变量

            // Fields
            private int ColumnToSort = 0;
            private CaseInsensitiveComparer ObjectCompare = new CaseInsensitiveComparer();
            private SortOrder OrderOfSort = SortOrder.None;

            #endregion

            #region 属性

            // Properties
            public SortOrder Order
            {
                get
                {
                    return this.OrderOfSort;
                }
                set
                {
                    this.OrderOfSort = value;
                }
            }

            public int SortColumn
            {
                get
                {
                    return this.ColumnToSort;
                }
                set
                {
                    this.ColumnToSort = value;
                }
            }

            #endregion

            #region 方法

            // Methods
            /// <summary>
            /// 依据double类型列的值比较
            /// </summary>
            /// <param name="x">ListViewItem</param>
            /// <param name="y">ListViewItem</param>
            /// <returns></returns>
            public int Compare(object x, object y)
            {
                ListViewItem item = (ListViewItem)x;
                ListViewItem item2 = (ListViewItem)y;
                int num = this.ObjectCompare.Compare(ConvertHelper.ObjectToDouble(item.SubItems[this.ColumnToSort].Text), ConvertHelper.ObjectToDouble(item2.SubItems[this.ColumnToSort].Text));
                if (this.OrderOfSort == SortOrder.Ascending)
                {
                    return num;
                }
                if (this.OrderOfSort == SortOrder.Descending)
                {
                    return -num;
                }
                return 0;
            }
            #endregion

        }
    #endregion

    }
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#获取URL参数发布时间:2022-07-13
下一篇:
如何使用VisualC#加密和解密文件发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap