Results 1 to 16 of 16

Thread: Associate TextBox with Tab?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2008
    Posts
    16

    Associate TextBox with Tab?

    Hello,

    I am making a program with tabs, and in each tab is a textbox for editing a file (like how VB's designer does). How can I match up a textbox with a tab so that the cut, copy, paste, select all, etc. in the menu can tell what textbox to preform actions on? I was thinking of getting the current selected tab, and somehow finding the textbox associated with that tab.

    Right now I dynamically create a new textbox when a new tab is opened.

    Any ideas? Thanks in advance.
    -Nick

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Associate TextBox with Tab?

    use me.activecontrol

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2008
    Posts
    16

    Re: Associate TextBox with Tab?

    Hmm that didn't work. My controls are inside of a SplitContainer and it selects that instead of the textbox.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Associate TextBox with Tab?

    try this:

    vb Code:
    1. DirectCast(Me.SplitContainer1.ActiveControl, TextBox).Paste()

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Associate TextBox with Tab?

    if you have other controls in your splitcontainer that aren't textboxes, you'll need to test if its the textbox that has the focus:

    vb Code:
    1. if typeof Me.SplitContainer1.ActiveControl is textbox then
    2.    'your code
    3. end if

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Aug 2008
    Posts
    16

    Re: Associate TextBox with Tab?

    Wow, that worked, thanks!

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Aug 2008
    Posts
    16

    Re: Associate TextBox with Tab?

    Well, what if the textbox inside that tab isn't the active control? I still need to find out what textbox is inside the selected tab.

    When I create the tabs, the Name property on the created textbox is set to PathToFile + "Box" so I can predict what the name of the textbox is, but I can't use it to select the textbox in my code (it formats it as a string, not a control). Any way around this?

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Associate TextBox with Tab?

    to refer to the textbox in code:

    vb Code:
    1. directcast(me.controls(PathToFile & "Box"), textbox)

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Aug 2008
    Posts
    16

    Re: Associate TextBox with Tab?

    Ah, thanks.

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Associate TextBox with Tab?

    you might need to use:

    vb Code:
    1. me.SplitContainer1.controls

    instead of:

    vb Code:
    1. me.controls

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Aug 2008
    Posts
    16

    Re: Associate TextBox with Tab?

    EH. I tried to run the following:

    vb Code:
    1. MsgBox(DirectCast(Me.Controls(projectPath & "\" & Me.TabControl1.SelectedTab.Text & "Box"), RichTextBox).Text)

    ..and got a NullReferenceException.

    Why?

    EDIT: Lol, didn't see your previous post, I'll try that.

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Aug 2008
    Posts
    16

    Re: Associate TextBox with Tab?

    Yeah, Me.SplitContainer1.Controls didn't work either. NullReferenceException.

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Associate TextBox with Tab?

    whats the exact name of the textbox?

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Aug 2008
    Posts
    16

    Re: Associate TextBox with Tab?

    Let me explain how it works.

    When you open a project, it lists the directory structure of the project to the left of the tabs in a TreeView. When you double-click a file in the TreeView, it opens a new tab in TabControl1 and dynamically creates a textbox in the new tab (and loads in the file's contents). The textbox's Name property is set to the following:

    vb Code:
    1. textbox.Name = projectPath + "\" + TreeView1.SelectedNode.Text + "Box"

    The projectPath is not null, because it is set when a project is opened. As you can see, it gets the project path and adds the selected node in the TreeView's text, and finally adds on "Box" to the end. The Name property ends up looking like this:

    C:\MyApp\Projects\MyProject\File.txtBox

    I use the full path, and not just the filename, in case there are multiple files with the same name (in different folders).

  15. #15
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Associate TextBox with Tab?

    i experimented with it. this works:

    vb Code:
    1. Dim tb As New TextBox
    2. tb.Name = "C:\MyApp\Projects\MyProject\File.txtBox"
    3. tb.Text = "test"
    4. Me.SplitContainer1.Panel1.Controls.Add(tb)
    5. MsgBox(DirectCast(Me.SplitContainer1.Panel1.Controls("C:\MyApp\Projects\MyProject\File.txtBox"), TextBox).Text)

    if something similar to this doesn't work for you, you must have the textbox name wrong

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Aug 2008
    Posts
    16

    Re: Associate TextBox with Tab?

    OH!

    I had it set wrong:

    vb Code:
    1. DirectCast(Me.SplitContainer1.Controls(projectPath & "\" & Me.TabControl1.SelectedTab.Text & "Box"), RichTextBox).Text

    When in reality, I make the controls under TabControl1, inside of a TabPage, so this would work:

    vb Code:
    1. DirectCast(Me.TabControl1.TabPages.Item(Me.TabControl1.SelectedTab.Text).Controls(projectPath & "\" & Me.TabControl1.SelectedTab.Text & "Box"), RichTextBox).Text

    ...and it did.

    Thanks for all the help, paul, really appreciate it.

Tags for this Thread

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