Solved

How do format a combo box control?

Posted on 2002-05-29
11
264 Views
Last Modified: 2010-05-02
I am using WIN2000 Operating Systems with Visual Basic 6.0.  

I have a combo box that needs to have a certain format when the length of the string in the combo box reaches a length of four characters.

For example:  The first four characters in the combo box control has no format.  After the user types in the fourth character, a dash (-), shows up in the combo box.

A maskEdit control gives a format property, but can't use the maskedit because its a textbox and doesn't have user choices like a combo box.

This statement doesn't work:

cbo_models = format(cbo_models,  "####-##")

This may work when formatting dates and time.


Question:  How would you format a data string using the combo box control?

Thanks
The FireBird






0
Comment
Question by:BirdsOfFire1
  • 4
  • 4
  • 3
11 Comments
 
LVL 2

Expert Comment

by:Crin
ID: 7042889
Hi,

Usually I do it in combobox change event procedure.
There you can achieve its Text property and perform any operation you like.

So, this simple example will help (but will produce some problems with delete and backspace buttons usage - you will see):

Private sub Combo1_Change()
   If Len(Combo1.Text) = 4 then
      Combo1.Text = Combo1.Text & "-"
   end if
end sub

Since I don't know your requirements I can not suggest other options for combobox maintenance, so explain more and function will change with time :)

Sincerely,
Crin
0
 
LVL 22

Accepted Solution

by:
rspahitz earned 100 total points
ID: 7042900
How about putting some code in the change event:

private sub combo1_Change()
  if len(combo1.text) > 4 then
    if mid$(combo1.text,5,1)<>"-" then
      combo1.text = left$(combo1.text, 4) & "-" & mid$(combo1.text,5)
    endif
  endif
end sub

The main problem with this code will be if the user inserts or deleted code to the left of the "-".  For that, you may need to make the code more sophisticated and check the current cursor position.  Or if dashes are not allowed in the code, then simply remove it and reinsert it each time.

Also, you may want to restrict input further by adding code to the KeyPress event procedure.
0
 
LVL 2

Expert Comment

by:Crin
ID: 7042903
by the way, cursor position should be adjusted after "-" sign addition, so

Private sub Combo1_Change()
  If Len(Combo1.Text) = 4 then
     Combo1.Text = Combo1.Text & "-"
     Combo1.SelStart = 5 'Len(Combo1.Text)
  end if
end sub

Sincerely,
Crin
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 22

Expert Comment

by:rspahitz
ID: 7042907
Crin...you beat me to it...
0
 
LVL 2

Expert Comment

by:Crin
ID: 7043258
rspahitz, don't worry about it, I'm just nearby next t-shirt, so trying to write anasers as fast as possible :))))))

Sincerely,
Crin
0
 

Author Comment

by:BirdsOfFire1
ID: 7045098
rspahitz,

I added code to take the cursor to the very end of the text in the combo box.  see the third and fourth lines of code.  This works great when inputting data from left to right and when using the backspace key.

private sub combo1_Change()
 if len(combo1.text) > 4 then
   combo1.SelStart = Len(combo1)
   combo1.SelLength = Len(combo1)
   if mid$(combo1.text,5,1)<>"-" then
     combo1.text = left$(combo1.text, 4) & "-" & mid$(combo1.text,5)
   endif
 endif
end sub

This doesn't work so hot in using the delete key when deleting the 1st, 2nd, 3rd, 4th characters of the combo box.  The code still detects whether or if there is not a dash in the 5th position of the combo box.  If a dash is not in the 5th position, it puts a dash in that position.  You then have two dashes.  One in the third and fourth position, because the character deleted caused the text to left justify.

Having 7 characters in the combo box and pressing the delete key, say on the 1st digit of the combo box, this is what you get: 111--11.

Another solution would be to disable the delete key.  How would you do that?


The FireBird
 
0
 

Author Comment

by:BirdsOfFire1
ID: 7045125
rspahitz,

In reference to the "111--11"  result discussed above, the result needs to be "111-11". In other words, the position deleted (or character) should not show up in the combo box.

Thanks
The FireBird
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 7045168
There are many possible solutions, but try this one (which should be worthy of an "A" grade...):

Private Sub combo1_Change()
  ' remove and re-insert the hyphen, if needed, on every change
  Static bChanging As Boolean
  Dim iCurrentCursorPosition As Integer
 
  ' only process if not already changing
  If Not bChanging Then
    ' Save the cursor position
    iCurrentCursorPosition = Combo1.SelStart
    If InStr(Combo1.Text, "-") > 0 And iCurrentCursorPosition > 4 Then
      ' shift back on if the "-" is removed
      iCurrentCursorPosition = iCurrentCursorPosition - 1
    End If
   
    bChanging = True
    ' strip out the existing dash
    ' this will fire a change event
    Combo1.Text = Replace(Combo1.Text, "-", vbNullString)
   
    ' if the length >4, re-insert the dash
    If Len(Combo1.Text) > 4 Then
      Combo1.Text = Left$(Combo1.Text, 4) & "-" & Mid$(Combo1.Text, 5)
      ' and update the cursor position
      iCurrentCursorPosition = iCurrentCursorPosition + 1
    End If
    bChanging = False
    Combo1.SelStart = iCurrentCursorPosition
  End If
End Sub
0
 
LVL 2

Expert Comment

by:Crin
ID: 7045182
BirdsOfFire1, can you explain why you accepted second similar answer? We both proposed absolutely same routes but I proposed it faster... rspahitz, no offence, just curious...

Sincerely,
Crin
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 7045191
Crin, our code had minor differences, but I agree that you should have had the first chance to clean it up...

Look for a question for you...
0
 

Author Comment

by:BirdsOfFire1
ID: 7055145
To Crin:

Both methods would have worked.  What sparked my attention was the additional mid function.  I thank both of you in coming to my aid.  Next time Crin,  it might be your answer.


Thanks, Again.
The FireBird.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

821 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