简体   繁体   English

为什么New()在我继承的控件上触发两次? (的WinForms)

[英]Why is New() firing twice on my inherited controls? (winforms)

Step 1 : Create inhered control class 第1步 :创建内联控件类

Public Class Test_Control
    Inherits ListBox

    Public Sub New()
        Items.Add("test")
    End Sub
End Class

Step 2 : Drag class to form in the designer 第2步 :在设计器中将类拖到窗体中

在此输入图像描述

Step 3 : Run the project 第3步 :运行项目

Result : 结果

在此输入图像描述

Why is this happening?! 为什么会这样?! I am completely stumped here.. I have googled and googled and I cannot find any solution or answer to this. 我完全被困在这里..我用谷歌搜索和谷歌搜索,我找不到任何解决方案或答案。

This is causing some major issues for me. 这给我带来了一些重大问题。 I am simply trying to add an initial "Select one..." option to every newly created Combobox. 我只是想为每个新创建的Combobox添加一个初始的“Select one ...”选项。 Same thing happens with every inherited control class, regardless of control type (textbox/combobox/listbox/etc). 每个继承的控件类都会发生同样的事情,无论控件类型如何(textbox / combobox / listbox / etc)。

Same thing happens if I use a message box within New(). 如果我在New()中使用消息框,也会发生同样的事情。 Two message boxes appear as soon as I run my application. 运行应用程序后会立即显示两个消息框。

在此输入图像描述

You need to tell the designer to not serialize the items collection: 您需要告诉设计者不要序列化项目集合:

Public Class Test_Control
  Inherits ListBox

  Public Sub New()
    Items.Add("test")
  End Sub

  <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
  Public Shadows ReadOnly Property Items As ListBox.ObjectCollection
    Get
      Return MyBase.Items
    End Get
  End Property
End Class

As far as the two message boxes go, MessageBoxes are just not a good debugging tool. 就两个消息框而言,MessageBoxes不是一个好的调试工具。 You are probably getting the WinForms designer calling new while the runtime calling new, too (or something like that). 您可能正在让WinForms设计器调用new,而运行时调用new(或类似的东西)。

The first test is from the designer and you are adding a second one in the constructor. 第一个test来自设计器,您在构造函数中添加第二个test

Either remove the test from the designer or clear the items in the constructor before adding, like this: 在添加之前从设计器中删除test或清除构造函数中的项目,如下所示:

Items.Clear()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 继承的 winforms 控件未编译 - Inherited winforms controls not compiling 防止Winforms Designer生成继承控件的属性值 - Prevent Winforms Designer from Generating Property Values for Inherited Controls 为什么WebBrowser_DocumentCompleted()会触发两次? - Why is WebBrowser_DocumentCompleted() firing twice? Visual Studio - 继承控件的新“默认”属性值 - Visual Studio - new “default” property values for inherited controls Winforms中的简单组合框-触发两次(使用鼠标展开组合框并使用键盘选择一个项目) - Simple Combobox in Winforms - Firing twice (Use mouse to expand combobox and select a item using keyboard) 为什么使用“新的NetworkCredential(用户名,密码)”不适用于我的网站的基本身份验证(来自WinForms C#应用程序)? - why is use of “new NetworkCredential(username, password)” not working for Basic Authentication to my website (from a WinForms C# app)? WinForms设计器呈现控件之前如何运行初始化代码? - How to run initialization code before the WinForms designer renders my controls? 当用户放大Excel时,为什么禁用WinForms控件? - Why are WinForms controls disabled when the user zooms in Excel? 为什么你可以在WinForms中交叉线程添加控件,而不是WPF? - Why can you cross thread adding controls in WinForms, but not WPF? 将继承的控件添加到面板 - Add Inherited Controls to a Panel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM