Morjens!
Listbox-luokassa ei ole tekstin tasaus mahdollisuutta (textalign).
Löysin kuitenkin tällaisen: http://www.vbforums.com/showthread.php?t=399546 , mutta en saa tuota toimimaan vb.net'llä, enkä sitäpaitsi oikeen edes tajua tuota...
Picturebox ja FontName..hmm?
ByVal Alignment As AlignmentConstants <- mikä ois tuolle korvaava
Ja mitäs tämä $ merkki tuolla tarkoittaa.... uutta minulle
Osaisko joku säätää tuota?
Tuo linkittämäsi sivun koodi on tehty vanhemmille vb:n versiolle, joten se ei suoraan toimi .netillä.
Tämä on periaatteessa sama koodi vb.netille, vain muutamia pikku juttuja on muutettu. Toimiakseen tarvitsee labelin nimeltä LblAlign. Tämän labelin voi piilottaa tai sijoittaa ruudun ulkopuolelle, jotta se ei häiritse turhaan. PictureBoxia ei enään tarvitse.
' Käyttö AlignText("Hello World", ListBox1, HorizontalAlignment.Left) AlignText("Hello World", ListBox1, HorizontalAlignment.Center) AlignText("Hello World", ListBox1, HorizontalAlignment.Right)
' moduuliin Public Sub AlignText(ByVal txt As String, ByVal lstBox As System.Windows.Forms.ListBox, ByVal Alignment As System.Windows.Forms.HorizontalAlignment) Dim i As Long With LblAlign ' Varmistetaan että AutoSize on päällä ja että labelilla ja listboxilla on sama fontti .AutoSize = True .Font = lstBox.Font .Text = txt Do While .Width < lstBox.Width ' lasketaan montako välilyöntiä voidaan lisätä .Text = .Text + Space(1) i += 1 Loop End With Select Case Alignment Case HorizontalAlignment.Left lstBox.Items.Add(txt) Case HorizontalAlignment.Right lstBox.Items.Add(Space(i) + txt) Case HorizontalAlignment.Center lstBox.Items.Add(Space(i \ 2) + txt) End Select End Sub
Tämä koodi ei kuitenkaan toimi kun funktiolle syötetään ComboBoxi. System.Windows.Forms.ListControl-muuttujan avulla tämä voisi jotenkin onnistua, mutta en sitten tiedä, miten uuden rivin lisäys onnituisi. Jos tarvitset funktion comboboksia varten, niin muuta System.Windows.Forms.ListBox >> System.Windows.Forms.ComboBox.
Lisäksi varmista aina että laatikko on riittävän pitkä sivu suunnassa, muuten tulee ongelmia.
Tämä voisi mennä melkein jo koodivinkistä!
Kiitos petrinm.
Moikka novice!
tässä toinen tapa...
'Module1 Imports System.Windows.Forms Public Module Module1 Public Const maxSpaces As Integer = 24 Public Function TextAlign(ByVal text As String, _ ByVal Alignment As HorizontalAlignment) As String If text.Length > 0 AndAlso text.Length < maxSpaces Then Select Case Alignment Case HorizontalAlignment.Center text = Space(CInt((maxSpaces - text.Length) / 2) - _ CInt((maxSpaces - text.Length) / 2) Mod 2) + text Case HorizontalAlignment.Right text = Space(maxSpaces - (text.Length)- 1) + text End Select End If Return text End Function End Module
'MainForm Imports System Imports System.Drawing Imports System.Windows.Forms Public Partial Class MainForm: Inherits Form Public box As Object Public Sub New() Me.InitializeComponent() End Sub Sub MainFormLoad(sender As Object, e As EventArgs) Me.ListBox1.Font = New System.Drawing.Font( _ "Courier New", 8.25!, System.Drawing.FontStyle. _ Regular, System.Drawing.GraphicsUnit.Point, CType(0,Byte)) Me.listBox1.Size = New System.Drawing.Size(188, 88) Me.listBox1.ScrollAlwaysVisible = True For i As Integer = 1 To 10 Me.listBox1.Items.Add("JUTSKA" & Cstr(i)) Next box = Me.listBox1 Me.radioButton1.Select End Sub Sub RadioButton1CheckedChanged(sender As Object, e As EventArgs) If RadioButton1.Checked Then For i As Integer = 0 To box.Items.Count -1 box.Items(i) = box.Items(i).Trim Next End If End Sub Sub RadioButton2CheckedChanged(sender As Object, e As EventArgs) If RadioButton2.Checked Then For i As Integer = 0 To box.Items.Count -1 box.Items(i) = TextAlign(box.Items(i).Trim, _ HorizontalAlignment.Center) Next End If End Sub Sub RadioButton3CheckedChanged(sender As Object, e As EventArgs) If RadioButton3.Checked Then For i As Integer = 0 To box.Items.Count -1 box.Items(i) = TextAlign(box.Items(i).Trim, _ RightHorizontalAlignment.Right) Next End If End Sub Sub ListBox1Enter(sender As Object, e As EventArgs) box = sender End Sub End Class
Heippa taas!
sama ListBox/ComboBox kombinaatiolla...
Public Module Module1 Public Structure BoxStatus Public box As Object Public name As String Public align As HorizontalAlignment End Structure Public Boxes() As BoxStatus '(säätö max merkit -- boxien leveys) Public Const maxSpaces As Integer = 24 Public Function TextAlign(ByVal text As String, _ ByVal Alignment As HorizontalAlignment) As String If text.Length > 0 AndAlso text.Length < maxSpaces Then Select Case Alignment Case HorizontalAlignment.Center text = Space(CInt((maxSpaces - text.Length) / 2) - _ CInt((maxSpaces - text.Length) / 2) Mod 2) + text Case HorizontalAlignment.Right text = Space(maxSpaces - (text.Length)- 1) + text End Select End If Return text End Function End Module
Imports System Imports System.Drawing Imports System.Windows.Forms Public Partial Class MainForm: Inherits Form '*** radionapit on groupbox'ssa Public box As Object Public Sub New() Me.InitializeComponent() End Sub Sub MainFormLoad(sender As Object, e As EventArgs) Me.listBox1.Font = New System.Drawing.Font( _ "Courier New", 8.25!, System.Drawing.FontStyle. _ Regular, System.Drawing.GraphicsUnit.Point, CType(0,Byte)) Me.comboBox1.Font = New System.Drawing.Font( _ "Courier New", 8.25!, System.Drawing.FontStyle. _ Regular, System.Drawing.GraphicsUnit.Point, CType(0,Byte)) Me.listBox1.Size = New System.Drawing.Size(188, 88) Me.listBox1.ScrollAlwaysVisible = True Me.comboBox1.Width = Me.ListBox1.Width For i As Integer = 1 To 10 Me.listBox1.Items.Add("JUTSKA" & Cstr(i)) Me.comboBox1.Items.Add("JUTSKA" & Cstr(i)) Next Me.comboBox1.SelectedIndex = 0 Dim ctl As Control, j As Integer = 0 For Each ctl In Me.Controls If TypeOf(ctl) Is comboBox Or _ TypeOf(ctl) Is listBox Then ReDim Preserve Boxes(j) Boxes(j).box = ctl Boxes(j).name = ctl.Name Boxes(j).Align = _ HorizontalAlignment.Left End If j += 1 Next: j = Nothing Me.listBox1.Select Me.radioButton1.Select End Sub Sub RadioButton1CheckedChanged(sender As Object, e As EventArgs) SetBoxState(HorizontalAlignment.Left) End Sub Sub RadioButton2CheckedChanged(sender As Object, e As EventArgs) SetBoxState(HorizontalAlignment.Center) End Sub Sub RadioButton3CheckedChanged(sender As Object, e As EventArgs) SetBoxState(HorizontalAlignment.Right) End Sub Sub ListBox1Enter(sender As Object, e As EventArgs) box = sender: CheckBoxState() End Sub Sub ComboBox1Enter(sender As Object, e As EventArgs) box = sender: CheckBoxState() End Sub Sub SetBoxState(ByVal Alignment As HorizontalAlignment) If TypeOf(box) Is ComboBox box.Text = _ TextAlign(box.Text.Trim, Alignment) End If For i As Integer = 0 To box.Items.Count -1 box.Items(i) = _ TextAlign(box.Items(i).Trim, Alignment) Next For i As Integer = 0 To Boxes.GetUpperBound(0) If Boxes(i).name = box.Name Then Boxes(i).align = Alignment Exit For End If Next End Sub Sub CheckBoxState() For i As Integer = 0 To Boxes.GetUpperBound(0) If Boxes(i).name = box.Name Then Select Case Boxes(i).align Case HorizontalAlignment.Left Me.radioButton1.Checked = True Case HorizontalAlignment.Center Me.radioButton2.Checked = True Case HorizontalAlignment.Right Me.radioButton3.Checked = True End Select Exit For End If Next End Sub End Class
Aihe on jo aika vanha, joten et voi enää vastata siihen.