Need help in ComboBox Custom Class-VBForums
Results 1 to 9 of 9

Thread: Need help in ComboBox Custom Class

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    13

    Need help in ComboBox Custom Class

    Hi

    I am using VB 2010 Express.

    I have the need for a Custom ComboBox where there is a TextBox placed on top of the ComboBox (replacing the ComboBox Text Area). I created the Class as shown:

    Code:
    Imports System
    Imports System.Windows.Forms
    Imports System.Drawing
    
    Public Class TextBoxCombo
        Inherits ComboBox
        Friend WithEvents ComboText As New TextBox
    
        Private ButtonWidth As Integer = 18
    
        Public Sub New()
            ComboText.Location = New Point(Me.Location.X, Me.Location.Y)
            ComboText.Size = New Size(Me.Width - ButtonWidth, Me.Height)
            ComboText.BackColor = Color.LightGreen     ' So I can see the TextBox
            ComboText.BringToFront()
            ComboText.Name = "ComboText"
            Me.Parent.Controls.Add(ComboText)
        End Sub
    End Class
    My problem is that the TextBox does not show when I place the Custom control on a form. I added the TextBox to the parent container of the inherited ComboBox. I did try a UserControl with a ComboBox and TextBox but had some sizing problems with it.

    Can someone tell me what I am doing wrong? I am sort of new to VB.Net and am just learning it.

    Jerry Bodoff

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: Need help in ComboBox Custom Class

    I have the need for a Custom ComboBox where there is a TextBox placed on top of the ComboBox
    Why. What's wrong with the textbox that's already part of the standard combobox (that is after all what combo means in this context!!!!)

    My problem is that the TextBox does not show when I place the Custom control on a form.
    No of course it doesn't. The active control is still the combobox. You haven't replaced the entry space of the combobox so it will always take precedence when the control is active.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    13

    Re: Need help in ComboBox Custom Class

    Hi

    I do know that but I need to know where the mouse cursor is in the TextBox portion of the ComboBox when I right click to display a ContextMenu. I searched for a solution and tried a number of scenarios trying to translate the mouse position to a caret position in the TextBox with no success. I insert special character(s) from a menu at the cursor position. Overlaying with a TextBox was the only way I saw to accomplish the task.

    I guess the question is now how do I replace the ComboBox Text area with a TextBox. Is the solution to use a UserControl with both a ComboBox and a TextBox?

    Jerry Bodoff
    Last edited by JB37; Mar 22nd, 2013 at 02:52 PM.

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: Need help in ComboBox Custom Class

    Huh? Like I said it's just another textbox ...

    If ComboBox1.Text <> "" Then
    CaretPos = ComboBox1.SelectionStart
    End If
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,474

    Re: Need help in ComboBox Custom Class

    Quote Originally Posted by JB37 View Post
    Hi

    I do know that but I need to know where the mouse cursor is in the TextBox portion of the ComboBox when I right click to display a ContextMenu. I searched for a solution and tried a number of scenarios trying to translate the mouse position to a caret position in the TextBox with no success. I insert special character(s) from a menu at the cursor position. Overlaying with a TextBox was the only way I saw to accomplish the task.

    I guess the question is now how do I replace the ComboBox Text area with a TextBox. Is the solution to use a UserControl with both a ComboBox and a TextBox?

    Jerry Bodoff
    the _mousemove event exposes the mouse position within the control, but as dunfiddlin said the textbox part of the combobox works as a textbox. in this example, i used a combobox, a contextmenustrip, + 2 labels:

    Code:
    Public Class Form1
    
        Private WithEvents cms As New ContextMenuStrip
    
        Private Sub ComboBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ComboBox1.MouseMove
            Label1.Text = e.Location.ToString
            Label2.Text = ComboBox1.PointToClient(Cursor.Position).ToString
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            cms.Items.Add("Insert", Nothing, AddressOf clicked)
            ComboBox1.ContextMenuStrip = cms
        End Sub
    
        Private Sub clicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
            ComboBox1.Text = ComboBox1.Text.Insert(ComboBox1.SelectionStart, "test")
        End Sub
    
    End Class

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    13

    Re: Need help in ComboBox Custom Class

    Hi

    Thanks for your help.

    I put a ComboBox and ContextMenu on a form. The ComboBox used the ContextMenu. I then ran the following:

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
            With Combox1
                .SelectedIndex = 0
                .SelectionStart = 0
                .SelectionLength = 0
            End With
    
            TextBox1.Text = "Text Box Text"
        End Sub
    
        Private Sub ToolStripMenuItem1_Click(sender As Object, e As System.EventArgs) Handles ToolStripMenuItem1.Click
            Dim CaretPosition = Combox1.SelectionStart
    
            MsgBox("ComboBox Caret Position = " & CaretPosition.ToString)
        End Sub
    End Class
    The caret position was always at the beginning or end of the ComboBox Text. I agree it is supposed to be a TextBox but from what I have read during searching for an answer I am not the only one with this problem and need. The ComboBox Text area really doesn't act as a true TextBox.

    I decided to do the overlay in my Main form (I have 4 instances of this situation).

    I guess we can say that this issue is resolved.

    Jerry B

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,474

    Re: Need help in ComboBox Custom Class

    clicking on a ToolStripMenuItem removes the focus from the combobox, so thats why you can't get the correct Caret Position

  8. #8
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: Need help in ComboBox Custom Class

    The ComboBox Text area really doesn't act as a true TextBox.
    Odd because it actually is a TextBox! You would get exactly the same result in a free standing TextBox for the reasons that .paul stated. To get a caret position you need a caret!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  9. #9

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    13

    Re: Need help in ComboBox Custom Class

    Hi dunfiddlin, paul

    Thanks for your help.
    As I said I ended up doing this in the form itself.

    First I defined a global value:
    Private InsertIndex As Integer

    Then for each of the TextBoxes overlaying the ComboBox I have the following:
    Code:
    Private Sub ComboText_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) _
                                    Handles txtCboCountry.MouseDown, _
                                            txtCboCatalog.MouseDown, _
                                            txtCboSubject.MouseDown, _
                                            txtCboItemType.MouseDown, _
           InsertIndex = DirectCast(sender, TextBox).GetCharIndexFromPosition(e.Location)
    End Sub
    Then to Insert only pre-defined characters I have the following (imInsertSymbol from ContextMenu):
    Code:
    Private Sub imInsertSymbol_Click(sender As Object, e As System.EventArgs) _
                                     Handles imInsertSymbol.Click
            Dim MenuParent As TextBox = DirectCast(cmsInsertMenu.SourceControl, TextBox)
    
            With New dlgInsertSymbols
                .ShowDialog(MenuParent, InsertIndex)
            End With
    End Sub
    With the ComboBox alone the ComboBox.SelectionStart was correct if I first did a Left Click followed by a Right Click. I was using the ComboBox_MouseDownEvent. Using Right Click only the SelectionStart was wrong. It would have been nice if it was correct.

    Jerry B

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.