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
Re: Associate TextBox with Tab?
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.
Re: Associate TextBox with Tab?
try this:
vb Code:
DirectCast(Me.SplitContainer1.ActiveControl, TextBox).Paste()
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:
if typeof Me.SplitContainer1.ActiveControl is textbox then
'your code
end if
Re: Associate TextBox with Tab?
Wow, that worked, thanks!
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?
Re: Associate TextBox with Tab?
to refer to the textbox in code:
vb Code:
directcast(me.controls(PathToFile & "Box"), textbox)
Re: Associate TextBox with Tab?
Re: Associate TextBox with Tab?
you might need to use:
vb Code:
me.SplitContainer1.controls
instead of:
Re: Associate TextBox with Tab?
EH. I tried to run the following:
vb Code:
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.
Re: Associate TextBox with Tab?
Yeah, Me.SplitContainer1.Controls didn't work either. NullReferenceException.
Re: Associate TextBox with Tab?
whats the exact name of the textbox?
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:
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).
Re: Associate TextBox with Tab?
i experimented with it. this works:
vb Code:
Dim tb As New TextBox
tb.Name = "C:\MyApp\Projects\MyProject\File.txtBox"
tb.Text = "test"
Me.SplitContainer1.Panel1.Controls.Add(tb)
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
Re: Associate TextBox with Tab?
OH!
I had it set wrong:
vb Code:
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:
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.