Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I want what I type in a Text field to appear as a button's caption

Status
Not open for further replies.

tonedefiant

Programmer
Jun 11, 2007
2
US
I'm new to this, any tips are appreciated.

I set my toggle's triplestate=TRUE and the FALSE state sets focus to a text field, yeah!

Now, I want what I type in the text field to appear as the new caption of the toggle I just came from. Any ideas? This same text field will need to be reused for many more toggles, just like this one eventually. Here's my work below.


Private Sub UserForm_Initialize()
CheckBox1.Caption = "Value is True"
CheckBox1.Value = True
CheckBox1.TripleState = False

ToggleButton1.Caption = "1ENG/OFF1ENG"
ToggleButton1.Value = Null
ToggleButton1.TripleState = True

End Sub
Private Sub TextBox1_Change()

End Sub


Private Sub ToggleButton1_Change()
If IsNull(ToggleButton1.Value) Then
ToggleButton1.Caption = "1ENG/OFF1ENG"
ToggleButton1.BackColor = RGB(255, 255, 255)
ElseIf ToggleButton1.Value = True Then
ToggleButton1.Caption = "PASS"
ToggleButton1.BackColor = RGB(0, 128, 64)
ElseIf ToggleButton1.Value = False Then
ToggleButton1.BackColor = RGB(255, 0, 0)
TextBox1.SetFocus
End If
End Sub
 
as the new caption of the toggle I just came from.
Could you elaborate on the "just came from"?

The code you have is from the Help example on ToggleButtons. OK. The example in Help does not use a Textbox, but two toggle buttons.

You want a Textbox change event to find out where you "came from" - I assume this means the last control that had focus before you moved focus to the Textbox?

What if it was not a togglebutton? The what do you want the textbox change to do?

faq219-2884

Gerry
My paintings and sculpture
 
You guessed right, ripped it right from Help. There was a checkbox too; 2 toggle buttons and a checkbox, you left out the checkbox. Anyways, your assumption is correct thanks for correcting my terminology. I'll try again...


I want what I type in the text field to appear as the new caption of the toggle that had focus before I moved focus to the textbox.


fumei said:
What if it was not a togglebutton? The what do you want the textbox change to do?

I'm a little confused, but if I had to answer I'd say: I want the textbox to update the caption of the control, which was immediately before focused, to display the text I typed in the textbox. I appreciate your trying to pick my brain for a clearer picture, I hope this helps.

Maybe if I explain the bigger picture:
I want to make a checklist for work (I am a tester). The captions for my toggles will have an expected result which I need to confirm (language settings). When I select a toggle once, that confirms things are as expected (hence "PASS"). If I select the same toggle again that confirms that things are not as expected (hence a red bgrd). The purpose of updating the caption at this point is to reference the bug ID# corresponding to this red mark. I am open to ideas and appreciate your response, fumei.
 
Thanks for pointing out I "forgot" the checkbox, but since the checkbox is not all that relevant I did not see the point.
I want what I type in the text field to appear as the new caption of the toggle that had focus before I moved focus to the textbox.
This is poor programming. And that was my question. This is based on the assumption that the previous control that had focus before the textbox was a toggle. Yes, it could have been. Can you absolutely guarantee that?

Supposed you happened to brush the userform itself on your way to the textbox. You may immediately keep going and click into the textbox...BUT, the last control woukld not be a toggle, but the userform itself. In which case...what is the textbox change to do?

Next, the Textbox_Change event is dynamic. If you type in:

"hello", what actually happens is:

h - Textbox_Change fires
e - Textbox_Change fires
l - Textbox_Change fires
l - Textbox_Change fires
o - Textbox_Change fires


EVERY change fires _Change. This is also not efficient coding. Perhaps update the toggle caption when the textbox loses focus, or on Update, or something else.

The code you got from Help does not use a textbox (as I have pointed out) and therefore it is not the same situation as used in the Help example.
When I select a toggle once, that confirms things are as expected (hence "PASS"). If I select the same toggle again that confirms that things are not as expected (hence a red bgrd).
I am confused by this.

So, there you are. You select the toggle. This somehow confirms things are expected. (How, I have no idea.) Then - doing nothing else you select the toggle again. Somehow this confirms things are NOT as expected.

Hmmm. Seems strange to me. So, if things are not as expected you NEED to select the toggle twice????

Also, if you are always going from the toggle to the textbox, I would force that. Make a SetFocus to the textbox as the final instruction of the toggle_click event.

In any case, to answer your original question directly;
Code:
Private Sub TextBox1_Change()
   ToggleButton1.Caption = TextBox1
End Sub
This will change the ToggleButton1 caption on each change in the textbox.

Note that this does NOT change the caption of the togglebutton you "just came from". It explicitly changes the caption of ToggleButton1. Not any other control, not the one you just came from, but ToggleButton1.

Not sure if this is helping.

faq219-2884

Gerry
My paintings and sculpture
 
Hi,

as I can deduce, you have sheet that has points to be tested on them and for each point a togglebutton starting with value Null.

Then as you've tested a point, you click once if the test was succesfull (Togglebutton is now TRUE), but if the test fails, you click twice making the value of the botton FALSE. However, in this event, you want to update the caption of the togglebutton with some info as to why it failed from a textbox.

If so, the only way I can think of is along these lines:

Code:
Dim CurrObj As Object

Private Sub TextBox1_LostFocus()

CurrObj.Caption = Me.TextBox1.Value
Me.TextBox1.Value = vbNullString

End Sub

Private Sub ToggleButton1_Click()

Set CurrObj = Me.ToggleButton1
Call UpdateToggleButton(CurrObj)

End Sub

Private Sub ToggleButton2_Click()

Set CurrObj = Me.ToggleButton2
Call UpdateToggleButton(CurrObj)

End Sub

Sub UpdateToggleButton(OBJ As MSForms.ToggleButton)

With OBJ
    If .Value = False Then
        TextBox1.Activate
    Else
        .Caption = "PASS"
    End If
End With

End Sub

Cheers,

Roel


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top