Results 1 to 8 of 8

Thread: Pointers

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2004
    Location
    Brazil
    Posts
    10

    Question Pointers

    Dear all,

    I would like to pass a string as parameter to a form that would modify it. Then, as a result, I would like, when finish the form, to change this variable.

    Observe that the I will need to access the parameter in many subs and functions before it is ready to be returned.

    I am thinking use pointers to this string to have how to modify it?
    How can I do it?

    If I pass a Label.Caption as parameter, could I modify it on a function?

    Thanks in advance,
    Nizam

  2. #2
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    You can pass objects through forms. Here is a small sample project:

    VB Code:
    1. 'Form1:
    2. Option Explicit
    3. Private Sub Command1_Click()
    4.     Form2.SetLabel Label1
    5.     Form2.Show vbModal
    6. End Sub

    VB Code:
    1. 'Form2:
    2. Option Explicit
    3. Dim MyLabel As Label
    4. Public Sub SetLabel(ByRef TheLabel As Label)
    5.     Set MyLabel = TheLabel
    6. End Sub
    7. Private Sub Command1_Click()
    8.     MyLabel.Caption = MyLabel.Caption & " Changed content"
    9.     Unload Me
    10. End Sub
    11. Private Sub Form_Unload(Cancel As Integer)
    12.     Set MyLabel = Nothing
    13. End Sub

    Hope this helps

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132
    You may certainly pass label's caption as parameter anywhere in your project.
    It will, however, be beneficial for you (and your program) if you create a Public Property directly on your form that processing this value so you may access it regadless of form being loaded into a memory.

    Let me know if you need more help on this subject.

    Best regards.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2004
    Location
    Brazil
    Posts
    10
    But I would like to change the string that I passed as parameter and I would not be able to do this working with a form property. How do I implement an event to observe this new property changes?

    With the object label worked because I pointed the both objects to the same address. How could I do this with strings?

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

    - one (not the best but will work for you) is to use timer and every once often update your label's caption:

    Label1.Caption = frmProcess.SomeProperty

    - another is to set label's caption directly from that form every time property is changed:

    frmMain.Label1.Caption = SomeProperty

    ... and so on ...

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2004
    Location
    Brazil
    Posts
    10

    Question

    BTW, how can I create a new label?

    Set Label1 = New Label

    is note working...

  7. #7
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    If you really really need to do it with strings, you need to use CopyMemory to edit the variable's address. I have never done this: you should find out where the variable's location is stored in memory and then change it to point to the new location in memory. Imo not worth it (because it is probably different in compiled program).

    Just declare a public string in a module and then use it on both forms.

  8. #8
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132
    Originally posted by nizam
    BTW, how can I create a new label?

    Set Label1 = New Label

    is note working...
    In VB6 there are two ways to create a new control at run time:

    1. Control Arrays.
    2. Controls collection.

    If you want to create a brand new instance then use method 2:
    VB Code:
    1. Dim lbl As Label
    2.  
    3.     Set lbl = Me.Controls.Add("VB.Label", "Label1")
    4.     lbl.Move 300, 300
    5.     lbl.Caption = "Label1"
    6.     lbl.Autosize = True
    7.     lbl.Visible = True
    ... to remove control from controls collection use Remove method:
    VB Code:
    1. Me.Controls.Remove "Label1"

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