从我的表单中获取我的字符串值到我的班级(不是另一种形式)

时间:2010-04-13 10:07:24

标签: vb.net string

我对从一个表单转移到我的班级的一些数据有疑问。这不是我想要的方式,所以我想也许有人可以帮助我。

这是我班上的代码

Public Class DrawableTextBox
  Inherits Drawable
  Dim i_testString As Integer

Private s_InsertLabel As String

Private drawFont As Font


Public Sub New(ByVal fore_color As Color, ByVal fill_color As Color, Optional ByVal line_width As Integer = 0, Optional ByVal new_x1 As Integer = 0, Optional ByVal new_y1 As Integer = 0, Optional ByVal new_x2 As Integer = 1, Optional ByVal new_y2 As Integer = 1)
    MyBase.New(fore_color, fill_color, line_width)

    X1 = new_x1
    Y1 = new_y1
    X2 = new_x2
    Y2 = new_y2


    Trace.WriteLine(s_InsertLabel)


End Sub


Friend WriteOnly Property _textBox() As String

    Set(ByVal Value As String)

        s_InsertLabel = Value
        Trace.WriteLine(s_InsertLabel)

    End Set

End Property



' Draw the object on this Graphics surface.
Public Overrides Sub Draw(ByVal gr As System.Drawing.Graphics)


    ' Make a Rectangle representing this rectangle.
    Dim rect As Rectangle = GetBounds()

    ' Fill the rectangle as usual.
    Dim fill_brush As New SolidBrush(FillColor)
    gr.FillRectangle(fill_brush, rect)
    fill_brush.Dispose()

    ' See if we're selected.
    If IsSelected Then


        ' Draw the rectangle highlighted.
        Dim highlight_pen As New Pen(Color.Yellow, LineWidth)
        gr.DrawRectangle(highlight_pen, rect)
        highlight_pen.Dispose()


        ' Draw grab handles.
        Trace.WriteLine("drawing the lines for my textbox")
        DrawGrabHandle(gr, X1, Y1)
        DrawGrabHandle(gr, X1, Y2)
        DrawGrabHandle(gr, X2, Y2)
        DrawGrabHandle(gr, X2, Y1)

    Else


        'TextBox()
        Dim fg_pen As New Pen(Color.Red, LineWidth)
        'Dim fontSize As Single = 0.1 + ((Y2 - Y1) / 2)
        Dim fontSize As Single = 20

        Try



            Dim drawFont As New Font("Arial", fontSize, FontStyle.Bold)
            Trace.WriteLine(s_InsertLabel)
            gr.DrawString(s_InsertLabel, drawFont, Brushes.Brown, X1, Y1)
        Catch ex As ArgumentException



        End Try
        gr.DrawRectangle(Pens.Azure, rect)
        ' gr.DrawRectangle(fg_pen, rect)
        fg_pen.Dispose()


    End If

End Sub


Public Function GetValueString(ByVal ValueType As String)
    Return ValueType
End Function

' Return the object's bounding rectangle.
Public Overrides Function GetBounds() As System.Drawing.Rectangle
    Return New Rectangle( _
        Min(X1, X2), _
        Min(Y1, Y2), _
        Abs(100), _
        Abs(30))
    Trace.WriteLine("don't forget to make variables in GetBounds DrawableTextbox")
End Function


' Return True if this point is on the object.
Public Overrides Function IsAt(ByVal x As Integer, ByVal y As Integer) As Boolean
    Return (x >= Min(X1, X2)) AndAlso _
           (x <= Max(X1, X2)) AndAlso _
           (y >= Min(Y1, Y2)) AndAlso _
           (y <= Max(Y1, Y2))
End Function

' Move the second point.
Public Overrides Sub NewPoint(ByVal x As Integer, ByVal y As Integer)
    X2 = x
    Y2 = y
End Sub

' Return True if the object is empty (e.g. a zero-length line).
Public Overrides Function IsEmpty() As Boolean
    Return (X1 = X2) AndAlso (Y1 = Y2)
End Function


End Class

我有一个带有文本框(form1)的表单,其中插入文本并通过buttonclick(通过属性)传递。

正如你所看到的,我已经放置了几个跟踪并且在类的属性中我的跟踪工作正常,但是如果我查看我的Draw函数它已经消失了。

我得到一个空白的痕迹。 有谁知道这里发生了什么。 提前谢谢。

(原谅我,我是新人)

2 个答案:

答案 0 :(得分:0)

如果我理解正确你说你将s_InsertLabel设置为一个值,你可以看到它在跟踪中被设置但是当调用Draw方法时s_InsertLabel是空的吗?

由于您只跟踪s_InsertLabel的值,因此很难判断跟踪输出中发生了什么。我建议包括位置或类似位置,例如:
Trace.WriteLine(“构造函数:”&amp; s_InsertLabel)

通过这种方式,您可以判断属性是否设置了多次,或者是否多次调用构造函数(可能在一个控件中设置了s_InsertLabel,但是绘制了另一个控件的相同类型)。

答案 1 :(得分:0)

我想出了一种获得我想要的东西的方法。

我在类的新方法(构造函数)中创建了一个新Form,并使用Dialogresult从我的文本框中获取正确的字符串。 像这样

  Dim frm As New Form1
        frm.ShowDialog()
        If frm.DialogResult = DialogResult.OK Then
            s_InsertLabel = frm.TextBox2.Text
        End If

非常感谢您的快速响应。

现在我很好,但我相信将来我会提出更多问题