Solved

get combo box value into rich text box

Posted on 2014-12-09
10
351 Views
Last Modified: 2014-12-09
I am trying to get the term type value from my combo box into term starts added rich text box.  In addition, I would like to clear the text boxes if the user wants to start over Calvin-code-error.PNG
0
Comment
Question by:cmed
  • 6
  • 2
  • 2
10 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 40489087
Not much to go on here...here's a sprinkling of magic dust:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If ComboBox1.SelectedIndex <> -1 Then
            RichTextBox1.SelectionStart = RichTextBox1.TextLength
            RichTextBox1.SelectedText = "Blah blah: " & ComboBox1.SelectedItem & ", blah" & vbCrLf
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        RichTextBox1.Clear()
    End Sub

Open in new window

0
 

Author Comment

by:cmed
ID: 40489127
@ Mike Tomlinson

This is what I have so far.  I am new to vb.net, so I am a work in progress. output of code
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DTPTerm1.Format = DateTimePickerFormat.Custom
        DTPTerm1.CustomFormat = " "
    End Sub

    Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged

    End Sub

    Private Sub COBTermType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles COBTermType.SelectedIndexChanged

    End Sub

    Private Sub btnClearTermStarts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearTermStarts.Click
        RichTextBox1.Clear()
    End Sub

    Private Sub btnAddTermStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddTermStart.Click
        If DTPTerm1.CustomFormat = CStr(DTPTerm1.CustomFormat) Then
            Dim cf As String
            cf = DTPTerm1.CustomFormat
            RichTextBox1.SelectedText = cf
        End If
        If COBTermType.SelectedIndex > -1 Then
            Dim sindex As Integer
            sindex = COBTermType.SelectedIndex
            Dim sitem As Object
            sitem = COBTermType.SelectedItem
            RichTextBox1.SelectedText = sitem
        End If
    End Sub

    Private Sub DTPTerm1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DTPTerm1.ValueChanged
        DTPTerm1.CustomFormat = "MM/dd/yyyy"
    End Sub

Open in new window

0
 

Author Comment

by:cmed
ID: 40489140
In additon, if I add selected item to the richtextbox, how can i get the selected items under each selected instead of next to each other?
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DTPTerm1.Format = DateTimePickerFormat.Custom
        DTPTerm1.CustomFormat = " "
    End Sub

    Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged

    End Sub

    Private Sub COBTermType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles COBTermType.SelectedIndexChanged

    End Sub

    Private Sub btnClearTermStarts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearTermStarts.Click
        RichTextBox1.Clear()
    End Sub

    Private Sub btnAddTermStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddTermStart.Click
        If DTPTerm1.CustomFormat = CStr(DTPTerm1.CustomFormat) Then
            Dim cf As String
            cf = DTPTerm1.CustomFormat
            RichTextBox1.SelectedText = cf
        End If
        If COBTermType.SelectedIndex > -1 Then
            Dim sindex As Integer
            sindex = COBTermType.SelectedIndex
            Dim sitem As Object
            sitem = COBTermType.SelectedItem
            RichTextBox1.SelectedText = sitem
        End If
    End Sub

    Private Sub DTPTerm1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DTPTerm1.ValueChanged
        DTPTerm1.CustomFormat = "MM/dd/yyyy"
    End Sub

Open in new window

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 40489188
I'd do something like:
Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DTPTerm1.Format = DateTimePickerFormat.Custom
        DTPTerm1.CustomFormat = "MM/dd/yyyy"
        DTPTerm1.Value = DateTimePicker.MinimumDateTime
        btnAddTermStart.Enabled = False
    End Sub

    Private Sub Changed(sender As Object, e As EventArgs) Handles DTPTerm1.ValueChanged, COBTermType.SelectedIndexChanged
        btnAddTermStart.Enabled = ((DTPTerm1.Value <> DTPTerm1.MinDate) AndAlso (COBTermType.SelectedIndex <> -1))
    End Sub

    Private Sub btnAddTermStart_Click(sender As Object, e As EventArgs) Handles btnAddTermStart.Click
        RichTextBox1.SelectionStart = RichTextBox1.TextLength
        RichTextBox1.SelectedText = DTPTerm1.Text & " " & COBTermType.SelectedItem & vbCrLf
    End Sub

    Private Sub btnClearTermStarts_Click(sender As Object, e As EventArgs) Handles btnClearTermStarts.Click
        RichTextBox1.Clear()
    End Sub

End Class

Open in new window

0
 
LVL 33

Expert Comment

by:it_saige
ID: 40489213
Something like this:

Form1.vb -
Public Class Form1
	Private source As New BindingSource()
	Private terms As List(Of Term) = New List(Of Term) From _
	 { _
	  New Term() With {.ID = 1, .Name = "Term One", .Value = "Term1"}, _
	  New Term() With {.ID = 2, .Name = "Term Two", .Value = "Term2"}, _
	  New Term() With {.ID = 3, .Name = "Term Three", .Value = "Term3"}, _
	  New Term() With {.ID = 4, .Name = "Term Four", .Value = "Term4"}, _
	  New Term() With {.ID = 5, .Name = "Term Five", .Value = "Term5"} _
	 }

	Private Sub OnClick(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click, Button3.Click, Button2.Click
		If TypeOf sender Is Button Then
			Dim btn As Button = DirectCast(sender, Button)
			If btn.Equals(Button1) Then
				RichTextBox1.AppendText(String.Format("{0} - [{1}]{2}", ComboBox1.SelectedValue, DateTimePicker1.Text, Environment.NewLine))
			ElseIf btn.Equals(Button2) Then
				' Report methods here
			ElseIf btn.Equals(Button3) Then
				RichTextBox1.Clear()
				ComboBox1.SelectedIndex = 0
				DateTimePicker1.ResetText()
			End If
		End If
	End Sub

	Private Sub OnLoad(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
		source.DataSource = terms
		ComboBox1.DataSource = source
		ComboBox1.DisplayMember = "Name"
		ComboBox1.ValueMember = "Value"
	End Sub
End Class

Public Class Term
	Public Property ID() As Integer
	Public Property Name() As String
	Public Property Value() As String
End Class

Open in new window


Form1.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
		Me.Button1 = New System.Windows.Forms.Button()
		Me.Button2 = New System.Windows.Forms.Button()
		Me.Button3 = New System.Windows.Forms.Button()
		Me.DateTimePicker1 = New System.Windows.Forms.DateTimePicker()
		Me.ComboBox1 = New System.Windows.Forms.ComboBox()
		Me.Label1 = New System.Windows.Forms.Label()
		Me.Label2 = New System.Windows.Forms.Label()
		Me.Label3 = New System.Windows.Forms.Label()
		Me.RichTextBox1 = New System.Windows.Forms.RichTextBox()
		Me.SuspendLayout()
		'
		'Button1
		'
		Me.Button1.Location = New System.Drawing.Point(372, 12)
		Me.Button1.Name = "Button1"
		Me.Button1.Size = New System.Drawing.Size(100, 23)
		Me.Button1.TabIndex = 0
		Me.Button1.Text = "Add Term Start"
		Me.Button1.UseVisualStyleBackColor = True
		'
		'Button2
		'
		Me.Button2.Location = New System.Drawing.Point(372, 86)
		Me.Button2.Name = "Button2"
		Me.Button2.Size = New System.Drawing.Size(100, 23)
		Me.Button2.TabIndex = 1
		Me.Button2.Text = "Create Report"
		Me.Button2.UseVisualStyleBackColor = True
		'
		'Button3
		'
		Me.Button3.Location = New System.Drawing.Point(372, 158)
		Me.Button3.Name = "Button3"
		Me.Button3.Size = New System.Drawing.Size(100, 23)
		Me.Button3.TabIndex = 2
		Me.Button3.Text = "Clear Term Starts"
		Me.Button3.UseVisualStyleBackColor = True
		'
		'DateTimePicker1
		'
		Me.DateTimePicker1.CustomFormat = "MM/dd/yyyy"
		Me.DateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Custom
		Me.DateTimePicker1.Location = New System.Drawing.Point(51, 12)
		Me.DateTimePicker1.Name = "DateTimePicker1"
		Me.DateTimePicker1.Size = New System.Drawing.Size(121, 20)
		Me.DateTimePicker1.TabIndex = 3
		'
		'ComboBox1
		'
		Me.ComboBox1.FormattingEnabled = True
		Me.ComboBox1.Location = New System.Drawing.Point(245, 12)
		Me.ComboBox1.Name = "ComboBox1"
		Me.ComboBox1.Size = New System.Drawing.Size(121, 21)
		Me.ComboBox1.TabIndex = 4
		'
		'Label1
		'
		Me.Label1.AutoSize = True
		Me.Label1.Location = New System.Drawing.Point(12, 16)
		Me.Label1.Name = "Label1"
		Me.Label1.Size = New System.Drawing.Size(33, 13)
		Me.Label1.TabIndex = 5
		Me.Label1.Text = "Date:"
		'
		'Label2
		'
		Me.Label2.AutoSize = True
		Me.Label2.Location = New System.Drawing.Point(178, 17)
		Me.Label2.Name = "Label2"
		Me.Label2.Size = New System.Drawing.Size(61, 13)
		Me.Label2.TabIndex = 6
		Me.Label2.Text = "Term Type:"
		'
		'Label3
		'
		Me.Label3.AutoSize = True
		Me.Label3.Location = New System.Drawing.Point(12, 58)
		Me.Label3.Name = "Label3"
		Me.Label3.Size = New System.Drawing.Size(98, 13)
		Me.Label3.TabIndex = 7
		Me.Label3.Text = "Term Starts Added:"
		'
		'RichTextBox1
		'
		Me.RichTextBox1.Location = New System.Drawing.Point(15, 75)
		Me.RichTextBox1.Name = "RichTextBox1"
		Me.RichTextBox1.Size = New System.Drawing.Size(309, 175)
		Me.RichTextBox1.TabIndex = 8
		Me.RichTextBox1.Text = ""
		'
		'Form1
		'
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.ClientSize = New System.Drawing.Size(484, 262)
		Me.Controls.Add(Me.RichTextBox1)
		Me.Controls.Add(Me.Label3)
		Me.Controls.Add(Me.Label2)
		Me.Controls.Add(Me.Label1)
		Me.Controls.Add(Me.ComboBox1)
		Me.Controls.Add(Me.DateTimePicker1)
		Me.Controls.Add(Me.Button3)
		Me.Controls.Add(Me.Button2)
		Me.Controls.Add(Me.Button1)
		Me.Name = "Form1"
		Me.Text = "Form1"
		Me.ResumeLayout(False)
		Me.PerformLayout()

	End Sub
	Friend WithEvents Button1 As System.Windows.Forms.Button
	Friend WithEvents Button2 As System.Windows.Forms.Button
	Friend WithEvents Button3 As System.Windows.Forms.Button
	Friend WithEvents DateTimePicker1 As System.Windows.Forms.DateTimePicker
	Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
	Friend WithEvents Label1 As System.Windows.Forms.Label
	Friend WithEvents Label2 As System.Windows.Forms.Label
	Friend WithEvents Label3 As System.Windows.Forms.Label
	Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox

End Class

Open in new window


Produces the following output -Initial state.Randomly added terms.Cleared terms and all controls set back to initial state.
-saige-
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Closing Comment

by:cmed
ID: 40489224
@ Mike Tomlinson

This works great. I will probably want to change the mimimum date time so the user will not have to go as far as 1753, but it works great.
0
 

Author Comment

by:cmed
ID: 40489253
@ it_saige

The dates would change, but the term type would only be quarters of semesters.  Any suggestions.  

@ Mike Tomlinson

How could I change the dates from minimum date to current date or 1/1/2015, so the user would not have to scroll as far for a date?
0
 
LVL 33

Expert Comment

by:it_saige
ID: 40489273
What are the specific dates for your semesters or what are the criteria that constitutes a semester.  We could probably easily generate everything based on todays date and your criteria.

As for the minimum date.  You only need to set the minimum date property; e.g. - using Mikes implementation:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
	DTPTerm1.Format = DateTimePickerFormat.Custom
	DTPTerm1.CustomFormat = "MM/dd/yyyy"
	' Setting the minimum date
	DTPTerm1.MinDate = DateTime.Now
	DTPTerm1.Value = DateTimePicker.MinimumDateTime
	btnAddTermStart.Enabled = False
End Sub

Open in new window


-saige-
0
 

Author Comment

by:cmed
ID: 40489545
@ it_saige

Thanks for the help.  now I have to take the dates and term type from rich text box and now run the sql code and excel from another code that I created.
0
 

Author Comment

by:cmed
ID: 40489547
I have been look everywhere and trying to read information about this and cannot find anything.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now