【问题标题】:Type "Record" is not defined in custom class vb.net自定义类 vb.net 中未定义类型“记录”
【发布时间】:2020-09-15 14:52:13
【问题描述】:

我正在按照下面的线程来创建多行组合框。我目前正在使用 Visual Studio 2019,该线程来自 2013 年。

Any way for a combo box with 2 values per line?

我复制的代码:

    Public Class MultiLineComboBox : Inherits ComboBox
   Public Sub New()
      ' Call the base class.
      MyBase.New()

      ' Typing a value into this combobox won't make sense, so make it impossible.
      Me.DropDownStyle = ComboBoxStyle.DropDownList

      ' Set the height of each item to be twice its normal value
      ' (because we have two lines instead of one).
      Me.ItemHeight *= 2
   End Sub

   Protected Overrides Sub OnDrawItem(e As DrawItemEventArgs)
      ' Call the base class.
      MyBase.OnDrawItem(e)

      ' Fill the background.
      e.DrawBackground()

      ' Extract the Record object corresponding to the combobox item to be drawn.
      If (e.Index >= 0) Then
         Dim record As Record = DirectCast(Me.Items(e.Index), Record)

         ' Format the item's caption string.
         Dim caption As String = String.Format("ID: {0}{1}Name: {2}", record.UniqueID.ToString(), Environment.NewLine, record.Name)

         ' And then draw that string, left-aligned and vertically centered.
         TextRenderer.DrawText(e.Graphics, caption, e.Font, e.Bounds, e.ForeColor, TextFormatFlags.Left Or TextFormatFlags.VerticalCenter)
      End If

      ' Finally, draw the focus rectangle.
      e.DrawFocusRectangle()
   End Sub
End Class

我创建了一个名为 MultiLineComboBox 的类,完全按照说明进行操作,它显示“未定义类型‘记录’。”我一直在寻找有关“记录”或 DirectCast 类型的答案(例如 https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/directcast-operator),但到目前为止没有一个对我有帮助。

我的声誉目前

(添加标签 visual-studio-2019 和 .net-4.7.2 以防 VS 或 .NET 版本是问题)

【问题讨论】:

  • 该问题的答案中有大量链接,而您还没有告诉我们您使用的是哪个链接。您似乎应该自己创建了 Record 类。它显然不是标准的 .NET 类型,那么您认为它应该来自哪里?
  • @jmcilhinney 很抱歉我粘贴了错误的链接。这是正确的:stackoverflow.com/questions/15514698/…

标签: vb.net winforms combobox visual-studio-2019 .net-4.7.2


【解决方案1】:

阅读整个答案。 Record 类在此处声明;您也需要它,并且您想更改代码以匹配组合框的字段。

【讨论】:

  • 知道了。我没有通读答案提到的 OOP 部分
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-08
  • 1970-01-01
  • 2013-11-14
相关资源
最近更新 更多