Solved

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

Posted on 2003-11-20
16
567 Views
Last Modified: 2010-05-03
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

0
Comment
Question by:aanimesh
  • 9
  • 7
16 Comments
 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility
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
0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 250 total points
Comment Utility
Ignore the proevious one


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 = 1

For i = 1 To prevcount
Dim str As String
str = "Text" & i

Call Controls.remove(str)

Next
End Sub

Private Sub Form_Load()
prevcount = 0
End Sub





0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility
As i told you in earlier discussion

follow the steps of these links, helps you

http://www.vb-helper.com/howto_scrolled_window.html
http://www.vb-helper.com/howto_add_controls_scrolled.html



0
 

Author Comment

by:aanimesh
Comment Utility
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

0
 

Author Comment

by:aanimesh
Comment Utility
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
0
 

Author Comment

by:aanimesh
Comment Utility
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
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility
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
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility
you can create the environment as suggested by ashoo
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility
>> actually i had a piece of code that i am using but it behaves very erratically

is it possible to see your code
0
 

Author Comment

by:aanimesh
Comment Utility
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
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility
Set x = Load(Text1(i))

check  if this works

but before you need to set index to 0 for textbox that was created initally
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility
>> load text1(i)

it is working fne for me
0
 

Author Comment

by:aanimesh
Comment Utility
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
0
 

Author Comment

by:aanimesh
Comment Utility
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
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility
Hi check the mail, i have uploaded the zip document

let me know if you have any problems
0
 

Author Comment

by:aanimesh
Comment Utility
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 : http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/VB_Controls/Q_20808716.html

 regards,
animesh




0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now