帮助解释代码的工作原理 [英] Help Explain how code works
问题描述
有人可以向我解释一下吗?
Can Someone please explain this to me?
newPanel.Top = If(p.Parent.Controls.OfType(Of cardPanel).Count > 0, p.Parent.Controls.OfType(Of cardPanel).Max(Function(cp) cp.Bottom) + 6, 19)
例如,此panel1_click位于Panel或Flowlayoutpanel内。如何将Flowlayoutpanel或面板设置为其父级?
这是完整代码:
for example, this panel1_click is inside an Panel or Flowlayoutpanel. How do I set the Flowlayoutpanel or panel as its parent?
This is the full code:
Private Sub Panel1_Click(sender As Object, e As EventArgs) Handles Panel1.Click
Dim retString As String = InputBox("Enter Some Info", "Info...", "", -1, -1)
If retString.Trim <> "" Then
Dim p As Panel = DirectCast(sender, Panel)
Dim newPanel As New cardPanel
newPanel.Left = 6
newPanel.Top = If(p.Parent.Controls.OfType(Of cardPanel).Count > 0, p.Parent.Controls.OfType(Of cardPanel).Max(Function(cp) cp.Bottom) + 6, 19)
newPanel.Width = p.Width
newPanel.Height = 36
newPanel.BackColor = Color.White
newPanel.percentage = 0
newPanel.Text = retString
newPanel.Name = "Card" & x.ToString
p.Parent.Controls.Add(newPanel)
panels.Add(newPanel)
p.Top = newPanel.Bottom + 6
x += 1
AddHandler newPanel.MouseDown, AddressOf childs_MouseDown
Me.FlowLayoutCard.AllowDrop = True
End If
End Sub
bytheway .. cardpanel是一个组件:
代码:
bytheway.. cardpanel is a component:
Codes:
Public Class cardPanel
Inherits Panel
Private _percentage As Integer
Public Property percentage() As Integer
Get
Return _percentage
End Get
Set(ByVal value As Integer)
_percentage = value
End Set
End Property
Private _text As String
Public Shadows Property Text() As String
Get
Return _text
End Get
Set(ByVal value As String)
_text = value
End Set
End Property
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
e.Graphics.FillRectangle(New SolidBrush(Color.FromArgb(212, 157, 93)), New Rectangle(0, 0, CInt((MyBase.Width / 100) * percentage), 6))
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
e.Graphics.DrawString(Text, MyBase.Font, Brushes.Black, New Rectangle(Point.Empty, MyBase.Size), sf)
MyBase.OnPaint(e)
End Sub
End Class
推荐答案
奇怪的If语句是三元运营商。您可以将其视为
That strange If statement is a "ternary operator". You could think of it as
If (p.Parent.Controls.OfType(Of cardPanel).Count > 0) Then
return p.Parent.Controls.OfType(Of cardPanel).Max(Function(cp) cp.Bottom) + 6
Else
return 19
EndIf
此外,还有一个Linq表达式 .Max(Function(cp)cp.Bottom)
在该上下文中,它意味着在Controls枚举中获取给定类型的任何控件的Buttom值的最大值。 Linq将枚举控件,检查类型,如果没问题,请查看Buttom值,然后查看最大值。
函数(cp)cp.Buttom
是最大函数的参数:使用子句 OfType(Of cardPanel)
之前,Linq期望一个cardPanel对象,它获取名称cp,以及要查找的属性是cp的Buttom属性。
对于当前.Net的新用户来说肯定不那么容易理解,它是一种更先进的编码风格。
Furthermore, there is a Linq expression .Max(Function(cp) cp.Bottom)
In that context, it means to get the maximum of the Buttom value of any control of the given type in the Controls enumeration. Linq will enumerate the controls, check the type, and if it is ok, look at the Buttom value, and thake the maximum.
Function(cp) cp.Buttom
is the argument for the maximum function: with the clause OfType(Of cardPanel)
before, Linq expects a cardPanel object, which gets the name cp, and the property to look for is the Buttom property of cp.
Surely not so easy to understand for someone new to current .Net, it's a more "advanced" coding style.
这篇关于帮助解释代码的工作原理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!