المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Need some help with active MDI in a tabcontrol



C# Programming
03-28-2009, 05:12 AM
I'm having some trouble getting control on the active mdi child of a richtextbox that is inside of a tab control. All the common text editor functions work in the first richtextbox. Like a right click contextmenu, toolbar functions, font etc. Its name is RichTextBox. I have a treeview control that I added a doubleclick event that creates a new mdi tabpage in the existing tabcontrol and in that tabpage adds a new mdichild richtextbox containing the text of the file that was clicked in the double click event. The tabpage and the richtextbox are named the name of the file + 1 to the count of the current number of tabs. I also have some buttons on the main form that will open specific text files in the original richtextbox (this is easy enough since I have a static richtextbox with a name I can point to) What Im having trouble with is getting control of the currently active richtextbox. this.ActiveMdiChild does not seem to work. I would like to eliminate the static richtextbox and change all the controls in the mainform to point to the active mdichild instead. I think part of my problem maybe that the tabpage is also called as the active mdichild in order to handle my close events(this was recommended in order to have easier control in making them the active) i have put on the tab. I tried it with a different name but that didn't seem to help either. An example of one of the ways I have tried to get control is from the msdn forums. All i get


// Determine the active child form.
Form activeChild = this.ActiveMdiChild;

// If there is an active child form, find the active control, which
// in this example should be a RichTextBox.
if (activeChild != null)
{
try
{
RichTextBox theBox = (RichTextBox)activeChild.ActiveControl;
if (theBox != null)
{
// Put the selected text on the Clipboard.
Clipboard.SetDataObject(theBox.SelectedText);

}
}
catch
{
MessageBox.Show("You need to select a RichTextBox.");
}


Th code Im using to create the new tabs with the richtextbox


System.IO.StreamReader StreamReader1 = fi.OpenText(); //reads the currently selected text file
String text = StreamReader1.ReadToEnd();
TextEditorForm editForm = new TextEditorForm(); // creates a new instance of the mdi child form editform
TabPage childTab = new TabPage(); //create new tab page
editForm.MdiParent = this; //set as child of this form
editForm.Name = "Child" + createdTab.ToString(); //sets the name of editform to child + 1
editForm.Text = lblFile.Text; //sets the tabpage text to the file name lblfile.text is the file that is currently in the listbox
childTab.Name = editForm.Name; //make sure name and text are same for easier control
childTab.Text = editForm.Text; //this is for syncrhonize later
TextEditortabControl.TabPages.Add(childTab); //add new tab
editForm.richTxtBox.Parent = childTab; //attach to tab
editForm.richTxtBox.Name = editForm.Name; // naming the rtb the same as the rest for easier control
editForm.richTxtBox.ContextMenu = new ContextMenu(); // does not add the contextmenu
ToolStripMenuItem newtexteditMenuTab = new ToolStripMenuItem(); //create menu to hold tab
newtexteditMenuTab.Text = editForm.Text; //make sure the name and text are same to synchonize later
newtexteditMenuTab.Name = editForm.Name; // still the same name
newtexteditMenuTab.Click += new EventHandler(newMenuTab_Click); //add event handler
TextEditortabControl.SelectTab(childTab); //this is to make sure that tab page is selected in the same time
editForm.EditText = text; //populates the rtb with the selected text file text
editForm.Show(); //as new form created so that corresponding tab and child form is active
createdTab++; //increment of course
StreamReader1.Close();


the TextEditorForm


public TextEditorForm()
{
this.richTxtBox = new RichTextBox();
this.richTxtBox.Dock = DockStyle.Fill;
this.richTxtBox.Multiline = true;
this.richTxtBox.ScrollBars = RichTextBoxScrollBars.Vertical;
this.richTxtBox.WordWrap = true;
this.richTxtBox.DetectUrls = true;
this.richTxtBox.Visible = true;
//this.ContextMenu = new ContextMenu(); this didnt work either



// Add the RichTextBox to the Form
this.Controls.Add(this.richTxtBox);

}



I know if I can just get 1 control working on the active mdi child the rest will follow easy enough.
This seems like it should work and I'm been stuck on this for a few days now scratching my head and cant figure out what I'm doing wrong maybe more eyes will help. This is my first c# application and I have only been at it for a month or so now so go easy on me lol http://www.barakasoft.com/script/Forums/Images/smiley_smile.gif