我有一个(字符串)列表,其中包含一些名称。我有另一个包含后缀的 List(of String),我希望可以使用 LINQ 来提取以该列表中的任何后缀结尾的名称列表。
我可以使用一对嵌套的 FOR..EACH 循环以编程方式完成此操作,但希望使用 LINQ 找到一种更简洁的方法。
我尝试了几种不同的 .Any 和 .Contains 方法,但没有成功。
我有一个(字符串)列表,其中包含一些名称。我有另一个包含后缀的 List(of String),我希望可以使用 LINQ 来提取以该列表中的任何后缀结尾的名称列表。
我可以使用一对嵌套的 FOR..EACH 循环以编程方式完成此操作,但希望使用 LINQ 找到一种更简洁的方法。
我尝试了几种不同的 .Any 和 .Contains 方法,但没有成功。
作为我用 VB.NET 表单编写的程序的一部分,可以选择删除数据库的内容。此功能不打算在最终版本中经常使用,我不希望用户意外单击该按钮并在不想删除内容时删除内容。
我认为带有“是”和“否”按钮的简单消息框不会解决这个问题,因为我听说有几个人在程序中做了他们不想做的事情,即使采取了这样的措施。因此,我希望弹出的消息框有一个“否”按钮和一个复选框,需要勾选该复选框才能显示“是”按钮。但是,不存在具有此功能的默认消息框。经过进一步研究,我决定创建一个定制的。它在大多数情况下都有效,但是,无论我是否勾选复选框并按“是”或是否按“否”,它都会清除数据库。
下面是用于清除数据库的代码:
Private Sub btnClearDatabase_Click(sender As Object, e As EventArgs) Handles btnClearDatabase.Click
'Opens the Clear Database dialog to confirm if the user wants to clear the database.
ClearDatabase.ShowDialog()
If ClearDatabase.DialogResult.OK Then
'Clears Database. Code not shown
End If
End Sub
我将“yes”按钮的 DialogResult 属性设置为“确定”,因此我假设 If 语句中的代码仅在按下该按钮时才会运行。但无论我是否按下它,它都会运行。当我打开对话框时它会运行。为什么要这样做?
这可能与 If 语句代码中的警告有关:
BC42025 通过实例访问共享成员、常量成员、枚举成员或嵌套类型;不会评估限定表达式。
重写If ClearDatabase.DialogResult.OK Then
为If DialogResult.Ok Then
消除了警告,但运行它仍然有相同的结果。我真的很困惑为什么要这样做。
编辑:为了澄清,代码在对话框打开时不会运行。它在对话框关闭时运行。
Dim col As Color 'the color I check
Dim ref As Byte 'the color what I expect
Select Case col.R
Case ref
'code
Case ref - 3 To ref + 3
'code
Case ref - 6 To ref + 6
'code
Case ref - 10 To ref + 10
'code
Case Else
'code
End Select
Ref - X 可以 < 0(错误),或者 ref + X 可以 >255(错误)。
有没有一种快速方法可以避免错误而无需大量 If...then...else?
我正在尝试借助第二个 BindingList 类通过文本框过滤列表 datagridview 并设置为 vb.net 中的 bindingsource.datasource 。
问题是,当我过滤两个事件文本框,然后在其中一个文本框中清除或空白时,datagridview 的状态不处于过滤器中的状态。
带绑定列表的结果过滤器
应该是当我在文本框 criteria2 中清除时,datagridview 应保持引用文本框 criteria1 的过滤器状态,如下图所示,屏幕截图应将过滤器保留在 criteria1 中A
我的代码有问题吗?请指导我
类 SortableBindingList
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
''' <summary>
''' Provides a generic collection that supports data binding and additionally supports sorting.
''' See http://msdn.microsoft.com/en-us/library/ms993236.aspx
''' If the elements are IComparable it uses that; otherwise compares the ToString()
''' </summary>
''' <typeparam name="T">The type of elements in the list.</typeparam>
Public Class SortableBindingList(Of T As Class)
Inherits BindingList(Of T)
Private _isSorted As Boolean
Private _sortDirection As ListSortDirection = ListSortDirection.Ascending
Private _sortProperty As PropertyDescriptor
''' <summary>
''' Initializes a new instance of the <see cref="SortableBindingList{T}"/> class.
''' </summary>
Public Sub New()
End Sub
''' <summary>
''' Initializes a new instance of the <see cref="SortableBindingList{T}"/> class.
''' </summary>
''' <param name="list">An <see cref="T:System.Collections.Generic.IList`1" /> of items to be contained in the <see cref="T:System.ComponentModel.BindingList`1" />.</param>
Public Sub New(ByVal list As IList(Of T))
MyBase.New(list)
End Sub
''' <summary>
''' Gets a value indicating whether the list supports sorting.
''' </summary>
Protected Overrides ReadOnly Property SupportsSortingCore As Boolean
Get
Return True
End Get
End Property
''' <summary>
''' Gets a value indicating whether the list is sorted.
''' </summary>
Protected Overrides ReadOnly Property IsSortedCore As Boolean
Get
Return _isSorted
End Get
End Property
''' <summary>
''' Gets the direction the list is sorted.
''' </summary>
Protected Overrides ReadOnly Property SortDirectionCore As ListSortDirection
Get
Return _sortDirection
End Get
End Property
''' <summary>
''' Gets the property descriptor that is used for sorting the list if sorting is implemented in a derived class; otherwise, returns null
''' </summary>
Protected Overrides ReadOnly Property SortPropertyCore As PropertyDescriptor
Get
Return _sortProperty
End Get
End Property
''' <summary>
''' Removes any sort applied with ApplySortCore if sorting is implemented
''' </summary>
Protected Overrides Sub RemoveSortCore()
_sortDirection = ListSortDirection.Ascending
_sortProperty = Nothing
_isSorted = False
End Sub
''' <summary>
''' Sorts the items if overridden in a derived class
''' </summary>
''' <param name="prop"></param>
''' <param name="direction"></param>
Protected Overrides Sub ApplySortCore(ByVal prop As PropertyDescriptor, ByVal direction As ListSortDirection)
_sortProperty = prop
_sortDirection = direction
Dim list As List(Of T) = TryCast(Items, List(Of T))
If list Is Nothing Then
Return
End If
list.Sort(AddressOf Compare)
_isSorted = True
'fire an event that the list has been changed.
OnListChanged(New ListChangedEventArgs(ListChangedType.Reset, -1))
End Sub
Private Function Compare(ByVal lhs As T, ByVal rhs As T) As Integer
Dim result = OnComparison(lhs, rhs)
'invert if descending
If _sortDirection = ListSortDirection.Descending Then
result = -result
End If
Return result
End Function
Private Function OnComparison(ByVal lhs As T, ByVal rhs As T) As Integer
Dim lhsValue As Object = If(lhs Is Nothing, Nothing, _sortProperty.GetValue(lhs))
Dim rhsValue As Object = If(rhs Is Nothing, Nothing, _sortProperty.GetValue(rhs))
If lhsValue Is Nothing Then
Return If(rhsValue Is Nothing, 0, -1) 'nulls are equal
End If
If rhsValue Is Nothing Then
Return 1 'first has value, second doesn't
End If
If TypeOf lhsValue Is IComparable Then
Return DirectCast(lhsValue, IComparable).CompareTo(rhsValue)
End If
If lhsValue.Equals(rhsValue) Then
Return 0 'both are the same
End If
'not comparable, compare ToString
Return lhsValue.ToString().CompareTo(rhsValue.ToString())
End Function
End Class
Public Class Form4
Private StockCardsCache As List(Of StockCards)
Private _criteriasBindingList As New SortableBindingList(Of StockCards)()
Private bindingSource As BindingSource = Nothing
Private Sub LoadData()
StockCardsCache = New List(Of StockCards) From {
New StockCards() With {
.Invono = "PI0001",
.InvoDate = New DateTime(2024, 2, 1),
.Transaction = "PURCHASE",
.CodeProduct = "TEST 1000",
.Barcode = "1000",
.Criteria1 = "A",
.Criteria2 = "C",
.Criteria3 = "E",
.Criteria4 = "G",
.IN = 10,
.OUT = 0,
.BLC = 10
},
New StockCards() With {
.Invono = "PI0001",
.InvoDate = New DateTime(2024, 2, 1),
.Transaction = "PURCHASE",
.CodeProduct = "TEST 1000",
.Barcode = "1000",
.Criteria1 = "B",
.Criteria2 = "D",
.Criteria3 = "F",
.Criteria4 = "H",
.IN = 12,
.OUT = 0,
.BLC = 22
},
New StockCards() With {
.Invono = "PI0001",
.InvoDate = New DateTime(2024, 2, 1),
.Transaction = "PURCHASE",
.CodeProduct = "TEST 1000",
.Barcode = "1000",
.Criteria1 = "A",
.Criteria2 = "E",
.Criteria3 = "G",
.Criteria4 = "I",
.IN = 12,
.OUT = 0,
.BLC = 34
},
New StockCards() With {
.Invono = "PI0001",
.InvoDate = New DateTime(2024, 2, 1),
.Transaction = "PURCHASE",
.CodeProduct = "TEST 1000",
.Barcode = "1000",
.Criteria1 = "B",
.Criteria2 = "F",
.Criteria3 = "H",
.Criteria4 = "J",
.IN = 12,
.OUT = 0,
.BLC = 46
}
}
_criteriasBindingList = New SortableBindingList(Of StockCards)(StockCardsCache)
bindingSource = New BindingSource With {.DataSource = _criteriasBindingList}
DataGridView1.DataSource = bindingSource
End Sub
Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LoadData()
End Sub
Private Sub txtcriteria1_TextChanged(sender As Object, e As EventArgs) Handles txtcriteria1.TextChanged
Dim str = txtcriteria1.Text.Trim().ToLower()
bindingSource.DataSource = If(String.IsNullOrEmpty(str), _criteriasBindingList, _criteriasBindingList.Where(Function(c) c.Criteria1.ToLower().Contains(str)).ToList())
End Sub
Private Sub txtcriteria2_TextChanged(sender As Object, e As EventArgs) Handles txtcriteria2.TextChanged
Dim str = txtcriteria2.Text.Trim().ToLower()
bindingSource.DataSource = If(String.IsNullOrEmpty(str), _criteriasBindingList, _criteriasBindingList.Where(Function(c) c.Criteria2.ToLower().Contains(str)).ToList())
End Sub
End Class
我有一个在设计时转换的图像以获得特定的角度。XAML 如下所示。
<Image x:Name="Yelkovan" HorizontalAlignment="Left" Height="163" Margin="178,80,0,0" VerticalAlignment="Top" Width="14" Source="/k-yelkovan.png" RenderTransformOrigin="0.558,0.863">
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="-30"/>
<TranslateTransform X="27.336" Y="-7.117"/>
</TransformGroup>
</Image.RenderTransform>
</Image>
我想在运行时通过 vb.net 代码设置角度,即当按下按钮时,将其变换角度从 -30 设置为仅:+45 度。这怎么可能?
我通过格式化双精度值得到了一个字符串,例如
Dim strFormat = "#,##0.00 weeks"
Dim strDisplayText As String = dblIn.ToString(strFormat) ' dblIn is type Double
然后 strDisplayText 显示在(wpf.datagrid 的)文本框中,用户可以对其进行编辑。之后,文本应转换回双精度型。
Dim dblOut = ???(strDisplayText, strFormat)
' dblOut should be the same as dblIn, if strDisplayText wasnt changed.
if dblOut.ToString(strFormat) <> strDisplayText Then
print("failed conversion.")
end if
Double.Parse(和 TryParse)由于末尾的“周”而失败,并且 Val 无法将“,”作为组分隔符处理。
strFormat 是从文件中读取的,因此它可能是任何有效的自定义或标准数字格式(https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings或https ://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings)
因此,我正在制作一个程序,该程序将根据用户给出的时间跨度给出新的日期(例如 Windows 10/11 中内置计算器应用程序中的日期计算)。问题是,我只能设法添加年份,而日期和月份保持不变。
这是我的尝试:
Dim dateresult As String
Dim datenow As Date = Now.ToShortDateString
dateresult = datenow.AddDays(CBbDay.Text)
dateresult = datenow.AddMonths(CBbMonth.Text)
dateresult = datenow.AddYears(CBbYear.Text)
Label17.Text = dateresult
例如,如果今天的日期是 2024 年 1 月 1 日,并且 CBbDay、CBbMonth 和 CBbYear 的值为 10,则我从该代码 ( ) 中得到的结果dateresult
是 2034 年 1 月 1 日,而它应该是 2034 年 11 月 11 日。
我的代码有什么问题或者我使用了错误的函数吗?
谢谢。
在我的表单中,我有一个面板。在一个循环中,我在此面板中创建了许多标签,但速度太慢。
因此,看来在循环中创建标签列表并将它们添加到面板中后应该更快。
这是我的代码:
Dim listLabels as new list(of label)
for a=0 to 100
dim lbl as new label
label.name="label" & a.Tostring
listLabels.add(lbl)
next
myPanel.controls.addrange(listLabels)
错误:“List(of Label)”类型的值无法转换为“Control()”
你能帮我吗?
谢谢
我已经搜索了几个小时并尝试了很多东西但没有运气。
我正在使用VB.net。我的主表单(Form1)未加载另一个表单(ECR_ECO_Details)。就在几个小时前。不知何故,ECR_ECO_Details 最终出现在“我的项目”文件夹中。我将它拖回到原来的位置,与 Form1 相同的位置。没有错误、警告或喜悦。
此外,我在 Load 和 Shown 事件中有一个 MsgBox。MsgBox 不存在,所以我的程序甚至没有找到第二种形式。
它必须很简单,但这是我第一次使用 Visual Studio。我确信这是 IDE 中的东西,而不是代码。
Private Sub NewECR_Click(sender As Button, e As EventArgs)
Dim NewECRForm As New ECR_ECO_Details
NewECRForm.ShowDialog()
End Sub
与上面几乎相同的有效代码,相同的结果。曾经工作过。
Private Sub NewECR_Click(sender As Button, e As EventArgs)
Using NewECRForm As New ECR_ECO_Details()
NewECRForm.ShowDialog()
End Using
End Sub
蒂亚!