Saving RichTextBox from Tab Control

A

AxOn

Hi,

How can i save the specific RichTextBox in the selected tab if i have
several tabs with RichTextBoxes in? The application is a text editor
type program and when i try to save the most recently opened text file
in the rich text box my application saves that file over the other
files?? Can i some how index the new RichTextBoxes so that only the
text in the selected RichTextBox is saved??
 
B

Bruce Wood

You need to give more details about how you've build your program. You
say, "...when I try to save..." but where is the code that does the
save? Do you have a Save button or menu choice?

Are you trying to save the contents of a single RichTextBox into a
single file with that Save operation, or do you want to save everything
that changed back into their respective files?

How do you remember which RichTextBox corresponds to which file name?
What data structure do you use in your program to remember that?

Answering these questions would be a start toward helping us understand
what's going on in your program and helping you fix it.
 
A

AxOn

Ok, I have a Save button and a menu choice that use the same function
it. I want to be able to have several tabs i a tabControl and save the
text from each RichTextBox one at the time. The plain text i loaded
into the RichTextBoxes via an openFile funktion that creates a new
RichTextBox. The problem is that my program doesn't remember which
tab(RichTextBox) corresponds with which file.
I hope this helps you to help me :)
 
B

Bruce Wood

Well, your first order of business is to remember which text box
corresponds to which file. There are lots of ways to do this, but what
is probably the easiest way is to use a Hashtable.

I don't know what version of .NET you're using. I'm still on 1.1, so I
apologize in advance if there's a cleaner way to do this in 2.0 using
generics. That said, here is a simple hashtable solution.

Add a using statement to your .cs file for collections:

using System.Collections;

At the top of your Form class definition, where the class members for
the controls are defined, add a new definition for a hash table:

private Hashtable _richTextBoxToFileName;

In your constructor, after the InitializeComponents() call, add a line
to create the hash table:

this._richTextBoxToFileName = new Hashtable();

Now, as you create each RichTextBox by reading a file, just store the
file name in the hash table, using the name of the rich text box as a
key. Every control has a Name property, and every Name on a form has to
be unique, so this will work fine. Let's say that you have a reference
to the new rich text box you added in the variable richBox, and that
you have the name of the file that you loaded in the variable fileName.
Just after you load the text box with the file contents (or before, it
doesn't really matter), do this:

this._richTextBoxToFileName[richBox.Name] = fileName;

Now, any time you have a rich text box and need to know which file to
save to, just do it like this:

string fileNameToSave =
(string)this._richTextBoxToFileName[richTextBoxBeingSaved.Name];

which will get you back the name of the file corresponding to the rich
text box.

There are, of course, other more sophisticated schemes, but this one
has the advantage of being easy to understand.

Hope this helps.
 
A

AxOn

Thanks for the help.
But I can't get it to work if I have several tabs with one RichTextBox
in each. The fileNameToSave is always the name of the most recently
opened RichTextBox. So, if I try to save the RichTextBox form the first
tab I end up saving the contens of the most recently opened
RichTextBox.
Thank again, I really hope you can help me with this problem, and yes
I'm new at this ;)
 
B

Bruce Wood

The fileNameToSave is always the name of the most recently opened RichTextBox.

Well, yes, but that's rather the point.

What I'm saying is that if you save away each file name as you open
each new text box, you will then build a table that remembers which
file is in which text box.

Why don't you post some code? That way I have something to work from.
 
A

AxOn

ok, here is some of my code. The NumberedTextBoxUC is a user controll
which contains the rich text box. I want to be able to have several
tabs open containing this user controll, and load text files into the
user controll's rich text boxes and save the files one at a time.

//The open function

public void openFile()
{

OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = Application.StartupPath
+ @"\Folder";
openFileDialog1.Filter = "All files (*.*)|*.*";

// Show the Dialog.
// If the user clicked OK in the dialog and
// a file was selected, open it.
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
StreamReader sr = new
StreamReader(openFileDialog1.FileName);

string title =
Path.GetFileName(openFileDialog1.FileName);
TabPage myTabPage = new TabPage(title);
tabControl1.TabPages.Add(myTabPage);

int index = tabControl1.TabPages.IndexOf(myTabPage);

boxen = new NumberedTextBoxUC();
boxen.Dock = DockStyle.Top;


this._richTextBoxToFileName[this.boxen.richTextBox1.Name] = title;


boxen.richTextBox1.LoadFile(openFileDialog1.FileName,RichTextBoxStreamType.PlainText);

tabControl1.TabPages[index].Controls.Add(boxen);
}
}

//And the save function

private void saveFile()
{


SaveFileDialog saveFile1 = new SaveFileDialog();
saveFile1.InitialDirectory = Application.StartupPath +
@"\Folder";

String fileNameToSave
=(string)this._richTextBoxToFileName[boxen.richTextBox1.Name];

saveFile1.FileName = fileNameToSave;
saveFile1.DefaultExt = "*.txt";
saveFile1.Filter = "Text (*.txt)|*.txt";

if (saveFile1.ShowDialog() ==
System.Windows.Forms.DialogResult.OK &&
saveFile1.FileName.Length > 0)
{
boxen.richTextBox1.SaveFile(saveFile1.FileName,
RichTextBoxStreamType.PlainText);

}


}

So what am I doing wrong, thanks!
 
B

Bruce Wood

Try making the following adjustments.

First, you need some way to uniquely name tab pages. At first I thought
of using the file name (the "title"), but this brings with it the
problem of "what if you open two files of the same name?" The very
surest way to have every page have a unique name is to create a counter
in your class:

private int _pageCount = 0;

Then use it to create the tab page name:

string title =
Path.GetFileName(openFileDialog1.FileName);
TabPage myTabPage = new TabPage(title);
myTabPage.Name = "Page" + this._pageCount.ToString();
this._pageCount += 1;
tabControl1.TabPages.Add(myTabPage);

Now, when you add the file name to the hash table, I would add the full
file name (not just the name part), and use the page name as the key:

this._richTextBoxToFileName[myTabPage.Name] =
openFileDialog1.FileName;

Now, when you want to get the full file path back, use the tab page
name:

string fileNameToSave =
(string)this._richTextBoxToFileName[tabPage.Name];
SaveFileDialog saveFile1 = new SaveFileDialog();
saveFile1.FileName = Path.GetFileName(fileNameToSave);

saveFile1.InitialDirectory = Application.StartupPath +
@"\Folder"; // (Or use the path from fileNameToSave)

Of course, you could set the Name property of the user control instead
of the tab page, if it's more convenient to get the user control name
when you're doing the save. Or you set and use the text box name. It
doesn't really matter, so long as the name of every control is unique,
and you use the same name to retrieve the file name as you used to
store it.
 
A

AxOn

I think the problem is the communication between my app and the user
control. I'm using a user control posted at
http://www.codeproject.com/cs/miscctrl/numberedtextbox.asp I just
added the control into my app's tabControl, and everytime I open a new
file i
"create" a new NumberedTextBoxUC which contains the rich text box and
load the file into it . This user control is perfect for me, if i only
could save the text. I would really appreciate it if you can give me
any advice on how to do this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top