473,394 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Saving RichTextBox from Tab Control

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??

Mar 20 '06 #1
8 3245
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.

Mar 20 '06 #2
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 :)

Mar 21 '06 #3
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.

Mar 21 '06 #4
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 ;)

Mar 21 '06 #5
> 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.

Mar 21 '06 #6
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.FileNa me,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!

Mar 25 '06 #7
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.

Mar 25 '06 #8
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/miscct...redtextbox.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.

Mar 27 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: gogaz | last post by:
Hello, I am facing one strange problem. I am using RichTextBox control in my VB6.0 application and have provided one button giving functionality to set bullets on the selected text. When i type...
1
by: vanvee | last post by:
Hi I have a user control that contains a RichTextBox in vb.net. In my program, I create multiple instances of this control (with the RichTextBox being in each one), that appear one above the...
0
by: AxOn | last post by:
Hi! I'm having trouble creating a save funktion. How can I save the contens of a new richTextBox that is created from an "external" class file? The open funktion looks like this: public void...
3
by: AxOn | last post by:
Hi, I have an application with a Save function. I want to be able to have several tabs(each containing a RichTextBox, created at runtime) open in a tabControl and save the text from each...
9
by: James Wong | last post by:
Hi, I use the RichTextBox in my program. It will use different language in this RichTextBox (chinese and english characters), and it set the "DualFont" and use different fonts. By the way, how...
3
by: michael sorens | last post by:
The documentation for the RichTextBox is sketchy at best. I want to do a very simple task but I cannot find information on this. I am using a RichTextBox as an output window. Some text I want to...
0
Ajm113
by: Ajm113 | last post by:
Contributing To: Saving RichTextBox from Tab Control Ok, I have a question what would I use if I wanted to do something like a paste function in a Rich Text Box using the code on that thread?...
4
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, I have a multiline RichTextBox that I use for data logging. I'm concerned about how the AppendText method reacts when over time the maximum number of characters have been added. Do the...
1
by: =?Utf-8?B?TWFya19C?= | last post by:
I have an unpredictable number of RichTestBoxes, stored in a generic list, that are created at runtime. Currently, I am saving these controls to a file by saving each attribute of each box in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.