-
Mar 22nd, 2013, 10:34 AM
#1
Thread Starter
New Member
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
-
Mar 22nd, 2013, 12:12 PM
#2
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!
-
Mar 22nd, 2013, 02:49 PM
#3
Thread Starter
New Member
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.
-
Mar 22nd, 2013, 03:12 PM
#4
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!
-
Mar 22nd, 2013, 06:43 PM
#5
Re: Need help in ComboBox Custom Class
Originally Posted by JB37
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 23rd, 2013, 03:17 AM
#6
Thread Starter
New Member
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
-
Mar 23rd, 2013, 09:32 AM
#7
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 23rd, 2013, 12:11 PM
#8
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!
-
Mar 24th, 2013, 07:39 AM
#9
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|