Set ComboBox NOT editable?

I would like to use a ComboBox, where the user cannot edit the text. He should just be able to select an item. Using the 'Lock'-Property locks all the box with no more selection possible.

I think the question is easy, but I didn't find an answer fast. I'll spend 100 points even though it's probably easy and would be very glad to get an answer.

Thanks
db
LVL 1
db_tigerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RuchiCommented:

Add the following code to the module

Public Const EM_SETREADONLY = &HCF

Public Declare Function SendMessage _
   Lib "user32" Alias "SendMessageA" _
  (ByVal hwnd As Long, _
   ByVal wMsg As Long, _
   ByVal wParam As Long, _
   lParam As Any) As Long
   
Public Declare Function SetWindowText _
   Lib "user32" Alias "SetWindowTextA" _
  (ByVal hwnd As Long, _
   ByVal lpString As String) As Long

Public Declare Function FindWindowEx _
   Lib "user32" Alias "FindWindowExA" _
  (ByVal hWnd1 As Long, _
   ByVal hWnd2 As Long, _
   ByVal lpsz1 As String, _
   ByVal lpsz2 As String) As Long


Toss a Combo (style 0) and a command button onto a form, use the default names, and add following code to the form.


Sub Form_Load()
   Dim i As Integer

   For i = 1 To 10
      List1.AddItem Screen.Fonts(i)
   Next

End Sub


Private Sub Command1_Click()

   Dim hwndEdit As Long

   hwndEdit = FindWindowEx(Combo1.hWnd, 0&, vbNullString, vbNullString)
   
   If hwndEdit <> 0 Then

      Call SetWindowText(hwndEdit, "You can copy, but not change me  :-)")

      Call SendMessage(hwndEdit, EM_SETREADONLY, 1&, ByVal 0&)

   End If
   
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ghassan99Commented:
Place this in the combobox's keypress event:

Private Sub Combo1_KeyPress(KeyAscii As Integer)
   KeyAscii = 0
End Sub

-Gus
0
db_tigerAuthor Commented:
I like it very much to get fast answers!

Why are those things so complicated?!
Why isn't there a nice property 'ReadOnly' and there we go?

Thanks db


(Change the List1 to Combo1 in your sample code.)
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

db_tigerAuthor Commented:
Hy ghassan99,

the other answer was fast and correct, so I already accepted ist. But seems to me, that your code does the same much more easy.
I'll post a question for you and get you some 50 points

thanks db

Btw: Is there a way that, the user can't even highlight or select the contenst of the combobox?
0
ghassan99Commented:
I think the combo1.locked=true
accomplishes that.  Or u want something else?

-Gus
0
db_tigerAuthor Commented:
No, it doesn't.
I would like to allow the user to change the contents by selecting an item as it is possible with yours solutions.
But I would like it - for reason of asethtics - that he could'nt even enter the textbox-part of the combobox (which gets highlighted and shows the blinking cursor, when enterd - (even though the cursor can't be moved).

I think HTML-ComboBoxes behave like that. Not important, but nice to have.
0
RuchiCommented:
How are you doing with the combo box thing? Please let me know...
0
db_tigerAuthor Commented:
Hy Ruchi,

read my last comment: that's what I'm still seeking for.

Your API-Declarations are great, but ghassans99 hint does pretty the same thing with less code.
So if I won't get in problems, I'll take of course the short solution. But thanks anyway for your solution. It's correct and you gave me a possible solution fast.

db
0
db_tigerAuthor Commented:
Hy again Ruchi,

I apologize: Your solution is superior! gansen99 idea lacks one major point - the user is able to delete the contents of the textbox-part by pressing <delete> - that is not acceptable, so your solution wins.
The points you got anyway.

by db
0
RuchiCommented:
Db,

Do you want to know how to "allow the user to change the contents by selecting an item as it iss possible with my solutions?" If so, please let me know... Or did you work? (I skimmed your comments because I am kind of in a hurry)

Thanks. :)
0
db_tigerAuthor Commented:
No I'm fine with that.

I would appreciate one further point: I would like it, if the user wasn't even able to enter the TextBox-Part of the comboBox with the cursor.
Your solution is fine, but if the user clicks in the combo-Box the text gets highlighted, seems to be editable and the cursor blinks.
 It would be nicer, to avoid this behaviour, because it's kind of misleading: The TextBox seems to be editable but isn't.

Don't worry Ruchi, if you're in troublke with some other work. I'm already fine with your solution.
It's just a small thing, which would be nice to have.

db

0
ghassan99Commented:
db_tiger,
u can disable all kind of keyboard clicks like del, ins...even ctrl+c to copy or ctrl+x to cut, just place this in the keydown event of the combo box:
KeyCode = 0
very simple!

-Gus
0
RuchiCommented:
I'm going to work on a small thing for you.. by tonight or tomorrow.

:)
0
ghassan99Commented:
I got what u want db, just change the style of the combo box to 2 or 'Dropdown list' u dont even need to disable keyboard strokes, coz in that style u cant change the values.

-Gus
0
db_tigerAuthor Commented:
ghassan99

First comment: That's not true: Select the Box, the text get highighted, try to type some character - does not work - try to type 'Delete' - the entries disappear.

ghassan99

Second comment: Now you really got, what I want: That is even more simple than both solution before.
I'm a little ashamed, that I did not found it by myself before, but now you made the work for me.
I thought that the MS-Box should have an optioin like that, because it's too usual to be more complicated.

Thanks again, db

ruchi:

Hope you read the comments before spending time on 'the little thing'. Thanks again for the API-declarations, which did the job - but of course using one simple property is the way, it should be done - if possible.

Thank you, db


0
ghassan99Commented:
Glad it worked db.
But again u didnt probably got me, yes the delete will work, but place an additional code of one line in the KEYDOWN event not KEYPRESS...
keycode=0
and also keep the orginal code in the KEYPRESS event
keyascii=0
then there is nothing u can type anymore, not even the delete.

-Gus
0
db_tigerAuthor Commented:
Oh you are right Gus, I didn't read that - KeyDOWN supplementaire to KeyPRESS: this way it does the same as ruchi API-calls.
But the correct setting of the appropriate property ('style') is the best way.

Thanks again, lots of comments on at least a simple question :-)

db
0
RuchiCommented:
I agree with Gus's comments on drop-down list - 2. With the drop-down list, the user cannot type an alternative item. I'm glad it worked fine for you.
:)
All the best,
Ruchi
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.