(RESOLVED) - button captions - please help
Hi,
Ok, so I have 2 forms.. I'll call them "form A" & "form B"
"Form A" has 72 buttons in an array - don' ask lol.. just bare with it please.
So it doesn't matter which button I click, "form B" will show.
"Form B" has a textbox and a button.. the issue I am having is whatever button I clicked on "form A", is changing that particular button caption through "form B".
When I try, I can only change the 1st one or all of them.. what I want to do is change the one I clicked on.
Thanks for the help in advance :)
Re: button captions - please help
Does form B have 72 buttons as well?
Re: button captions - please help
No, just a textbox and a button...
Re: button captions - please help
What code are you using at the moment?
Re: button captions - please help
Main.command1(index).caption = text1.text which only changes the first one
And if I do the for I = 0 to 71... I change all the buttons
Re: button captions - please help
I think you meant bear. Too darn cold here tonight to be taking my clothes off for a programming question.
Re: button captions - please help
Re: button captions - please help
I'm going to guess it's because Index is set to 0... it doesn't have the correct value.
-tg
Re: button captions - please help
This is the only way I could get it to work.
Main
Code:
Option Explicit
Private Sub Command1_Click(index As Integer)
Form2.Command1Click (index)
Form2.Show
End Sub
Form2
Code:
Option Explicit
Public Sub Command1Click(index As Integer)
Main.Command1(index).Caption = Text1.Text
End Sub
1 Attachment(s)
Re: button captions - please help
Attachment 118613
Code for Form1:
VB Code:
Option Explicit
Public cmdindex As Integer
Private Sub cmd_Click(Index As Integer)
Me.cmdindex = Index
Form2.Show
End Sub
Code for Form2:
VB Code:
Private Sub Command1_Click()
Form1.cmd(Form1.cmdindex).Caption = Me.Text1.Text
End Sub
Re: button captions - please help
Hmm... doesn't work for me..
I'll have to post my code tomorrow.. maybe seeing that, it will help.
Right now I do not have internet access, I'm using my phone to be here on the forum
Re: button captions - please help
Quote:
Originally Posted by
terry002
Code for Form2:
VB Code:
Private Sub Command1_Click()
Form1.cmd(Form1.cmdindex).Caption = Me.Text1.Text
End Sub
Not quite!
Code:
Private Sub Command1_Click()
Main.Command1(Main.cmdindex).Caption = Me.Text1.Text
End Sub
Re: button captions - please help
Nice! Ty very much
Ty everyone for helping
Re: button captions - please help
Quote:
Originally Posted by
elRuffsta
Nice! Ty very much
Ty everyone for helping
Have you "Resolved" the issue?
Re: (RESOLVED) - button captions - please help
Yes
Now tomorrow is gonna come the fun part of this project.. "database"
Re: button captions - please help
Quote:
Originally Posted by
Nightwalker83
Not quite!
...???? :confused:
Programming has a vast way of solutions...
It depends how you understand and how you approach it... As long as you know what you are doing. :rolleyes:
Re: (RESOLVED) - button captions - please help
Edit: Modified in response to techgnome's reply. See quote in following post for original comment and code.
Form1 Code:
Code:
Private Sub Command1_Click(Index As Integer)
Form2.Show vbModal, Me
If Form2.DoChange Then Command1(Index).Caption = Form2.NewCaption
Unload Form2
End Sub
Form2 Code:
Code:
Option Explicit
Private blnDoChange As Boolean
Property Get DoChange() As Boolean
DoChange = blnDoChange
End Property
Property Get NewCaption()
NewCaption = Text1.Text
End Property
Private Sub Command1_Click()
blnDoChange = True
Hide
End Sub
Private Sub Command2_Click()
blnDoChange = False
Hide
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If UnloadMode = vbFormControlMenu Then
Cancel = 1
blnDoChange = False
Hide
End If
End Sub
Re: (RESOLVED) - button captions - please help
Quote:
Originally Posted by
Logophobic
For what it's worth, Form2 should be Modal, and Form1 should reference a Form2 variable rather than Form2 referencing a Form1 variable.
Form1 Code:
Code:
Private Sub Command1_Click(Index As Integer)
Form2.Show vbModal, Me
If Form2.DoChange Then Command1(Index).Caption = Form2.Text1.Text
Unload Form2
End Sub
Form2 Code:
Code:
Option Explicit
Public DoChange As Boolean
Private Sub Command1_Click()
DoChange = True
Hide
End Sub
Private Sub Command2_Click()
DoChange = False
Hide
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If UnloadMode = vbFormControlMenu Then
Cancel = 1
DoChange = False
Hide
End If
End Sub
While that's better, it still results in tight coupling between the two forms. A best practice solution would have a property on Form2 that returns the value... rather than Form1 accessing the form2 textbox directly.
-tg
Re: (RESOLVED) - button captions - please help
Touche, and modified accordingly.
Re: (RESOLVED) - button captions - please help
Quote:
Originally Posted by
Logophobic
Touche, and modified accordingly.
Although it doesn't really make a difference, if you access a self-defined Public-Property of
a certain Form-Class - or a selfdefined TextBox on that same Form-Class - both require
that you use a concrete Form-Interface...
That said, when working with small Dialogue-Forms which often are shown only modally,
and have to return something to the caller - one can avoid using Hide or QueryUnload
and whatnot in the modal-form by simply defining a Public Function on it, which
internally also ensures the modal showing:
e.g. into Form2 (the one which is shown modally - having the usual Cancel and an OK-Buttons):
Code:
Option Explicit
Private mCaption As String
Public Function ShowModalAndGetNewCaption(OwnerForm As Object, CurCaption As String) As String
mCaption = CurCaption
Show vbModal, OwnerForm
ShowModalAndGetNewCaption = mCaption
End Function
Private Sub cmdOk_Click()
mCaption = txtNewCaption.Text
Unload Me
End Sub
Private Sub cmdCancel_Click()
Unload Me
End Sub
And within the Calling Form an explicit instantiation (combined with a single method call) is possible:
Code:
Option Explicit
Private Sub cmdArr_Click(Index As Integer)
With New Form2
cmdArr(Index).Caption = .ShowModalAndGetNewCaption(Me, cmdArr(Index).Caption)
End With
End Sub
The With construct above ensures, that an entire new Form2-instance is created -
and after the call (after running over End With) again absolutely destroyed.
Olaf
Re: (RESOLVED) - button captions - please help
Quote:
Originally Posted by
Schmidt
Although it doesn't really make a difference, if you access a self-defined Public-Property of
a certain Form-Class - or a selfdefined TextBox on that same Form-Class - both require
that you use a concrete Form-Interface...
True... but at least from that point, you only care about the property. You shouldn't need to care if it's Textbox1, Textbox2, or some other data source.
Quote:
Originally Posted by
Schmidt
That said, when working with small Dialogue-Forms which often are shown only modally,
and have to return something to the caller - one can avoid using Hide or QueryUnload
and whatnot in the modal-form by simply defining a Public Function on it, which
internally also ensures the modal showing:
e.g. into Form2 (the one which is shown modally - having the usual Cancel and an OK-Buttons):
Code:
Option Explicit
Private mCaption As String
Public Function ShowModalAndGetNewCaption(OwnerForm As Object, CurCaption As String) As String
mCaption = CurCaption
Show vbModal, OwnerForm
ShowModalAndGetNewCaption = mCaption
End Function
Private Sub cmdOk_Click()
mCaption = txtNewCaption.Text
Unload Me
End Sub
Private Sub cmdCancel_Click()
Unload Me
End Sub
And within the Calling Form an explicit instantiation (combined with a single method call) is possible:
Code:
Option Explicit
Private Sub cmdArr_Click(Index As Integer)
With New Form2
cmdArr(Index).Caption = .ShowModalAndGetNewCaption(Me, cmdArr(Index).Caption)
End With
End Sub
The With construct above ensures, that an entire new Form2-instance is created -
and after the call (after running over End With) again absolutely destroyed.
Olaf
I thought about that approach too, and I'd agree it's the best of the best practice... and is probably how it would be handled in the real world, but at the same time, I didn't want to potentially muddy things up with a concept the OP wouldn't be able to digest.
I do like it though. It is elegant. And decouples the forms completely.
-tg
Re: (RESOLVED) - button captions - please help
I just realized that we are reinventing the wheel here.
Code:
Private Sub Command1_Click(Index As Integer)
Dim NewCaption As String
NewCaption = InputBox("Enter new caption")
If NewCaption <> "" Then Command1(Index).Caption = NewCaption
End Sub