小编Rez*_*aei的帖子

C#自定义验证唯一属性 - 泛型类

我正在尝试进行自定义验证[IsUnique].检查属性值是否唯一并返回正确的消息.

这是我的代码,但这只适用于指定的类,是否可以通过元数据来获取正确的类?

public class ArticleMetaData
    {
        [Required(AllowEmptyStrings = false)]
        [IsUnique("Name")]
        public String Name{ get; set; }      
    }
Run Code Online (Sandbox Code Playgroud)

我的自定义验证:

class IsUnique : ValidationAttribute
    {
        public IsUnique(string propertyNames)
        {
            this.PropertyNames = propertyNames;
        }

        public string PropertyNames { get; private set; }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {

            var myproperty = validationContext.ObjectType.GetProperty(PropertyNames);
            var value = propiedad.GetValue(validationContext.ObjectInstance, null);

            IEnumerable<String> properties;

            List<string> propertiesList = new List<string>();
            propertiesList.Add(myproperty.Name);

            var dba = new myContext();

            if (dba.Articles.Any(article => article.Name == (string)value))
            {
                return new ValidationResult("The name …
Run Code Online (Sandbox Code Playgroud)

.net c# validation entity-framework data-annotations

8
推荐指数
1
解决办法
6190
查看次数

在Visual Studio窗体窗体中在面板上使用不透明度的任何技巧?

我最近开始探索Visual Studio.我试图创建一个幻灯片菜单.更具体地说,当用户按下按钮时,子菜单会弹出到右侧.为了实现这一点,我已经放置了一个Panel调整自己的大小.除了功能,我想添加更多的设计,使Panel显示有点褪色.

我知道Panels在Visual Studio中没有不透明度,但我在想是否有人知道如何实现它的方法技巧.我尝试过Picture Box但是也没有Opacity作为属性.我避免使用Menuvisual studio提供的常规对象,因为我想添加更多设计.有任何想法吗?

.net c# panel opacity winforms

8
推荐指数
2
解决办法
2万
查看次数

如何创建带圆角的用户控件?

我正在尝试使用具有圆角的用户控件.它没有固定的大小,但通常宽度不超过120像素.

我需要用户控件及其内容(标签和表格)具有圆形边缘,看起来像一个圆形框.

我用过这段代码.

[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
    private static extern IntPtr CreateRoundRectRgn
    (
        int nLeftRect, // x-coordinate of upper-left corner
        int nTopRect, // y-coordinate of upper-left corner
        int nRightRect, // x-coordinate of lower-right corner
        int nBottomRect, // y-coordinate of lower-right corner
        int nWidthEllipse, // height of ellipse
        int nHeightEllipse // width of ellipse
    );

    public static System.Drawing.Region GetRoundedRegion(int controlWidth, int controlHeight)
    {
            return System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, controlWidth - 5, controlHeight - 5, 20, 20));
    } 
Run Code Online (Sandbox Code Playgroud)

这给控件圆角但是在它运行了几次之后我已经将多个用户控件添加到表单中它将导致泄漏,我将在我的用户控件上获得带有红叉的白盒.

有没有更好的方法呢?

.net c# gdi+ custom-controls winforms

8
推荐指数
2
解决办法
5280
查看次数

Visual Studio颜色属性编辑器中的自定义调色板

在Visual Studio设计,在属性窗口下,您可以选择ForeColor,BackColor使用颜色选择器等.当您想要选择颜色时,颜色选择器会出现"自定义,Web,系统"选项卡.如果选择自定义,则可以向选择器添加新颜色,但只有底部2行可以更改,并且更改不会在控件之间保留.因此,如果您向调色板添加颜色,当您选择另一个控件并想要更改时,例如BackColor您之前添加的颜色不存在.

有没有办法在设计师的颜色选择器控件中创建和导入一组自定义颜色?

注意:这个问题不是询问VS主题,或者是否可以在代码隐藏中将颜色实现为类.我正在寻找一种定制设计师的方法.

.net c# windows-forms-designer visual-studio winforms

8
推荐指数
1
解决办法
6411
查看次数

按钮列的DataGridView图像

我正在尝试将可点击的图像/按钮添加到datagridview按钮列.

图像/按钮将是播放或停止的图标.如果用户单击播放按钮,则启动系统上的服务,如果用户单击停止按钮,则服务停止.

我已经编写了启动和停止服务的函数.我遇到的困难是让按钮/图像显示在数据网格中并使其可点击.

这是我对代码的看法:

this.dgrdServices.RowPrePaint +=new DataGridViewRowPrePaintEventHandler(dgv_RowPrePaint);
this.dgrdServices.Rows.Add();
this.dgrdServices.Rows[0].Cells[0].Value = Image.FromFile(@"C:\users\brad\desktop\green-dot.gif"); 
this.dgrdServices.Rows[0].Cells[1].Value = "MyServer";
this.dgrdServices.Rows[0].Cells[2].Value = "MyService";
this.dgrdServices.Rows[0].Cells[3].Value = "Started";
this.dgrdServices.Rows[0].Cells[4].Value = new DataGridViewButtonCell();
this.dgrdServices.Rows[0].Cells[5].Value = "Uninstall";
Run Code Online (Sandbox Code Playgroud)

如果最好使用一个图像按钮或一个可点击的图像,我就无法解决.我也无法正确显示按钮.

谢谢Brad

.net c# datagridview winforms

8
推荐指数
1
解决办法
2万
查看次数

DropdownStyle是DropDownList时如何更改ComboBox的BackColor?

我试图改变的的dispaly颜色ComboBoxDropdownStyle属性为DropdownList.当属性DropdownDropdownList颜色更改更改为.

如何控制下拉框的视图颜色?

谢谢

.net c# dropdownbox winforms windows-applications

8
推荐指数
1
解决办法
1万
查看次数

根据BackColor反转文本颜色

我有一个ProgressBar像以下两个控件:

在此输入图像描述

第一个画得很好.正如你所看到的,第二个只有一个0,它应该有两个,但另一个不能看到因为ProgressBar ForeColor与它相同TextColor.当ProgressBar下面用Lime绘制时,我可以用黑色绘制文本,并在背景为黑色时在Lime中绘制文本吗?

.net c# gdi+ gdi winforms

8
推荐指数
1
解决办法
672
查看次数

处理DataGridView中TextBox中的导航键

我们DataGridView在表单中有数据.为了使我们增加了快速搜索TextBoxDataGridView.Controls并突出其包含从文本框文本的单元格.

但是有一个问题.DataGridView的消耗Left箭头,Right箭头,HomeEnd(有或无Shift)键,即使光标在文本框,并且用户不能更改光标处或选择从键盘文本.

TextBox生成PreviewKeyDown事件,没有更多的事情发生

简化代码:

public partial class TestForm : Form
{
    public TestForm()
    {
        InitializeComponent();
        Width = 400;
        Height = 400;

        var txt = new TextBox { Dock = DockStyle.Bottom, BackColor = Color.Khaki };
        var dgv = new DataGridView
        {
            Dock = DockStyle.Fill,
            ColumnCount = 3,
            RowCount = 5
        };
        dgv.Controls.Add(txt);            
        Controls.Add(dgv);

        dgv.PreviewKeyDown += DgvOnPreviewKeyDown;
        dgv.KeyDown …
Run Code Online (Sandbox Code Playgroud)

.net c# datagridview winforms

8
推荐指数
2
解决办法
558
查看次数

如何设置 HttpClient (WinForms) 的默认 json 序列化设置

我有一个使用 Web API 后端的 WinForms 应用程序,我希望所有 HTTP 请求/响应都使用特定的 json SerializerSettings。在服务器端,只需在 Global.asax.cs 中执行此操作即可轻松实现:

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.DateParseHandling = DateParseHandling.None;
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
Run Code Online (Sandbox Code Playgroud)

在客户端,我尝试了以下方法:

JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
    Formatting = Formatting.Indented,
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
    DateParseHandling = DateParseHandling.None
}.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
Run Code Online (Sandbox Code Playgroud)

当我显式使用 JsonConvert 时,这有效,例如:

var someVm = JsonConvert.DeserializeObject<SomeVm>(jsonString);
Run Code Online (Sandbox Code Playgroud)

但对于使用HttpClient.GetAsync / PostAsAsync等调用不生效,导致异常:

将值 2/24/2018 4:00:00 PM 转换为类型“System.Nullable 1[NodaTime.LocalDateTime]'. Path 'folderExpectedDate', line 16, position 45. Newtonsoft.Json.JsonSerializationException: Error converting value 2/24/2018 4:00:00 PM to type 'System.Nullable1[NodaTime.LocalDateTime]”时出错。路径“folderExpectedDate”,第 16 行,位置 45。 ---> System.ArgumentException:无法从 System.DateTime 强制转换或转换为 NodaTime.LocalDateTime。

请注意,在如上所述调用 DeserializeObject 时,导致上述异常的完全相同的 JSON 工作正常。 …

.net c# json.net winforms asp.net-web-api

8
推荐指数
1
解决办法
1万
查看次数

网络核心:将字符串转换为TagBuilder

以下代码将转换TagBuilderString。恰恰相反?如何将a反转String为a TagBuilder?寻找解决方案。

将IHtmlContent / TagBuilder转换为C#中的字符串

public static string GetString(IHtmlContent content)
{
    using (var writer = new System.IO.StringWriter())
    {        
        content.WriteTo(writer, HtmlEncoder.Default);
        return writer.ToString();
    } 
}       
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core-mvc .net-core asp.net-core

8
推荐指数
1
解决办法
292
查看次数