0

以下の画像を参照してください。

Button.Text ハードコード データベースからの Button.Text

左の画像にはハードコードされた文字列があり、右の画像には MSSQL データベースから取り込まれたデータがあります。

以下のコードは、これらのボタンを構成します (データベース テーブル内のレコードの量に基づいて動的に作成されます)。

    'Button 
Private Sub LoadUseraccount()

    'Connect to Database (from module1)
    connectSQL()

    'Setup DataAdapter with query
    Dim da As SqlDataAdapter
    da = New SqlDataAdapter("SELECT * from useraccounts", SQLConn)

    'Store results in temporary datatable
    Dim dt As DataTable
    dt = New DataTable()

    da.Fill(dt)

    Dim i As Integer

    'Create a button for every record shown in useraccount table.
    For i = 0 To dt.Rows.Count - 1
        Dim dr As DataRow = dt.Rows(i)
        Dim newbutton As New Windows.Forms.Button
        Dim dataString As String = CStr(dr.Item("username"))


        newbutton.Name = "btnButton" & i
        newbutton.Text = dataString '<========= This Line is where the button text is set
        newbutton.Top = 200 + i * 105
        newbutton.Left = 40

        newbutton.TextAlign = ContentAlignment.MiddleCenter

        newbutton.Height = 107
        newbutton.Width = 180

        Dim myFont As System.Drawing.Font
        myFont = New System.Drawing.Font("Arial", 20.25)
        newbutton.Font = myFont
        newbutton.ForeColor = Color.White
        newbutton.BackColor = Color.MidnightBlue

        Me.Controls.Add(newbutton)
    Next

    SQLConn.Close()

End Sub

したがって、画像の唯一の違いは、テキストがボタンに表示されるように設定されていることですが、文字列が静的でない場合、配置はすべて狂っています.

右の画像を左の画像と同じようにフォーマットするには、何が間違っているのですか、または何が欠けていますか?

乾杯 :-)

4

1 に答える 1

0

あなたの例を試してみましたが、ボタンテキストに使用される変数に改行/改行を追加するまで、問題を再現できませんでした。

したがって、データには、出力をずらす印刷できない文字が含まれていると思います。

簡単な修正は

newbutton.Text = dataString.Trim()
于 2013-11-08T23:25:44.307 に答える