vb.net 在 VB 2010 中的表单上显示输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13804473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display output on the form in VB 2010
提问by John White
I'm designing a windows form. I have output to be displayed on the form it self. Tried using print, but it is not working. How do I do that?
我正在设计一个窗体。我有输出要显示在它自己的表单上。尝试使用打印,但它不起作用。我怎么做?
I'M NOT PRINTING THE FORM.
我不是在打印表格。
ADDED: I need to display 3 numbers with text string next to each number. I want to do this in a way that it shows in the form or label in the form without overwriting the previous results.
添加:我需要显示 3 个数字,每个数字旁边都有文本字符串。我想以一种显示在表单中的方式或表单中的标签来执行此操作,而不会覆盖以前的结果。
example:
例子:
3 (wrong) 1 (right) 8 (wrong)
2 (wrong) 1 (right) 5 (right)
9 (right) 1 (right) 5 (right)
ADDED: Thanks for the help everyone. one more question and i think i'm good.
补充:感谢大家的帮助。还有一个问题,我觉得我很好。
I was thinking of doing something like this inside a loop, problem is I can't add a string and an int together to make a new var: Xnum1 = Xnum1 + 50
我正在考虑在循环中做这样的事情,问题是我不能将一个字符串和一个 int 添加在一起来创建一个新的 var:Xnum1 = Xnum1 + 50
Xnum2 = Xnum1 + ".0F"
Ynum1 = Ynum1 + 50
Ynum2 = Ynum1 + ".0F"
回答by Steven Doggart
In VB6 you could use the Printstatement to draw to the surface of the form. In VB.NET, however, you should be using the Form.CreateGraphicsmethod to create a new Graphicsobject that can be used to draw to the form's surface. For instance:
在 VB6 中,您可以使用该Print语句绘制到窗体的表面。但是,在 VB.NET 中,您应该使用该 Form.CreateGraphics方法创建一个Graphics可用于绘制到窗体表面的新对象。例如:
Private Sub PrintText(text As String, x As Single, y As Single)
Dim g As Graphics = Me.CreateGraphics()
g.DrawString(text, New Font("Arial", 16), New SolidBrush(Color.Black), New PointF(x, y))
End Sub
That would be the closest equivalent to using the VB6 Printstatement like that.
这将是最接近使用 VB6Print语句的等价物。
However, I would strongly recommend using a control to display the data. It looks like for the data you need to display, a simple multi-line text box or label would be sufficient. For instance:
但是,我强烈建议使用控件来显示数据。看起来对于您需要显示的数据,一个简单的多行文本框或标签就足够了。例如:
Private Sub AppendResult(index As Integer, right As Boolean)
If right Then
TextBox1.Text = TextBox1.Text & " " & index.ToString() & " (right)"
Else
TextBox1.Text = TextBox1.Text & " " & index.ToString() & " (wrong)"
End If
End Sub
If you want to get more fancy, you could look into using a data grid, a list box, a list view, or even a table layout control instead.
如果您想变得更花哨,您可以考虑使用数据网格、列表框、列表视图,甚至是表格布局控件。
回答by Nianios
I believe that the most efficient way is to use a tableLayoutPanelwith 6 columns. Add in each cell a label showing in the first cell the number, in the second the indicator for that number (right/wrong). Do the same for second and third number.(second number = third and fourth cell, third number =fifth and sixth cell)
我认为最有效的方法是使用6 列的tableLayoutPanel。在每个单元格中添加一个标签,在第一个单元格中显示数字,在第二个单元格中添加该数字的指示符(正确/错误)。对第二个和第三个数字执行相同操作。(第二个数字 = 第三个和第四个单元格,第三个数字 = 第五个和第六个单元格)
For the next set of numbers you can add a new row with with labels in each cell.
对于下一组数字,您可以在每个单元格中添加一个带有标签的新行。
I'll add some code to make my answer more professional.
我会添加一些代码,使我的回答更专业。
First you add the tableLayoutPanel in your form. You size it as you like (make its width, long enough to handle the data) You delete the lastRow and then you add columns (you want to have 6 columns). You edit the size of the columns to be Percentage =16.67%
首先在表单中添加 tableLayoutPanel。您可以根据需要调整大小(使其宽度足够长以处理数据)您删除 lastRow 然后添加列(您希望有 6 列)。您将列的大小编辑为 Percentage =16.67%
Public Class Form1
Private rowIndex
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
For i = 0 To 4 Step 2
Dim val As Integer = 3
AddLabels(val, i, 0)
Next
For i = 1 To 5 Step 2
Dim val As String = "right"
AddLabels(val, i, 0)
Next
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
rowIndex = rowIndex + 1
Me.TableLayoutPanel1.RowStyles.Add(New RowStyle(SizeType.Absolute, 30))
Me.TableLayoutPanel1.Height = Me.TableLayoutPanel1.Height + 30
For i = 0 To 4 Step 2
Dim val As Integer = 3 'here you have to put your number
AddLabels(val, i, rowIndex)
Next
For i = 1 To 5 Step 2
Dim val As String = "right" 'here you have to put your indicator
AddLabels(val, i, rowIndex)
Next
End Sub
Private Sub AddLabels(ByVal lblValue As String, ByVal column As Integer, ByVal row As Integer)
Dim lblHeader As New Label
lblHeader.AutoSize = True
lblHeader.Margin = New Padding(0)
lblHeader.BackColor = Color.Transparent
lblHeader.TextAlign = ContentAlignment.MiddleLeft
lblHeader.Dock = DockStyle.None
lblHeader.Text = lblValue
'Put the lblHeader in the right cell
Dim lblHeaderPos As New TableLayoutPanelCellPosition(column, row)
TableLayoutPanel1.SetCellPosition(lblHeader, lblHeaderPos)
TableLayoutPanel1.Controls.Add(lblHeader)
End Sub
Let me know if you facing any problems.
如果您遇到任何问题,请告诉我。
Also if you don't know how many rows you will add, put the tableLyoutPanel inside a panel. Make the panel's property AutoScroll=True and then you can add infinite number of new rows.
此外,如果您不知道要添加多少行,请将 tableLyoutPanel 放在面板中。使面板的属性 AutoScroll=True 然后您可以添加无限数量的新行。

