Среда Visual Basic STUDIO’2010 Professional
Как в среде VB NET
В ниже приведенных процедурах в консольном приложении
заменить вывод вместо чёрного окна
в контрол типа TextBox, имя которого, пускай, xmlDisplay?
Спасибо.
Имеем 3 файла
file Program.vb
Namespace Samples
Class Program
Shared Sub Main()
Dim dbPath As String = System.IO.Path.GetFullPath(System.IO.Path.Combine(Application.StartupPath, "..\..\Data\NORTHWIND.MDF"))
Dim sqlServerInstance As String = ".\SQLEXPRESS"
Dim connString As String = "AttachDBFileName='" + dbPath + "';Server='" + sqlServerInstance + "';user instance=true;Integrated Security=SSPI;"
Dim path As String = System.IO.Path.GetFullPath(System.IO.Path.Combine(Application.StartupPath, "..\..\Northwind.xml"))
Dim mappingSource As XmlMappingSource = XmlMappingSource.FromXml(File.ReadAllText(path))
Dim db As Northwind = New Northwind(connString, mappingSource)
db.Log = Console.Out
Samples.Sample10(db) ' как выводить, допустим в TextBox
End Sub
End Class
End Namespace
File " Samples.vb" содержит:
Public Class Samples
Public Shared Sub Sample10(ByVal db As Northwind)
' Используйте оператор группировки (Group By) и статистические функции, такие как Min() и Max(), для вычисления значений по разделам групп
Dim q = From p In db.Products _
Group p By p.CategoryID Into Group _
Select New With { _
.Category = CategoryID, _
.MinPrice = Group.Min(Function(p) p.UnitPrice), _
.MaxPrice = Group.Max(Function(p) p.UnitPrice) _
}
ObjectDumper.Write(q, 1)
End Sub
End Class
File "ObjectDumper.vb" содержит:
Public Class ObjectDumper
Public Shared Sub Write(ByVal o As Object)
Write(o, 0)
End Sub
Public Shared Sub Write(ByVal o As Object, ByVal depth As Integer)
Write(o, depth, Console.Out)
End Sub
Public Shared Sub Write(ByVal o As Object, ByVal depth As Integer, ByVal log As TextWriter)
Dim dumper As New ObjectDumper(depth)
dumper.writer = log
dumper.WriteObject(Nothing, o)
End Sub
Private Sub WriteObject(ByVal prefix As String, ByVal o As Object)
If (o Is Nothing OrElse TypeOf o Is ValueType OrElse TypeOf o Is String) Then
WriteIndent()
Write(prefix)
MsgBox("prefix = " + prefix)
WriteValue(o)
WriteLine()
ElseIf TypeOf o Is IEnumerable Then
For Each element As Object In CType(o, IEnumerable)
If (TypeOf element Is IEnumerable AndAlso Not TypeOf element Is String) Then
WriteIndent()
Write(prefix)
Write("...")
WriteLine()
If (level < depth) Then
level += 1
WriteObject(prefix, element)
level -= 1
End If
Else
WriteObject(prefix, element)
End If
Next
Else
Dim t As Type
Dim members As MemberInfo() = o.GetType.GetMembers((BindingFlags.Public Or BindingFlags.Instance))
WriteIndent()
Write(prefix)
Dim propWritten As Boolean = False
For Each m As MemberInfo In members
Dim f As FieldInfo = TryCast(m, FieldInfo)
Dim p As PropertyInfo = TryCast(m, PropertyInfo)
If (f IsNot Nothing OrElse p IsNot Nothing) Then
If propWritten Then
WriteTab()
Else
propWritten = True
End If
Write(m.Name)
Write("=")
If (f IsNot Nothing) Then
t = f.FieldType
Else
t = p.PropertyType
End If
If (t.IsValueType OrElse t Is GetType(String)) Then
If (f IsNot Nothing) Then
WriteValue(f.GetValue(o))
Else
WriteValue(p.GetValue(o, Nothing))
End If
ElseIf GetType(IEnumerable).IsAssignableFrom(t) Then
Write("...")
Else
Write("{ }")
End If
End If
Next
If propWritten Then WriteLine()
If (level < depth) Then
For Each m As MemberInfo In members
Dim f As FieldInfo = TryCast(m, FieldInfo)
Dim p As PropertyInfo = TryCast(m, PropertyInfo)
If (f IsNot Nothing OrElse p IsNot Nothing) Then
If (f IsNot Nothing) Then
t = f.FieldType
Else
t = p.PropertyType
End If
If (Not (t.IsValueType OrElse t Is GetType(String))) Then
Dim value As Object
If (f IsNot Nothing) Then
value = f.GetValue(o)
Else
value = p.GetValue(o, Nothing)
End If
If (value IsNot Nothing) Then
level += 1
WriteObject((m.Name & ": "), value)
level -= 1
End If
End If
End If
Next
End If
End If
End Sub