Results 1 to 8 of 8

Thread: [RESOLVED] Passing an Array to Custom Messagebox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Roodepoort, South Africa
    Posts
    484

    Resolved [RESOLVED] Passing an Array to Custom Messagebox

    How do one pass an array to and from a custom messagebox/inputbox?

    I'm trying to create a custom messagebox/inputbox where I can enter values into certain fields and then return those values as an array.

    However...

    The custom messagebox/inputbox must also return a string or integer so I can know whether Ok or Cancel has been selected.

    So my thinking was;
    1. Create the array in my form.
    2. Pass that array to the custom messagebox/inputbox form by reference so that changing values in the array will be reflected in the original form after the custom messagebox/inputbox form are closed.
    3. Enter the values into the array in the messagebox/inputbox form.
    4. Return the button clicked as the return value of the custom messagebox/inputbox when the box is closed.


    Is this possible? How do I declare/pass the array from one form to another and back again?

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Passing an Array to Custom Messagebox

    Quote Originally Posted by Bezzie View Post
    How do one pass an array to and from a custom messagebox/inputbox?
    Not sure what you mean by "custom"... Do you already have some custom form?
    If you don't then for what you want you'll need to design your own form that would act like Message and/or Input box.
    If you have troubles designing your own forms search forum - there are many samples already posted.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Roodepoort, South Africa
    Posts
    484

    Re: Passing an Array to Custom Messagebox

    Busy designing the form. I have done custom messageboxes before and know how to get one return value from the messagebox for the button clicked.

    How do I pass an array to this custom form so that I can modify the values of the array and then return that array as well so the new values is available in the form that called the custom messagebox.

  4. #4

  5. #5
    Addicted Member Optional's Avatar
    Join Date
    Jan 2010
    Location
    Rudimentary Space
    Posts
    214

    Post Re: Passing an Array to Custom Messagebox

    This is a very simplified proof of concept.

    I'm assuming when you are talking about a custom messagebox you are talking about a form acting as a custom messagebox.

    Assuming that is correct my form2 in my sample would be your custom messagebox and my form1 would be the calling form.

    In form2 (acting as custom messagebox) I have the following code:
    VB Code:
    1. Private pMyArrayData() As String
    2.  
    3. Public Property Let MyArrayData(value() As String)
    4.     pMyArrayData = value
    5. End Property
    6.  
    7. Public Function ShowForm() As String()
    8.     Me.Show vbModal
    9.     ShowForm = pMyArrayData
    10. End Function
    11.  
    12. Private Sub uiChangeArrayData_Click()
    13.     ReDim pMyArrayData(10)
    14.     Unload Me
    15. End Sub

    uiChangeArrayData is the name of a command button on Form2.

    The puplic property will allow you to assign the array to modify from anywhere outside form2.

    The reason I would do that rather than passing anything by reference is my personal preference and serves as one example of achieving what you intended to do.
    This does not mean off course that you can't do it passing anything ByRef.

    The code on Form1 which will show Form2 is as follows:
    VB Code:
    1. Private Sub uiShowCustomMessageBox_Click()
    2.     Dim mySourceArray() As String
    3.     ReDim mySourceArray(100)
    4.    
    5.     MsgBox UBound(mySourceArray)
    6.  
    7.     Load Form2
    8.    
    9.     Form2.MyArrayData = mySourceArray
    10.     mySourceArray = Form2.ShowForm()
    11.    
    12.     Set Form2 = Nothing
    13.    
    14.     MsgBox UBound(mySourceArray)
    15. End Sub

    uiShowCustomMessageBox is the name of my command button on Form1.

    The combined code above will declare an array in Form1 with 100 elements and display that count in a messagebox.

    Form1 will then assign that array to the puplic property of Form2 and then call the ShowLoad method of Form2.

    Form2 will show itself in vbModal to ensure the code will continue in the ShowForm method ones Form2 is closed by any means.

    Form2 then changes the size of the array to demonstrate it has changed it before Form2 then unloaded which will return to Form1.
    (I unload Form2 manually in the command button click event simply to demonstrate it can be done, clicking the X on form2 will achieve the same result).

    Form1 then will show again the count of elements to show the changes done in Form2 have been applied to the local array in Form1.

    I hope this helped in giving you some ideas of your own. This is simply one option of many on how to "pass" an array between forms.

    Edit
    Just read the post on declaring the array in a module.
    Yes, you can do that and it will work.
    Personally I always use modular or global declerations as very last choice if at all.
    In a module I feel I have less control/oversight over who and what accesses the variable.

    By making it part of the "object" though (form2) I feel more in control what is changing my variable.

    That is just personal choice though.



    Kind Regards,
    Optional



    If you feel this post has helped in answering your question please return the favour and Rate this post.
    If your problem has been solved and your question has been answered mark the thread as [RESOLVED] by selecting the Thread Tools menu option at the top and clicking the Mark Thread Resolved menu item.


    VB6 - (DataGrid) Get the Row selected with the right mouse button



  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Roodepoort, South Africa
    Posts
    484

    Re: Passing an Array to Custom Messagebox

    I currently have the array declared as a global variable. It works but I'm not too keen on using too many global variables if it can be avoided. Simply because it's all too easy to accidentally change a variable and then try to track down why something that should work, doesn't.

    Optional: This is exactly my idea. I'm going to look into your example. At first glance it looks to be what I'm after. The reason why I was thinking to pass the array by reference is that I also need to return a value to indicate what button the user selected e.g. Ok or Cancel. How would one go about to pass the array by reference? Would it be as simple as declaring it
    Code:
    Public Property Let MyArrayData(ByRef value() As String)
    Or am I not that lucky?

  7. #7
    Addicted Member Optional's Avatar
    Join Date
    Jan 2010
    Location
    Rudimentary Space
    Posts
    214

    Re: Passing an Array to Custom Messagebox

    In that case you could just pass the array to ShowForm and remove the puplic property, keep the private variable though for internal use.

    Add another private variable for the user choice and make ShowForm of type vbMsgBoxResult.

    Change you code in form2 to this:
    VB Code:
    1. Private pMyArrayData() As String
    2. Private pUserChoice As VbMsgBoxResult
    3.  
    4. '-- Pass the array by reference and have the method return the result of the user's choice.
    5. Public Function ShowForm(ByRef myArrayData() As String) As VbMsgBoxResult
    6.     pMyArrayData = myArrayData
    7.    
    8.     Me.Show vbModal
    9.    
    10.     myArrayData = pMyArrayData
    11.    
    12.     ShowForm = pUserChoice
    13. End Function
    14.  
    15. '-- First Command Button to simulate change of array data.
    16. Private Sub uiChangeArrayData_Click()
    17.     ReDim pMyArrayData(10)
    18. End Sub
    19.  
    20. '-- Second Command button to deomnstrate User Choice being set.
    21. Private Sub uiSetUserChoice_Click()
    22.     pUserChoice = vbOK
    23. End Sub
    24.  
    25. '-- Will unload the messagebox, naturally that would actually
    26. '-- happen upon the user's choice but this is simply to demonstrate.
    27. Private Sub uiCloseMessageBox_Click()
    28.     Unload Me
    29. End Sub

    Again, the above is merely a demonstration of one of many options you have available. don't forget to think about validations, as in what should the default return value be. Use your Form_initialize to pre-load your defaults, etc....

    Your Form1 would change to something like this:
    VB Code:
    1. Private Sub uiShowCustomMessageBox_Click()
    2.     Dim mySourceArray() As String
    3.     Dim userChoice As VbMsgBoxResult
    4.    
    5.     ReDim mySourceArray(100)
    6.  
    7.     MsgBox UBound(mySourceArray)
    8.  
    9.     Load Form2
    10.  
    11.      userChoice = Form2.ShowForm(mySourceArray)
    12.  
    13.     '-- If you do not set Form2 to nothing some events like Form_initialize will
    14.     '-- not trigger next time you load Form2 as VB will keep
    15.     '-- the code page of a form object alive in memory until you do so, even
    16.     '-- if you have unloaded the form2 object.
    17.     '-- Remember that.
    18.     Set Form2 = Nothing
    19.    
    20.     MsgBox userChoice
    21.     MsgBox UBound(mySourceArray)
    22. End Sub

    As you can see, the above would let you pass the array by reference and still receive the user's choice of the vbMsgBoxResult.
    Again, this is simply one option and there might be many more ways to achieve the same.

    Hope this helps though.



    Kind Regards,
    Optional



    If you feel this post has helped in answering your question please return the favour and Rate this post.
    If your problem has been solved and your question has been answered mark the thread as [RESOLVED] by selecting the Thread Tools menu option at the top and clicking the Mark Thread Resolved menu item.


    VB6 - (DataGrid) Get the Row selected with the right mouse button



  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Roodepoort, South Africa
    Posts
    484

    Re: Passing an Array to Custom Messagebox

    I'm going to look into this as soon as I get a chance. Thanks in advance Optional.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width