• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 436
  • Last Modified:

Tracking the text in dynamically generated text box (also how to modify such boxes)

Hi,

My objective is to display (say n) number of text messages. when user clicks on a message then i should be able to access the related information to that text.
the text messages and related info is stored in a 2 dimentional array and n (and text message array )will keep on changing in runtime.

My approach: I am creating n number of dynamic text objects and printing the messages in sequential order in those boxes. When n changes, i recreate all the text objects (with text coming from the array.)

i also capture mouse click on a text box. however now i need to know what is the index number of this test message in the array so as to access/display other related info.

NOW THE PROBLEM IS : that all the dynamically generated boxes have the same name (the name i give to the object that i include on the form). I cannot assign some ID (i.e. array index no) to each new box' name as it is read only property and so cannot identify the which array position the text belongs to.

I think that one workaround would be to use rich text; include all related info in the text but do not show it (change font color) and when user clicks on a box then read the text in the box, then seperate and display the related info. this is not the best way but if there is no other way then i will have to use it.

ANOTHER RELATED QUESTION: (or should i post is a new question)?
I want the text box height to adjust according to the content. Right now i am setting the height to a specific number. (while i was typing the question i realized that i might calculate the length of the text and then calculate the height to be set dynamically...will try that tomorrow) Is there a method to do it in VB.

Thanks a lot
Animesh
0
aanimesh
Asked:
aanimesh
  • 6
  • 6
  • 4
4 Solutions
 
sudhakar_koundinyaCommented:
there are two ways to create the controls dynamically

i think you are creating them using load method

go through this URL http://www.vb-helper.com/howto_add_controls_scrolled.html

this will help your problem at maximum

instead of creating index for a control it treats controls some thing like text1,text2,text3 and so on

0
 
sudhakar_koundinyaCommented:
Private Sub Command2_Click()
On Error Resume Next
Dim index As Integer

 Dim ctl As TextBox
Dim prev As TextBox


    m_NumControls = m_NumControls + 1
    Set ctl = Controls.Add("VB.TextBox", "Text" & Format$(m_NumControls), Form1)


    Set prev = Controls("Text" & Format$(m_NumControls - 1))
    ctl.Move prev.Left, _
        prev.Top + prev.Height + 30, _
        prev.Width, prev.Height
    ctl.Text = ctl.Name


    Form1.Move 0, 0, _
        ctl.Left + ctl.Width + 320, _
        ctl.Top + ctl.Height + 320


    ctl.Visible = True


End Sub
0
 
sudhakar_koundinyaCommented:
text height+textbox height and may be some extra stuff helps you to do your second task

reagrds
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ashooooCommented:
Create a text box called txtDynamic with an index 0 on your form and make it invisible.

Use this code to dynamically create more text boxes on the fly.

  For i = 1 to arrayCount
    Load txtDynamic(i)
    txtDynamic(i).visible = True
    ...
  Next i

You can get the index number of hte text box on any event using the Index parameter passed to you. for example, on the click event...


Private Sub txtDynamic_Click(ByVal Index as Integer)
  ' the Index tells you the index of the text box
End Sub

Hope this helps
0
 
aanimeshAuthor Commented:
Hi Sudhakar,

I tried what you suggested. I can assign unique names to each textbox that appears on the form but here is a problem . In this case I cannot write a "text click code" for each of those blocks. (which i am doing now using  Private Sub Text0_Click(Index As Integer)  

my requirement is  to capture which textbox a user has clicked and also execute some code when he does that (this code needs to know the name of the text box in order to know what is displayed in that test box and present some related info ---which can be done using the technique u showed me but in which block do i write the code that will execute when a text box with such unique names is clicked.

am i making sense?
 let me know if it is possible?
0
 
aanimeshAuthor Commented:

Hi Ashooo.....

i am doing what you told but then i do not want the index of the control, i want the identifier that i will give when i assign the text to a textbox (as the index will keep on changing but the identifier that i give will not). Sudhakar has understood what i am doing.
0
 
ashooooCommented:
You can store the identifier (assuming its a string) in the Tag.
0
 
aanimeshAuthor Commented:
hi ashoo...

let me try that....if that cab be done...then that wouuld be greaaat...
thanks a lot man..
will get back to you soon


animesh
0
 
aanimeshAuthor Commented:
hi ashoo...
thanks a lot boss..it worked....really appreciate it....
but can i give some points to sudhakar too...as he also had given an alternate way of doing what i wanted to do....you decide....

thanks a lot once again....
0
 
ashooooCommented:
You can split points between us anyway you like.  I'm glad I was of help.
0
 
sudhakar_koundinyaCommented:
this is upto you friend, any thing is accepted by me :-)

0
 
aanimeshAuthor Commented:
hi sudhakar,

i could not find an option to split points....
but don't worry i will give you another opportunity very soon :-)


all the best guys....thanks a lot once again,
animesh
0
 
ashooooCommented:
Thanks aani, but I think there is an option somewhere to split the points. If you want, you can ask the moderator to split the points for you.

Thanks again.
0
 
sudhakar_koundinyaCommented:
ok no problem :-)

why don't you ask moderators for help, if possible
0
 
aanimeshAuthor Commented:
thanks annieMod.

by the way guys i have another question regarding the same objective.
i am posting a question seperately ..if you guys can help that would be great , especially now that you understand what i am trying to do...

regards,
animesh
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now