Winform 中 DesignMode 返回值不正确的问题。
Winform 正确 返回值 问题
2023-09-14 08:57:24 时间
本文转载:http://blog.csdn.net/sabty/article/details/5325260
以前也曾遇到这样的问题,不过影响不大也没有去详细了解。今天又重新遇到此问题,实在太不便。经查证这是 Visual Studio 2005 的 Bug。微软对此的 Bug 描述:http://support.microsoft.com/?scid=kb;zh-cn;839202&x=10&y=15
//DesignMode存在BUG,在构造函数里面DesignMode永远都是false,
//建议:不要把代码写在此处。否则每次"重新"解决方案时候都会执行。
建议把初始化代码代码写在FormLoad事件里面或者重载 protected override void OnCreateControl()。
解决方法:
在你的 Form 控件中重写 DesignMode 属性,代码如下:
- /// <summary>
- /// 标题:获取一个值,用以指示 System.ComponentModel.Component 当前是否处于设计模式。
- /// 描述:DesignMode 在 Visual Studio 2005 产品中存在 Bug ,使用下面的方式可以解决这个问题。
- /// 详细信息地址:http://support.microsoft.com/?scid=kb;zh-cn;839202&x=10&y=15
- /// </summary>
- protected new bool DesignMode
- {
- get
- {
- bool returnFlag = false;
- #if DEBUG
- if (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime)
- {
- returnFlag = true;
- }
- else if (System.Diagnostics.Process.GetCurrentProcess().ProcessName.ToUpper().Equals("DEVENV"))
- {
- returnFlag = true;
- }
- #endif
- return returnFlag;
- }
- }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public partial class UserControl3 : UserControl
{
public UserControl3()
{
InitializeComponent();
//DesignMode存在BUG,在构造函数里面DesignMode永远都是false,
//建议:不要把代码写在此处。否则每次"重新"解决方案时候都会执行。
//if (this.DesignMode==false)
// MessageBox.Show(Application.StartupPath);
}
private bool _IsViewMode = false;
public bool IsViewMode
{
get
{
if (this.DesignMode == false)
MessageBox.Show(Application.StartupPath);
return _IsViewMode;
}
}
private void UserControl3_Load(object sender, EventArgs e)
{
if (this.DesignMode == false)
MessageBox.Show(Application.StartupPath);
}
{
public UserControl3()
{
InitializeComponent();
//DesignMode存在BUG,在构造函数里面DesignMode永远都是false,
//建议:不要把代码写在此处。否则每次"重新"解决方案时候都会执行。
//if (this.DesignMode==false)
// MessageBox.Show(Application.StartupPath);
}
private bool _IsViewMode = false;
public bool IsViewMode
{
get
{
if (this.DesignMode == false)
MessageBox.Show(Application.StartupPath);
return _IsViewMode;
}
}
private void UserControl3_Load(object sender, EventArgs e)
{
if (this.DesignMode == false)
MessageBox.Show(Application.StartupPath);
}
方法二:
Winform中自定义控件判断是否处于IDE设计模式(DesignMode,Designtime,构造函数,Load)
在设计自定义控件时,经常需要在构造函数或者Load事件中添加初始化代码,但是这些代码在进入窗体设计也会被执行,造成了设计窗口出现异常的情况。
使用下面的代码,可以让你判断出是否处于窗体设计模式,进而保证代码只会在最终用户使用时才会被执行。
public static bool IsDesignMode() { bool returnFlag = false; #if DEBUG if (LicenseManager.UsageMode == LicenseUsageMode.Designtime) { returnFlag = true; } else if (Process.GetCurrentProcess().ProcessName == "devenv") { returnFlag = true; } #endif return returnFlag; }
相关文章
- C# winform开发界面美化
- C# Winform 窗体美化(目录)
- 详解C# WinForm如何实现自动更新程序的案例分享
- Winform DataGridViewTextBoxCell 编辑添加右键菜单,编辑选中文本
- C#Winform禁止用户调整ListView的列宽
- WinForm下TextBox只允许输入数字的小例子
- 深入分析C#中WinForm控件之Dock顺序调整的详解
- 深入C#winform清除由GDI绘制出来的所有线条或图形的解决方法
- 解决C#winForm自定义鼠标样式的两种实现方法详解
- C#WinForm导出Excel方法介绍
- Winform圆形环绕的Loading动画实现代码
- win7中C#的winForm编程使用savefiledialog不能弹出保存窗体的解决方法
- WinForm项目开发中WebBrowser用法实例汇总
- C#WinForm中禁止改变窗口大小的方法
- C#Winform中实现主窗口打开登录窗口关闭的方法
- C#WinForm窗体编程中处理数字的正确操作方法