質問

Compact Framework.Net 2 SP 2 を使用して現在発生している問題について、誰かが私を助けてくれることを願っています。

現時点では、一連のテキスト ボックスを含む UI があり、各テキスト ボックスにはデータベース フィールドの内容が表示されます。これらは、フォームの右側のスクロール バーを使用して上下に表示されます。各テキストボックスには幅が設定されており、

各テキストボックスでスクロールバーの使用を避けるために、保持している行数、フォントサイズ、フォントに基づいて各テキストボックスの高さを調整したいと考えています。

現時点では、テストアプリケーションでこれを行うことができます。


スクリーンショット:

出力のスクリーンショットを参照 http://morrislgn.brinkster.net/SO/screenshot.jpg


私のコード:

'Text used in this example:
'TextBox1qwertyuiop lkjhgfdsazxcvbnm1234567890 TextBo

'x1qwer tyuioplkjhgfdsazxcvb nm1234567890

'qwe
'End of exmaple text.

Me.Textbox2.Text = Me.Textbox1.Text

Dim pobjGraphic As Graphics = Me.Textbox2.Parent.CreateGraphics()
Dim pobjSize As SizeF

'Padding values:
Dim piTop As Int32 = 4 'top of text and top of textbox
Dim piBottom As Int32 = 3 'bottom of text and top of textbox

Dim piLines As Int32 = 0

'Based on the font size chosen by the user, create a font to perform the calculation with.
Dim piFontSize As Single = 10

If Me.CheckBox1.Checked.Equals(True) Then
    piFontSize = 6
ElseIf Me.CheckBox2.Checked.Equals(True) Then
    piFontSize = 8
ElseIf Me.CheckBox3.Checked.Equals(True) Then
    piFontSize = 12
Else
    piFontSize = 10
End If

Dim pobjFont As New Font("Tahoma", piFontSize, FontStyle.Regular)

'Calculate the height of one line.
pobjSize = pobjGraphic.MeasureString("HELLO WORLD", pobjFont)
'Value of pobjSize returned: {Width = 71.0 Height = 13.0}


'Calculate the number of lines          
Dim b As Bitmap
b = New Bitmap(1, 1, Imaging.PixelFormat.Format32bppRgb)

'Calculate the number of lines required to display the text properly based on the lenght of the text the width of the control.
'Length of text to show divide by the width of the textbox
piLines = Graphics.FromImage(b).MeasureString(Me.Textbox2.Text, pobjFont).Width / Me.Textbox2.Width
'Value of piLines returned: 2

If piLines = 0 Then
    piLines = 1
End If

'Calculate the size of the text to be displayed using the margins, height of one line and number of lines.
Me.Textbox2.Height = (pobjSize.Height * piLines) + piTop + piBottom
' value produced: 33 = (13 * 2) + 4 + 3
'set font of text box
Me.Textbox2.Font = pobjFont

最後に、これは p/invoke を使用して COREDLL.dll を呼び出すことで実現できることはわかっていますが、これを行うとアプリケーションがクラッシュします。

こんにちは皆さん。

以下は要求された pinvoke コードです。

    <Runtime.InteropServices.DllImport("coredll.dll")> _
Private Function SendMessage( _
    ByVal hwnd As IntPtr, ByVal msg As Integer, _
    ByVal wParam As Integer, ByVal lParam As Integer) As Integer
End Function

<Runtime.InteropServices.DllImport("coredll.dll")> _
Private Function GetCapture() As IntPtr
End Function

<Runtime.InteropServices.DllImport("coredll.dll")> _
Private Function ReleaseCapture() As Boolean
End Function

Public Function GetNumberOfLines(ByVal ptxtCountBox As TextBox) As Integer
    Try
        Dim hnd As IntPtr = New IntPtr

        ptxtCountBox.Capture = True

        ' Capture the textbox handle.
        hnd = GetCapture()
        ptxtCountBox.Capture = False

        ' Get the count of the lines in the box.
        Dim plCount As Integer = SendMessage(ptxtCountBox.Handle, EM_GETLINECOUNT, 0, 0)

        ' Count the number of return lines as we minus this from the total lines to take.
        plCount = plCount - (CharCount(ptxtCountBox.Text, vbCrLf, False))

        plCount += RemoveNonASCIIReturns(ptxtCountBox)

        ReleaseCapture()

        hnd = Nothing

        ' Return the line count.
        Return plCount
    Catch ex As Exception
        GenerateError(msCLASS_NAME, "GetNumberOfLines", ex.Message.ToString)
    End Try
End Function

ありがとう、

モリス

役に立ちましたか?

解決

私は、同様の質問をし、完全に件名に私のニーズを満たして答えを得ました!私の質問からstevo3000の答えをチェックしてみて下さい: AutoSizeプロパティ

彼はただ、完全に1回のスワイプで私の問題を修正しこれら二つのブログの記事に言及しました! http://www.mobilepractices.com/2007/12/multi-ラインgraphicsmeasurestring.htmlする http://www.mobilepractices.com/2008/01/メイキング・マルチラインmeasurestring-work.htmlする

他のヒント

私はこれの一番下になったと考えて

    Public Function GetNumberOfLines(ByVal pstext As String, ByVal pobjfont As Font, ByVal pobjDimensions As Size) As Decimal
    Dim pslines As String() = Nothing
    'Used to measure the string to be placed into the textbox
    Dim pobjBitMap As Bitmap = Nothing
    Dim pobjSize As SizeF = Nothing

    Try
        Dim psline As String = String.Empty
        Dim pilinecount As Decimal = 0.0
        'Spilt the text based on the number of lines breaks.
        pslines = pstext.Split(vbCrLf)
        For Each psline In pslines
            'Create a graphics image which is used to work out the width of the text.
            pobjBitMap = New Bitmap(1, 1, Imaging.PixelFormat.Format32bppRgb)
            pobjSize = Graphics.FromImage(pobjBitMap).MeasureString(psline, pobjfont)

            'If the width of the text is less than 1.0 then add one to the count. This would incidcate a line break.
            If pobjSize.Width < 1.0 Then
                pilinecount = pilinecount + 1
            Else
                'Based on the dimensions of the text, work out the number of lines. 0.5 is added to round the value to next whole number.
                pilinecount = pilinecount + (Round((pobjSize.Width / pobjDimensions.Width) + 0.5))
            End If
        Next

        'If the line count it less than 1 return one line.
        If pilinecount < 1.0 Then
            Return 1.0
        Else
            Return pilinecount
        End If
    Catch ex As Exception
        Return 1.0
    Finally
        If pslines IsNot Nothing Then
            Array.Clear(pslines, 0, pslines.Length - 1)
            pslines = Nothing
        End If
        If pobjBitMap IsNot Nothing Then
            pobjBitMap.Dispose()
        End If
    End Try
End Function

確かに、そのハックのビットが、それは現時点では正常に動作するように見えます!これを改善する方法上の任意の観察やコメントは歓迎以上です。

また、P /呼び出すものについて、問題の根、またはむしろ解決策を発見しました。私のデバイス上の.Net FXをアップグレードし、それが問題を解決しているように見えます。

おかげ

モリス

そうですね、私はあなたに健全で賢い解決策を提案します。アルゴリズムは次のとおりです。

  1. 参照用に Label コントロールを使用します。
  2. 割当:• ラベルに対するテキストボックスのサイズ。• ラベルのテキストボックスのフォント。• ラベルの Autosize プロパティを設定する 真実。• Textbox のラベルの BorderStyle プロパティ。• テキストボックスの元のサイズとしてのラベルの MinimumSize プロパティ。• ラベルの MaximumSize プロパティは、幅は元と同じ、高さは元の高さの倍数になります。

  3. テキストボックスのテキストをラベルのテキストに割り当てます。

  4. 今:ラベルのprefferdheight-property> textbox == trueの高さの場合、テキストボックスの高さを上げて、上記の状態を誤りになるまで確認する時が来ました。
  5. ラベルはすぐに廃棄できます。

MSDN フォーラムにも同様のソリューションを投稿しましたので、こちらもチェックしてください。[http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/03fc8e75-fc13-417a-ad8c-d2b26a3a4dda][1]

よろしく。:)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top