Link to home
Start Free TrialLog in
Avatar of aanimesh
aanimesh

asked on

how to clear dynamic textboxes placed on the screen and how to scroll down if all text boxes cannot be displayed

hi all,

Using the code below in a loop i am printing text boxes dyanamically that contain text from an array.

<vb>
 Call Controls.Add("VB.TextBox", "Text" & CStr(iCount), Controls(sFrame))
                Load Text0(iCount)
                Set Text0(iCount).Container = Controls(sFrame)
                With Text0(iCount)
                    .Visible = True
                   
                    .Width = 3035 + width1
                    .Top = 200 + (x * (height1 + 350))
                    '.Tag = .Top
                    .Tag = MessageArray2(pathArray(i), 1)
                    .Left = 1000
                    .Height = 315 + height1
                      .Text = MessageArray2(pathArray(i), 2)
                                       
                End With
                Controls.Remove "Text" & CStr(iCount)
----------------------------------------------------------------------------
</vb>          

this number of elements can be different everytime the procedure is called. so, if there were 5 messges in last array and next array has only 3, still the last 2 test boxes can be seen. I want to clear the screen before i print new array in textboxes.

A related question is how to provide scrolling (without any significant flickering if possible) for these dynamic textboxes that are placed on a form (if they cannot fit visible screen).
please let me know if you need any further clarification.

thanks a lot,
regards
animesh

Avatar of sudhakar_koundinya
sudhakar_koundinya

Dim prevcount As Integer
Private Sub Command1_Click()
On Error Resume Next
Call remove

For i = 1 To Val(aTextBox.Text)
Dim a As TextBox
Dim prev As TextBox

Set a = Controls.Add("VB.TextBox", "Text" & i, Form1)

a.Visible = True
       a.Move prev.Left, _
        prev.Top + prev.Height + 30, _
        prev.Width, prev.Height
   a.Text = a.Name
   
   Set prev = a


   

Next
prevcount = Val(aTextBox.Text)

End Sub

Private Sub remove()
Dim i As Integer
i = 0
MsgBox prevcount
For i = 1 To prevcount

Dim a As TextBox

Set a = Controls.Add("VB.TextBox", "Text" & i, Form1)
MsgBox a.Name
i = i + 1
Call Controls.remove(a)
MsgBox a
MsgBox a
Next
End Sub

Private Sub Form_Load()
prevcount = 0
End Sub





May be some logical mistakes are there

but this idea works
ASKER CERTIFIED SOLUTION
Avatar of sudhakar_koundinya
sudhakar_koundinya

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aanimesh

ASKER

hi sudhakar,

thanks a lot man, i tried it as a standalone code, it works perfectly...
will try tomorrow in my office....

i am sure it would work with my overall application....
i will go through the links that you have provided....actually i had a piece of code that i am using but it behaves very erratically...don't know what mistake i am making..
will try to fix it myself but if cannot then again come back for help..

thanks a lot man.....
take care and enjoy
animesh

hi sudhakar,

i again looked at your proposed solution....but since i am using an control array...it might not work....

as u would recall, i used control array as i need to write code that executes when a text box is clicked (you earlier also had suggested this alternative way of dynamically generating boxes to resolve another problem but then ashoo...told me to use the tag attribute to hold the unique identifier associated with each textbox and therefore i continue to use this method).
THIS MEANS THAT each box has the same name and it seems that i cannot delete the boxes by using their names..(as i lose control of the box once it is written and removed)

PLEASE let me know how can deletion operation be done if i am using the control array.
regards,
animesh
all these textboxes are placed on a frame....
is it possible to clear everything that is on the frame by using some command (before i write elements of the new array)

waiting for ur answer
Dim prevcount

Private Sub Command2_Click()
On Error Resume Next
For i = 1 To Val(Text2.Text)
Dim a As TextBox
   Load Text1(i)
   Text1(i).Visible = True
   Set a = Text1(i)
       a.Move prev.Left, _
        prev.Top + prev.Height + 30, _
        prev.Width, prev.Height
   a.Text = a.Name
   
   Set prev = a

   
   
Next i
prevcount = Val(Text2.Text)

End Sub

Private Sub Command3_Click()

For i = 1 To prevcount
Dim a As TextBox
   Unload Text1(i)
   
   
   
Next i
End Sub

Private Sub Form_Load()
prevcount = 0
End Sub


Hope the solution helps
you can create the environment as suggested by ashoo
>> actually i had a piece of code that i am using but it behaves very erratically

is it possible to see your code
hi sudhakar,
thanks a lot man for your prompt replies.

well my environment has always been the one with control arrayt to create dynamic textboxex. however since i was not able to give them a unique name, ashoo's idea of using tag to store such unique id solved my problem...but now the problem is to clear the frame before i display a new array in text boxes (and i guess that we do not have handle to those boxes displayed on the screen to be able to remove them).

i tried your new code, it is giving me errors (says "wrong no. of arguments or invalid property assignment" ...guess at this line "Load Text1(i)"
...dont know why?
I hope you have understood what i am trying to do/..le tme know if i have not been able to explain it clearly.

sure u can see the code...the only reason i did not post it earlier is that it is messy....actually i am doing this on my own so there might be no structure...no comments...copy paste code etc etc. ..am just playing with VB and trying to learn something....am not good at vb..and am learning....so hope you won;t mind the sloppiness in the code... :-)

here is all the code related to the text boxes.....
<vb>
'dynamic text object creation on the forms
Private Sub TabStrip1_BeforeClick(Cancel As Integer)
Dim sFrame As String
'
' Hide the frame for the tab we are leaving.
'
sFrame = "Frame" & CStr(TabStrip1.SelectedItem.index)
Controls(sFrame).Visible = False
End Sub

'dynamic text object creation on the forms
Private Sub TabStrip1_Click()
Dim sFrame As String
'
' Show the frame for the tab we are displaying.
' Set the values for its scrollbar.
'
sFrame = "Frame" & CStr(TabStrip1.SelectedItem.index)
Controls(sFrame).Visible = True

Call pUpdateScrollBar(sFrame)
End Sub

'dynamic text object creation on the forms
Private Sub Text0_Click(index As Integer)
'MsgBox "Textbox" & CStr(Index) & " Was Clicked", vbInformation, "Dynamic Form"
'my code
'sFrame = "Frame" & CStr(1)

If (lastMessageID < 0) Then
   '  Load Text0(Index)
                Set Text0(index).Container = Controls(sFrame)
                With Text0(index)
                    .ForeColor = &HC0C000
                    '.FontBold = True
                    .Visible = True
                     
                    .Width = Text0(index).Width
                    .Top = Text0(index).Top
                   
                    '.Tag = .Top
                    .Left = 1000
                    .Height = Text0(index).Height
                      .Text = Text0(index).Text
                       ' to find out how to change the read only properties of the dynamic control
                     '  .Name = Text0(Index).Name
                      '   pmid = .Name
                      .Tag = Text0(index).Tag
                      pmid = .Tag
                     
                End With
               ' Controls.Remove "Text" & CStr(Index)
                lastMessageID = index
                highlightedMID = pmid
               
               
Else
           ' Load Text0(lastMessageID)
                Set Text0(lastMessageID).Container = Controls(sFrame)
                With Text0(lastMessageID)
                    .ForeColor = &H80000012
                    '.FontBold = False
                    .Visible = True
                      .Width = Text0(lastMessageID).Width
                    .Top = Text0(lastMessageID).Top
                   ' .Tag = .Top
                    .Left = 1000
                    .Height = Text0(lastMessageID).Height
                      .Text = Text0(lastMessageID).Text
                       ' to find out how to change the read only properties of the dynamic control
                      '.Name = Text0(lastMessageID).Name
                      .Tag = Text0(lastMessageID).Tag
                End With
                'Controls.Remove "Text" & CStr(lastMessageID)

' Load Text0(Index)
                Set Text0(index).Container = Controls(sFrame)
                With Text0(index)
                    '.FontBold = True
                      .ForeColor = &HC0C000
                    .Visible = True
                    .Width = Text0(index).Width
                    .Top = Text0(index).Top
                    '.Tag = .Top
                    .Left = 1000
                    .Height = Text0(index).Height
                      .Text = Text0(index).Text
                       ' to find out how to change the read only properties of the dynamic control
                     ' .Name = Text0(Index).Name
                     'pmid = .Name
                      .Tag = Text0(index).Tag
                      pmid = .Tag
                End With
                highlightedMID = pmid
               ' Controls.Remove "Text" & CStr(Index)
               
                 lastMessageID = index

End If

'pmid = CStr(Index)
txtmsg.SetFocus

        'end
               
End Sub

'dynamic text object creation on the forms
Private Sub pUpdateScrollBar(sFrame As String)
'
' If the height of all the controls is greater
' then the height of the the frame, display
' a scrollbar.
'
With Controls(sFrame)
    If Val(.Tag) > Val(.Height) Then
        VScroll1.Visible = True
        VScroll1.Max = ((Val(.Tag) - Val(.Height)) / 400) + 1
        bResetting = True
        VScroll1.Value = colScroll(sFrame)
        bResetting = False
    Else
        VScroll1.Visible = False
    End If
End With
End Sub


'dynamic text object creation on the forms
Private Sub VScroll1_Change()
Call pScroll
End Sub

'dynamic text object creation on the forms
Private Sub VScroll1_Scroll()
Call pScroll
End Sub

'dynamic text object creation on the forms
Private Sub createFormtoWrite()

bLoading = True
iOldControls = Controls.Count

iCount = 1
'
' Start with a TabStrip control with a single tab.
' Add new tabs, for a total of 4, and populate each one.
'
For iTab = 1 To 1
    '
    ' If we are not on the first tab then
    ' add a Tab.  The first one already exists.
    '
    If TabStrip1.Tabs.Count < iTab Then
        TabStrip1.Tabs.Add
    End If
    'TabStrip1.Tabs(iTab).Caption = "Tab Caption" & CStr(iTab)
    TabStrip1.Tabs(iTab).Caption = "Conversation " & CStr(iTab)
    '
    ' Add a frame. The frame is a "container" control
    ' and allows us to easily show the appropriate
    ' controls when a tab is selected.
    '
    sFrame = "Frame" & CStr(TabStrip1.Tabs.Count)
    Controls.Add "VB.Frame", sFrame, Me
    'Position the frame and set its properties.
    With Controls(sFrame)
        .Top = TabStrip1.ClientTop + 150
        .Left = TabStrip1.ClientLeft + 60
        .Height = TabStrip1.ClientHeight - 150
        .Width = TabStrip1.ClientWidth - VScroll1.Width - 120
        .BorderStyle = 0
        .Visible = False
        .ZOrder (0)
    End With
    '
    ' Maintain a collection of scrollbar values
    '
    colScroll.Add 0, sFrame
   
    bLoading = False
'
' Display the first tab and set the
' values of its scrollbar.
'
Next
TabStrip1.Tabs(1).Selected = True

Call pUpdateScrollBar("Frame1")
     

End Sub


'populating the text boxes which are dynamically creation on the form
' to find out a way to dynamically setting the height of the text box or to activate text wrap up option.
Private Sub writeMessages()
' On Error Resume Next


Dim x As Integer
Dim k As Integer
Dim i As Integer


height1 = 300
width1 = 5000

      'lastMessageID = -1
'List1.Clear

   
    i = pathlength - 1
   
   
   
   
     
   
   
    'For x = 1 To (TotalMessages - 1)
    For x = 1 To (pathlength - 1)
        '
        ' Set the first 4 controls to ComboBoxes
        ' and the rest to TextBoxes.
        '
       
                  sCtlType = "Textbox"
         
        ' Add a label for each new control added.
        '
        sCaption = MessageArray2(pathArray(i), 4) & ":" & CStr(x)
        Call Controls.Add("VB.Label", "Label" & CStr(iCount), Controls(sFrame))
        Load Label0(iCount)
        Set Label0(iCount).Container = Controls(sFrame)
        With Label0(iCount)
            .Visible = True
            .Width = 800 + width1
            .Top = 200 + (x * (height1 + 350))
            .Tag = .Top
            .Left = 100
            .Height = 195 + height1
            .Caption = sCaption
        End With
        '
       
        '
        Controls.Remove "Label" & CStr(iCount)
       
        sValue = ""
       
        Select Case sCtlType
            Case "Textbox"
                Call Controls.Add("VB.TextBox", "Text" & CStr(iCount), Controls(sFrame))
                Load Text0(iCount)
                Set Text0(iCount).Container = Controls(sFrame)
                With Text0(iCount)
                    .Visible = True
                   
                      If (pathArray(i) = getIndex(highlightedMID)) Then
                      .ForeColor = &HC0C000
                      End If
                     
                    .Width = 3035 + width1
                    .Top = 200 + (x * (height1 + 350))
                    '.Tag = .Top
                    .Tag = MessageArray2(pathArray(i), 1)
                    .Left = 1000
                    .Height = 315 + height1
                      .Text = MessageArray2(pathArray(i), 2)
                      ' to find out how to change the read only properties of the dynamic control
                     ' .Name = MessageArray2(x, 1)
                     
                     
                     
                End With
                Controls.Remove "Text" & CStr(iCount)
         
        End Select
        iCount = iCount + 1
        i = i - 1
    Next
    Controls(sFrame).Tag = x * 400

   
End Sub


Private Sub pScroll()
Dim sFrame As String
Dim x      As Integer

If bResetting Then Exit Sub
'
' Get the name of the currently displayed frame.
' Update its scrollbar value.
'
sFrame = "Frame" & CStr(TabStrip1.SelectedItem.index)
colScroll.Remove (sFrame)
colScroll.Add VScroll1.Value, sFrame
'
' Reposition the controls on the displayed frame.
'
For x = iOldControls To Controls.Count - 1
    If Controls(x).Container.Name = sFrame Then
        Controls(x).Top = Val(Controls(x).Tag) - (VScroll1 * 400)
    End If
Next
End Sub



thanks
animesh
Set x = Load(Text1(i))

check  if this works

but before you need to set index to 0 for textbox that was created initally
>> load text1(i)

it is working fne for me
hi sudhakar,

i created a zip file with the last code that you sent but which is not working on my machine. Maybe i am not putting the right objects on the form or am doing some silly mistake..can i send it to you so that you can see what is it that i am doing wrong.

also i created another small zip file that has a code to scroll the picture frame within another picture frame..(using one of the links that u had given me). but it is not working. can u please see what's wrong there too (right now scrolling is the secondary problem , primary problem is to delete the textboxes).

regarding your last message : "Set x = Load(Text1(i))
but before you need to set index to 0 for textbox that was created initally "

what  is x and how and where do i set index? can u send me the file that you have written?

my email address is sir_animesh at yahoo dot com
also let me know how can i send u those zipped files.

thanks
animesh
hi sudhakar,

please reply....
if you are not able to answer, please let me know. In that case, if you do not mind, can i open this question again..as then i will get more opiniions.

thanks a lot
regards,
animesh
Hi check the mail, i have uploaded the zip document

let me know if you have any problems
thanks a lot, sudhakar (for your time and efforts),


though i replied to you personally,,,,..here is the text of that mail....for others to know the problems that i faced  

i used your idea and now my code is working.
It worked. (however it was not working on the complex form that i was earlier using;which was not created by me..i was modifying a code that i found on net..

 old form has a tabstrip, a frame on that strip and the textboxes had to be printed on this frame. Also this frame was such that resizing the form lead to readjustment of all the control present on the frame....maybe that was the problem)
 
anyway now i am using a simplified form..and bottomline i was able to do what i wanted to do.
SO THE QUESTION IS CLOSED NOW.
 

There are certain other things that i want to do now..like putting scrolling function with the existing code that you have given me ..and also if possible having frame that adjusts the control positions if the form is resized...will post those as seperate questions.

ALSO CHECK this link : https://www.experts-exchange.com/questions/20808716/how-to-clear-dynamic-textboxes-placed-on-the-screen-and-how-to-scroll-down-if-all-text-boxes-cannot-be-displayed.html

 regards,
animesh