CalculatedDielectricSpacing

CalculatedDielectricSpacing

本文介绍了TableLayoutPanel-绘制矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我正在尝试根据用户输入创建一组tablelayoutpanel控件。 我可以创建第一个控件并在我放在tablelayoutpanel的一行中的usercontrol中绘制矩形。 问题似乎是当我尝试创建
另一个tablelayoutpanel时。 我尝试将每个tablelayoutpanel控件命名为:tablelayoutpanel1,  tablelayoutpanel2,  tablelayoutpanel3 ...这是为了使每个面板都是唯一的。  正如我所说,第一个tablelayoutpanel已创建并按预期工作
。 创建下一个tablelayoutpanel时,  然后usercontrol应该创建一个新的tablelayoutpanel但是窗口表单只是清除自己,我得到一个空白屏幕。  另一个问题是搜索代码 找到
新的tablelayoutpanel对象似乎找不到代码应该刚刚创建的新tablelayoutpanel。  请参阅下面的代码示例。

I am trying to create a set of tablelayoutpanel controls base off of user inputs.  I can create to first control and paint rectangles in the usercontrol I have placed in a row of the tablelayoutpanel.  The problem seems to be when i try to create another tablelayoutpanel.  I try to name each tablelayoutpanel control as such: tablelayoutpanel1,  tablelayoutpanel2, tablelayoutpanel3... this is to make each panel unique.   As I stated the first tablelayoutpanel is created and works as expected.  When the next tablelayoutpanel is created,   then usercontrol should create a new tablelayoutpanel but the Window Form simply clears itself and I get a blank screen.   The other problem is the search code  to find the new tablelayoutpanel object seems to find no new tablelayoutpanel the code should have just created.   See the code sample below.

我想了解以下内容:


  • 为什么我似乎无法创建多个TableLayoutPanel控件?
  • 有没有办法让tablelayoutpanel控件在创建下一个控件时不会消失?  我尝试解决这个问题是使用List数组。

  • 如何在调整WindowForm大小时保持TableLayoutPanel控件不会消失?
         

Public Shared Myrectangles As List(Of Myrectangle) = New List(Of Myrectangle)

Dim oObj As Object


LayoutForm.CreateTableLayoutPanel(SectionNo_NumericUpDown.Value - 1)'创建新的TableLayoutPanel



'这循环查找并绘制了许多松树部分的每个部分。
对于数字As Integer = 1到ListCount步骤1

notAmazing =" TableLayoutPanel" &安培; number'唯一部分名称

LayoutForm.NewTableLayoutPanel = LayoutForm.Controls.Find(notAmazing,True).FirstOrDefault()'查找带有unque的tablelayoutpanel名称

使用DirectCast(LayoutForm。 NewTableLayoutPanel.Controls(0),LayoutUserControl)

.DrawSection(number)'用来自用户的矩形信息创建一个管道的新部分

结束使用

使用DirectCast(LayoutForm.NewTableLayoutPanel.Controls(1),TextBox)

.Text = SectionNo_NumericUpDown.Value - 1'标记管道部分

以$结尾b
$ b下一步'循环到下一部分





Public Sub CreateTableLayoutPanel(ByRef PanelNumber As Integer)

NewTableLayoutPanel = New TableLayoutPanel'创建新控件

NewTableLayoutPanel.Controls.Add (New LayoutUserControl)'添加用户控件
NewTableLayoutPanel.Controls.Add(新TextBox)'添加文本框
NewTableLayoutPanel.Enabled = False'强制不可编辑

使用DirectCast( NewTableLayoutPanel.Controls(0),LayoutUserControl)

.Name =" LayoutUserControl" &安培; Convert.ToString(PanelNumber)'为TableLayoutPanel创建一个唯一的名称
.Dock = DockStyle.Fill'控制以填充第1行
.Visible = True'使控件可见

结束使用

使用DirectCast(NewTableLayoutPanel.Controls(1),TextBox)

.BorderStyle = BorderStyle.None'使TextBox不带边框
.BackColor = SystemColors。控制'使颜色与背景颜色相同
.Dock = DockStyle.Fill'文本框以填充行
.ForeColor = Color.Red'使文本颜色为红色
.Font =新字体(" Ariel",10,FontStyle.Bold)'Make Font Ariel,size 10 and Font Bold
.Text = Convert.ToString(PanelNumber)'将文本添加到TextBox
.TextAlign = Horizo​​ntalAlignment.Center'对齐到中心

结束时

结束子


公共子DrawLine(ByRef ID为Double,OD为D ouble,Name As String,SectionLength As Double)

Dim ZoomFactor As Double = 1
Dim tablelayoutcount As Integer = CInt(EditCoaxLine.SectionNo_NumericUpDown.Value - 1)
Dim InchToPixelConvertion As Double = 500
Dim Yvalue As Double = 0
Dim Xposition As Double
Dim CalculatedDielectricSpacing As Double = 0
Dim GroudThickness Double = 30
Dim ContactThickness As Double = IDBasic * InchToPixelConvertion * ZoomFactor


Dim SectionLengthPixels As Double = SectionLength * InchToPixelConvertion * ZoomFactor
Dim YPositionforContact As Double =((310/2) - ((IDBasic / 2)* InchToPixelConvertion))* ZoomFactor
Dim YPositionforDielectric As Double =(((310/2) - GroudThickness) - ((IDBasic / 2)* InchToPixelConvertion))* ZoomFactor

试试
Dim g As Graphics = CreateGraphics()

'sc reen size是779宽和310 deep

如果tablelayoutcount = 0那么
Xposition = 0
Else

结束如果

GroundPen =新的SolidBrush(Color.Gray)
ContactPen =新的SolidBrush(Color.Gold)


CalculatedDielectricSpacing = Math.Abs​​((310/2) - GroudThickness) - ((IDBasic / 2)* InchToPixelConvertion)

Select Case Dielectric
Case" PTFE"
DielectricPen =新型SolidBrush(Color.White)'PTFE
案例"空气"
DielectricPen =新的SolidBrush(Color.Aquamarine)'Air
Case" Glass"
DielectricPen =新的SolidBrush(Color.DarkSeaGreen)'Glass
Case" Ultem"
DielectricPen = New SolidBrush(Color.Beige)'Ultem
Case" Torlon"
DielectricPen =新的SolidBrush(Color.LawnGreen)'Torlon
案例'Delrin"
DielectricPen = New SolidBrush(Color.Cornsilk)'Delrin
Case" User Entry"
DielectricPen =新的SolidBrush(Color.Maroon)'用户输入
结束选择

Dim rect作为新矩形(Xposition,0,SectionLengthPixels,GroudThickness)
Dim rect1作为新矩形(Xposition,GroudThickness,SectionLengthPixels,CalculatedDielectricSpacing)
Dim rect2 As New Rectangle(Xposition,YPositionforContact,SectionLengthPixels,ContactThickness)
Dim rect3 As New Rectangle(Xposition,ContactThickness + YPositionforContact,SectionLengthPixels,CalculatedDielectricSpacing)
Dim rect4 As New Rectangle(Xposition,ContactThickness + YPositionforContact + CalculatedDielectricSpacing,SectionLengthPixels,GroudThickness)


如果tablelayoutcount = 1则

LayoutForm.NewTableLayoutPanel .Size =新大小(rect4.Width,344)

否则

LayoutForm.NewTableLayoutPanel。 Location = New Point(rect4.Width,344)

End if

Myrectangles.Add(New Main.Myrectangle(tablelayoutcount,GroundPen,rect))
Myrectangles .Add(New Main.Myrectangle(tablelayoutcount,DielectricPen,rect1))
Myrectangles.Add(New Main.Myrectangle(tablelayoutcount,ContactPen,rect2))
Myrectangles.Add(New Main.Myrectangle(tablelayoutcount, DielectricPen,rect3))
Myrectangles.Add(New Main.Myrectangle(tablelayoutcount,GroundPen,rect4))


Catch ex As Exception
MessageBox.Show(ex 。消息)
结束尝试

结束子

Dim oObj As Object..LayoutForm.CreateTableLayoutPanel(SectionNo_NumericUpDown.Value - 1) 'Create new TableLayoutPanel. ..' This loops to find and draw each section of numerous pine sections.For number As Integer = 1 To ListCount Step 1 notAmazing = "TableLayoutPanel" & number 'Unique Section Name LayoutForm.NewTableLayoutPanel = LayoutForm.Controls.Find(notAmazing, True).FirstOrDefault() 'Find tablelayoutpanel with unque Name With DirectCast(LayoutForm.NewTableLayoutPanel.Controls(0), LayoutUserControl) .DrawSection(number) 'Create a new section of the Pipe with rectangle info from user End With With DirectCast(LayoutForm.NewTableLayoutPanel.Controls(1), TextBox) .Text = SectionNo_NumericUpDown.Value - 1 'Labels the Section of Pipe End With Next 'Loop to next section... Public Sub CreateTableLayoutPanel(ByRef PanelNumber As Integer) NewTableLayoutPanel = New TableLayoutPanel 'Create new control NewTableLayoutPanel.Controls.Add(New LayoutUserControl) 'Add User Control NewTableLayoutPanel.Controls.Add(New TextBox) 'Add Text Box NewTableLayoutPanel.Enabled = False 'Force non-editable With DirectCast(NewTableLayoutPanel.Controls(0), LayoutUserControl) .Name = "LayoutUserControl" & Convert.ToString(PanelNumber) 'Create a unique Name for TableLayoutPanel .Dock = DockStyle.Fill 'Control to fill Row #1 .Visible = True 'Make control visible End With With DirectCast(NewTableLayoutPanel.Controls(1), TextBox) .BorderStyle = BorderStyle.None 'Make TextBox without Border .BackColor = SystemColors.Control 'Make color same as background color .Dock = DockStyle.Fill 'TextBox to fill row .ForeColor = Color.Red 'Make Text color Red .Font = New Font("Ariel", 10, FontStyle.Bold) 'Make Font Ariel, size 10 and Font Bold .Text = Convert.ToString(PanelNumber) 'Add text to TextBox .TextAlign = HorizontalAlignment.Center 'Align to Center End With End Sub Public Sub DrawLine(ByRef ID As Double, OD As Double, Name As String, SectionLength As Double) Dim ZoomFactor As Double = 1 Dim tablelayoutcount As Integer = CInt(EditCoaxLine.SectionNo_NumericUpDown.Value - 1) Dim InchToPixelConvertion As Double = 500 Dim Yvalue As Double = 0 Dim Xposition As Double Dim CalculatedDielectricSpacing As Double = 0 Dim GroudThickness As Double = 30 Dim ContactThickness As Double = IDBasic * InchToPixelConvertion * ZoomFactor Dim SectionLengthPixels As Double = SectionLength * InchToPixelConvertion * ZoomFactor Dim YPositionforContact As Double = ((310 / 2) - ((IDBasic / 2) * InchToPixelConvertion)) * ZoomFactor Dim YPositionforDielectric As Double = (((310 / 2) - GroudThickness) - ((IDBasic / 2) * InchToPixelConvertion)) * ZoomFactor Try Dim g As Graphics = CreateGraphics() 'The screen size is 779 wide and 310 deep If tablelayoutcount = 0 Then Xposition = 0 Else End If GroundPen = New SolidBrush(Color.Gray) ContactPen = New SolidBrush(Color.Gold) CalculatedDielectricSpacing = Math.Abs((310 / 2) - GroudThickness) - ((IDBasic / 2) * InchToPixelConvertion) Select Case Dielectric Case "PTFE" DielectricPen = New SolidBrush(Color.White) 'PTFE Case "Air" DielectricPen = New SolidBrush(Color.Aquamarine) 'Air Case "Glass" DielectricPen = New SolidBrush(Color.DarkSeaGreen) 'Glass Case "Ultem" DielectricPen = New SolidBrush(Color.Beige) 'Ultem Case "Torlon" DielectricPen = New SolidBrush(Color.LawnGreen) 'Torlon Case "Delrin" DielectricPen = New SolidBrush(Color.Cornsilk) 'Delrin Case "User Entry" DielectricPen = New SolidBrush(Color.Maroon) 'User Entry End Select Dim rect As New Rectangle(Xposition, 0, SectionLengthPixels, GroudThickness) Dim rect1 As New Rectangle(Xposition, GroudThickness, SectionLengthPixels, CalculatedDielectricSpacing) Dim rect2 As New Rectangle(Xposition, YPositionforContact, SectionLengthPixels, ContactThickness) Dim rect3 As New Rectangle(Xposition, ContactThickness + YPositionforContact, SectionLengthPixels, CalculatedDielectricSpacing) Dim rect4 As New Rectangle(Xposition, ContactThickness + YPositionforContact + CalculatedDielectricSpacing, SectionLengthPixels, GroudThickness) If tablelayoutcount = 1 Then LayoutForm.NewTableLayoutPanel.Size = New Size(rect4.Width, 344) Else LayoutForm.NewTableLayoutPanel.Location = New Point(rect4.Width, 344) End If Myrectangles.Add(New Main.Myrectangle(tablelayoutcount, GroundPen, rect)) Myrectangles.Add(New Main.Myrectangle(tablelayoutcount, DielectricPen, rect1)) Myrectangles.Add(New Main.Myrectangle(tablelayoutcount, ContactPen, rect2)) Myrectangles.Add(New Main.Myrectangle(tablelayoutcount, DielectricPen, rect3)) Myrectangles.Add(New Main.Myrectangle(tablelayoutcount, GroundPen, rect4)) Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub

谢谢

Nuadha

推荐答案



In这个:


In this:

   Dim g As Graphics = CreateGraphics() 

   Dim g As Graphics = CreateGraphics() 

未指定控件,因此使用表单。您将在表单上绘图,但我没有看到任何实际的绘图代码。

no control specified so the form is used. You would be drawing on the form, however I dont see any actual drawing code.



这不是绘图的工作方式:


This is not how drawing works:

  Myrectangles.Add(New Main.Myrectangle(tablelayoutcount,GroundPen,rect))

 Myrectangles.Add(New Main.Myrectangle(tablelayoutcount, GroundPen, rect))

应该是:

  g .DrawRectangle(GroundPen,rect)

 g.DrawRectangle(GroundPen, rect)

或者我错过了你在做什么?

Or am I missing what you are doing?

一个绘制在图形表面上。这就是CreateGraphics为我们提供的参考。我们绘制的内容后来由系统转移到屏幕上。

One draws on a graphics surface. That is what CreateGraphics gives us a reference to. What we draw is later transfered to the screen by the system.

您应该在用户事件中制作矩形列表,例如按钮单击或鼠标按钮(或其他方式)然后绘制后面的矩形列表,根据需要,最好在绘图事件中,图形对象作为e.graphics给我们。

You should make your list of rectangles in a user event like button click or mouse up (or some other way) and then draw the list of rectangles later, as required, preferably in the paint event where the graphics object is given to us as e.graphics.

因此,使矩形列表是一回事。然后绘制列表是另一回事。

So make the rectangle list is one thing. Then drawing the list is something else.

这是drawrectangle方法:

Here is the drawrectangle method:

https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k(System.Drawing.Graphics.DrawRectangle); K(TargetFrameworkMoniker-.NETFramework,版本%3Dv4 .5.2); k(DevLang-VB)& rd = true

以下是一个例子:

https://social.msdn.microsoft.com/Forums/vstudio/en-US/a81a4c4d-cfd1-42d1-8aa8-451ca6921926/losing-graphics-on-scrolling-picturebox?forum=vbgeneral


这篇关于TableLayoutPanel-绘制矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 17:57