我正在尝试进行自定义验证[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) 我最近开始探索Visual Studio.我试图创建一个幻灯片菜单.更具体地说,当用户按下按钮时,子菜单会弹出到右侧.为了实现这一点,我已经放置了一个Panel
调整自己的大小.除了功能,我想添加更多的设计,使Panel
显示有点褪色.
我知道Panels
在Visual Studio中没有不透明度,但我在想是否有人知道如何实现它的方法技巧.我尝试过Picture Box
但是也没有Opacity作为属性.我避免使用Menu
visual studio提供的常规对象,因为我想添加更多设计.有任何想法吗?
我正在尝试使用具有圆角的用户控件.它没有固定的大小,但通常宽度不超过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)
这给控件圆角但是在它运行了几次之后我已经将多个用户控件添加到表单中它将导致泄漏,我将在我的用户控件上获得带有红叉的白盒.
有没有更好的方法呢?
在Visual Studio设计,在属性窗口下,您可以选择ForeColor
,BackColor
使用颜色选择器等.当您想要选择颜色时,颜色选择器会出现"自定义,Web,系统"选项卡.如果选择自定义,则可以向选择器添加新颜色,但只有底部2行可以更改,并且更改不会在控件之间保留.因此,如果您向调色板添加颜色,当您选择另一个控件并想要更改时,例如BackColor
您之前添加的颜色不存在.
有没有办法在设计师的颜色选择器控件中创建和导入一组自定义颜色?
注意:这个问题不是询问VS主题,或者是否可以在代码隐藏中将颜色实现为类.我正在寻找一种定制设计师的方法.
我正在尝试将可点击的图像/按钮添加到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
我试图改变的的dispaly颜色ComboBox
时DropdownStyle
属性为DropdownList
.当属性Dropdown
从DropdownList
颜色更改更改为.
如何控制下拉框的视图颜色?
谢谢
我有一个ProgressBar
像以下两个控件:
第一个画得很好.正如你所看到的,第二个只有一个0,它应该有两个,但另一个不能看到因为ProgressBar ForeColor
与它相同TextColor
.当ProgressBar
下面用Lime绘制时,我可以用黑色绘制文本,并在背景为黑色时在Lime中绘制文本吗?
我们DataGridView
在表单中有数据.为了使我们增加了快速搜索TextBox
到DataGridView.Controls
并突出其包含从文本框文本的单元格.
但是有一个问题.DataGridView的消耗Left
箭头←,Right
箭头→,Home和End(有或无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) 我有一个使用 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.Nullable
1[NodaTime.LocalDateTime]”时出错。路径“folderExpectedDate”,第 16 行,位置 45。 ---> System.ArgumentException:无法从 System.DateTime 强制转换或转换为 NodaTime.LocalDateTime。
请注意,在如上所述调用 DeserializeObject 时,导致上述异常的完全相同的 JSON 工作正常。 …
以下代码将转换TagBuilder
为String
。恰恰相反?如何将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# ×10
.net ×9
winforms ×8
datagridview ×2
gdi+ ×2
.net-core ×1
asp.net-core ×1
dropdownbox ×1
gdi ×1
json.net ×1
opacity ×1
panel ×1
validation ×1