Kirjautuminen

Haku

Tehtävät

Keskustelu: Ohjelmointikysymykset: VB.NET: [VB.Net] Combobox

Sivun loppuun

Seppo_Taalasmaa [09.04.2010 00:50:41]

#

Morjensta taas!

Olen kohta kolunnu puoli internettiä läpi etsiäkseni mitenkä ohjelmoida comboboxi josta voi valita kaikki mahdolliset fontit ja samalainen comboboxi josta voisi valita koon... Jonkun ohjeen löysin msdn:stä mutta se oli epäselvä :/

groovyb [09.04.2010 12:19:48]

#

tässä esimerkissä:

Lisää Form1:seen command button (Button1) sekä label (label1), sekä kaksi comboboxia (combobox1 ja combobox 2)

Imports System
Imports System.Windows.Forms
Imports System.Drawing.Text
Imports System.Drawing

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        For i = 6 To 24
            ComboBox2.Items.Add(i.ToString()) 'Koot 6 - 24
        Next

        Dim installed_fonts As New InstalledFontCollection
        ' Taulukko System Font Families:stä
        Dim font_families() As FontFamily = installed_fonts.Families()

        'Lisätään comboboxiin
        For Each font_family As FontFamily In font_families
            ComboBox1.Items.Add(font_family.Name)
        Next font_family


    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Font = New Font(ComboBox1.SelectedItem.ToString(), Convert.ToSingle(ComboBox2.SelectedItem.ToString()), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point) 'asetetaan uusi fontti ja koko
    End Sub
End Class

Seppo_Taalasmaa [09.04.2010 17:34:01]

#

[noob]http://msdn.microsoft.com/en-us/library/aa733519(VS.60).aspx tämä se ohje oli aika epäselvä... haluaisin richtextboxiin tämän tekstin ja fontin muutoksen.
Kiitos jo etukäteen! :)[/noob]

neau33 [09.04.2010 21:36:19]

#

Heippa Seppo_Taalasmaa!

tässä osviittaa RtfBoxin fontti-ominaisuuksien säätelyyn...

 'Väännetty SharpDevelop 3.2:lla
Imports System
Imports System.Drawing
Imports System.Drawing.Text
Imports System.Windows.Forms

Public Partial Class MainForm

   Private fntStyle As FontStyle

   Public Sub New()
      Me.InitializeComponent()
   End Sub


   Sub MainFormLoad(sender As Object, e As EventArgs)

       For Each ctl As Control In Me.Controls
         If ctl.name.Substring(0,5) = "fsBtn" Then
             ctl.Tag = False
             ctl.BackColor = Color.WhiteSmoke
         End If
       Next

       FillCboFontNames

   End Sub

   Sub FsBtn0Click(sender As Object, e As EventArgs)
      SetFontStyleOptions(sender)
   End Sub

   Sub FsBtn1Click(sender As Object, e As EventArgs)
      SetFontStyleOptions(sender)
   End Sub

   Sub FsBtn2Click(sender As Object, e As EventArgs)
      SetFontStyleOptions(sender)
   End Sub

   Sub FsBtn3Click(sender As Object, e As EventArgs)
      SetFontStyleOptions(sender)
   End Sub

   Sub FcBtnClick(sender As Object, e As EventArgs)
      SetFontColorOptions(sender)
   End Sub

   Sub BcBtnClick(sender As Object, e As EventArgs)
      SetFontColorOptions(sender)
   End Sub

   Sub ComboBox1SelectedIndexChanged(sender As Object, e As EventArgs)
      SetFontStyleOptions(sender)
   End Sub

   Sub NumericUpDown1ValueChanged(sender As Object, e As EventArgs)
      SetFontStyleOptions(sender)
   End Sub

   Public Sub FillCboFontNames()

      Dim index As Integer = 0
      Dim ifonts As New InstalledFontCollection

      For Each family As FontFamily In ifonts.Families
         ComboBox1.Items.Add(family.Name)
      Next

      For i As Integer = 0 To comboBox1.Items.Count -1
        If  comboBox1.Items(i) = "Times New Roman" Then
          index = i: Exit for
        End If
      Next

      comboBox1.SelectedIndex = index
      numericUpDown1.Value = 10
      ifonts = Nothing

   End Sub

   Public Sub ChangeRtfStyle()

      Try
         Richtextbox1.SelectionFont = _
         New Font(New FontFamily( _
         ComboBox1.SelectedItem.ToString ) _
         , numericUpDown1.value, fntStyle)
         richTextbox1.Focus
      Catch ex As Exception
      End Try

   End Sub

   Public Sub SetFontStyleOptions(ctl As Control)

      ctl.Tag = Not ctl.Tag

      If ctl.Tag Then
         ctl.BackColor = Color.LightGray
      Else
         ctl.BackColor = Color.WhiteSmoke
      End If

      Dim binStr As String = ""

      For i = 3 To 0 Step -1
         If Me.Controls("fsBtn" _
         & Cstr(i)).Tag = True Then
            binStr += "1"
         Else
            binStr += "0"
         End If
      Next

      Dim value As long = 0
      For i As Integer = binStr.Length To 1 Step -1
         If Mid(binStr, i, 1) = "1" Then
            value +=  2 ^ (binStr.Length - i)
         End If
      Next i

      fntStyle = value
      ChangeRtfStyle

   End Sub

   Public Sub SetFontColorOptions(ctl As Control)

      colorDialog1.ShowDialog

      Select Case ctl.Name
         Case "fcBtn"
            richTextbox1.SelectionColor = _
            ColorDialog1.Color
            richTextbox1.Focus
         Case "bcBtn"
            richTextbox1.SelectionBackColor = _
            ColorDialog1.Color
            richTextbox1.Focus
      End Select
   End Sub

End Class

Seppo_Taalasmaa [11.04.2010 12:08:03]

#

En saanut toimimaan tuota... tai sitten en vaan osaa :) Mulla on siis fonttiToolStripComboBox ja fontinkokoToolStripComboBox Comboboxit ja näihin pitäis saada ne säätelyt toimimaan. Voihan se olla siitä kiinni että olen vielä aloittelija ja en ymmärrä vielä kaikkia mahdollista mutta koittakaa kestää noobiuteni :D

Seppo_Taalasmaa [11.04.2010 13:51:28]

#

ja tämä kyseinen ominaisuus tulee tekstinkäsittely ohjelmaan

Seppo_Taalasmaa [12.04.2010 18:13:35]

#

http://msdn.microsoft.com/en-us/library/aa733519(VS.60).aspx voisiko joku tämän kääntää vb6:sta vb.nettiin?

neau33 [13.04.2010 14:37:25]

#

Heippa taas Seppo_Taalasmaa!

mielestäni linkin VB6 esimerkki on syvältä, mutta tässä käännetty VB.NET versio

Imports System
Imports System.Drawing
Imports System.Drawing.Text

Public Class Form1

   Private Sub Form1_Load(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles MyBase.Load

      With toolstripCombobox1
         .Items.Add ("Black")
         .Items.Add ("Blue")
         .Items.Add ("Red")
         .Items.Add ("Green")
         .SelectedIndex = 0
      End With

      Dim ifonts As New InstalledFontCollection

      With toolstripCombobox2
         For Each family As FontFamily In ifonts.Families
            .Items.Add(family.Name)
         Next
         .DropDownStyle = 2
      End With

      ifonts = Nothing

      With toolstripCombobox3
         For i As Integer = 8 To 72 Step 2
            .Items.Add(i)
         Next
         .SelectedIndex = 1
      End With

      For i As Integer = 0 To _
      toolstripCombobox2.Items.Count -1

         If toolstripCombobox2.Items(i) = _
         "Times New Roman" Then
            toolstripCombobox2.SelectedIndex = i
            Exit For
        End If
      Next

   End Sub

   Private Sub RichTextBox1_SelectionChanged(ByVal sender As Object, _
   ByVal e As System.EventArgs) Handles RichTextBox1.SelectionChanged

      Try
         ToolStripComboBox3.Text = _
         CInt(RichTextBox1.SelectionFont.Size).ToString
         ToolStripComboBox2.Text = RichTextBox1.SelectionFont.Name

         With RichTextBox1
            Select Case .SelectionColor
               Case Color.Black
                  ToolStripComboBox1.SelectedIndex = 0
               Case Color.Blue
                  ToolStripComboBox1.SelectedIndex = 1
               Case Color.Red
                  ToolStripComboBox1.SelectedIndex = 2
               Case Color.Green
                  ToolStripComboBox1.SelectedIndex = 3
            End Select
         End With
      Catch ex As Exception
      End Try

   End Sub

   Private Sub ToolStripComboBox1_SelectedIndexChanged(sender as Object, _
   e as EventArgs) Handles ToolStripComboBox1.SelectedIndexChanged
      ChangeFontColor(sender.SeletedIndex)
   End Sub

   Private Sub ToolStripComboBox2_SelectedIndexChanged(sender as Object, _
   e as EventArgs) Handles ToolStripComboBox2.SelectedIndexChanged
      ChangeFontProperties
   End Sub

   Private Sub ToolStripComboBox3_SelectedIndexChanged(sender as Object, _
   e as EventArgs) Handles ToolStripComboBox3.SelectedIndexChanged
      ChangeFontProperties
   End Sub

   Sub ChangeFontColor(ByVal Index As Integer)

      With Richtextbox1
         Select Case Index
            Case 0
               .SelectionColor = Color.Black
            Case 1
               .SelectionColor = Color.Blue
            Case 2
               .SelectionColor = Color.Red
            Case 3
               .SelectionColor = Color.Green
         End Select
         .Focus
      End With

   End Sub

   Sub ChangeFontProperties()

      With Richtextbox1
         Try
            .SelectionFont = _
            New Font(New FontFamily( _
            ToolStripComboBox2.Text), _
            Cint(ToolStripComboBox3.Text), _
            Fontstyle.Regular)
            .Focus
         Catch ex As Exception
         End Try
      End With

   End Sub

End Class

Seppo_Taalasmaa [15.04.2010 21:53:51]

#

Kiitoksia neau33! :)


Sivun alkuun

Vastaus

Aihe on jo aika vanha, joten et voi enää vastata siihen.

Tietoa sivustosta