Getting the selected value from an unbound combobox
I have used the following code to list all of the installed fonts into a combobox.
Code:
For Each oFont As FontFamily In FontFamily.Families
cboFont.Items.Add(oFont.Name)
Next
I am using PDFSharp and I want to use the selection to add the combo choice into the code that produces the PDF file.
I don't know the syntax I should use to extract the choice, as selectedValue and ValueMember are not applicable in an unbound combo.
Can anyone point me in the right direction?
Here is the complete code for the form:
Code:
Imports System.Data.SqlClient
Imports System.Drawing
Imports PdfSharp
Imports PdfSharp.Drawing
Imports PdfSharp.Pdf
Imports PdfSharp.Drawing.Layout
Enum pageOrientation
Portrait
Landscape
End Enum
Enum pageSize
A4
End Enum
Public Class PrintFrm
Private connectionString As String = "Data Source=DESKTOP-S7FRNAL\SQLEXPRESS;Initial Catalog=Verses_Find;Integrated Security=True"
Public Property dt As Object
Private Sub PrintFrm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Verses_Find_Color_DataSet.TxtColor' table. You can move, or remove it, as needed.
Me.TxtColorTableAdapter.Fill(Me.Verses_Find_Color_DataSet.TxtColor)
'TODO: This line of code loads data into the 'Verses_FindDataSet5.CSize' table. You can move, or remove it, as needed.
Me.CSizeTableAdapter.Fill(Me.Verses_FindDataSet5.CSize)
Dim Print As New PrintFrm
Me.TopMost = True
Me.WindowState = FormWindowState.Normal
For Each oFont As FontFamily In FontFamily.Families
cboFont.Items.Add(oFont.Name)
Next
End Sub
Private Sub cboSize_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboSize.SelectedIndexChanged
Me.CSizeTableAdapter.Fill(Me.Verses_FindDataSet5.CSize)
Dim CSizeValue As Integer
Dim cboCSize As Object = Nothing
CSizeValue = cboCSize.selectedvalue
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Me.TxtColorTableAdapter.Fill(Me.Verses_Find_Color_DataSet.TxtColor)
End Sub
Private Sub btnPasteVerse_Click(sender As Object, e As EventArgs) Handles btnPasteVerse.Click
' Determine if there is any text in the Clipboard to paste into the text box.
If Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) = True Then
' Determine if any text is selected in the text box.
If txbVerse.SelectionLength > 0 Then
' Ask user if they want to paste over currently selected text.
If MessageBox.Show("Do you want to paste over current selection?",
"Cut Example", MessageBoxButtons.YesNo) = DialogResult.No Then
' Move selection to the point after the current selection and paste.
txbVerse.SelectionStart = txbVerse.SelectionStart +
txbVerse.SelectionLength
End If
End If
' Paste current text in Clipboard into text box.
txbVerse.Paste()
End If
End Sub
Private Sub txbVerse_TextChanged(sender As Object, e As EventArgs) Handles txbVerse.TextChanged
Dim FSize As Single
FSize = 18
txbVerse.Font = New Font(cboFont.Text, FSize, FontStyle.Regular)
MessageBox.Show(Replace(txbVerse.Text, Chr(13) & Chr(10), " VBCrLf "))
End Sub
Private Sub btnClose3_Click(sender As Object, e As EventArgs) Handles btnClose3.Click
Close()
End Sub
Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
Dim document As PdfDocument
Dim CID As Integer
Dim ort As String
Dim Narrative As String
Narrative = Nothing
ort = Nothing
Dim boxX As Integer
Dim boxY As Integer
Dim cellw As Integer
Dim cellh As Integer
Dim sql As String = Nothing
Dim sqlAdaptor As SqlDataAdapter
Dim dt As New DataTable
Using connection As New SqlConnection(connectionString)
connection.Open()
sql = $"SELECT CID,boxX, boxY,ort, Narrative FRom CSize Where CID = CSizeValue"
sqlAdaptor = New SqlDataAdapter(sql, connection)
dt = New DataTable()
sqlAdaptor.Fill(dt)
End Using
' Create a new PDF document
document = New PdfDocument()
document.Info.Title = "Created with PDFsharp"
' Create an empty page
Dim page As PdfPage = document.AddPage
If ort = "L" Then
page.Orientation = CType(pageOrientation.Landscape, PdfSharp.PageOrientation)
page.Width = XUnit.FromMillimeter(297)
page.Height = XUnit.FromMillimeter(210)
Else
page.Orientation = CType(pageOrientation.Portrait, PdfSharp.PageOrientation)
page.Width = XUnit.FromMillimeter(210)
page.Height = XUnit.FromMillimeter(297)
End If
' Draw the text
Dim Ftext As String = txbVerse.Text
Dim CFont As String = cboFont. <<<<<<<<<<<<
Dim gfx As XGraphics
gfx = XGraphics.FromPdfPage(page)
Dim font As XFont = New XFont(CFont, 20, XFontStyle.Regular)
Dim tf As XTextFormatter
tf = New XTextFormatter(gfx)
Dim rect As XRect
rect = New XRect(boxX, boxY, cellw, cellh)
gfx.DrawRectangle(XBrushes.SeaShell, rect)
tf.Alignment = XParagraphAlignment.Center
tf.DrawString(Ftext, font, XBrushes.Black, rect, XStringFormats.TopLeft)
' Save the document
Dim filename As String = "verse.pdf"
document.Save(filename)
' ...and start a viewer.
Process.Start(filename)
End Sub
End Class
The last sub is the code I am unsure how to write.
Re: Getting the selected value from an unbound combobox
Dim CFont As String = cboFont.Text
Re: Getting the selected value from an unbound combobox
You should try consulting the documentation first in future. It's accessible via the Help menu in VS for a reason. This is from the topic for the WinForms ComboBox class:
Quote:
You can use these properties to manage the currently selected item in the list, the Text property to specify the string displayed in the editing field, the SelectedIndex property to get or set the current item, and the SelectedItem property to get or set a reference to the object.
You could even go straight to that page by clicking on the ComboBox in code or (I think) selecting it in the designer and then hitting the F1 key. Context-sensitive help has been available in Windows for decades.