Solved

How do format a combo box control?

Posted on 2002-05-29
11
256 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…

762 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

25 Experts available now in Live!

Get 1:1 Help Now