473,574 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3259
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.Collecti ons;

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 _richTextBoxToF ileName;

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

this._richTextB oxToFileName = 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._richTextB oxToFileName[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._r ichTextBoxToFil eName[richTextBoxBein gSaved.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 NumberedTextBox UC 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 .InitialDirecto ry = Application.Sta rtupPath
+ @"\Folder";
openFileDialog1 .Filter = "All files (*.*)|*.*";

// Show the Dialog.
// If the user clicked OK in the dialog and
// a file was selected, open it.
if (openFileDialog 1.ShowDialog() == DialogResult.OK )
{
StreamReader sr = new
StreamReader(op enFileDialog1.F ileName);

string title =
Path.GetFileNam e(openFileDialo g1.FileName);
TabPage myTabPage = new TabPage(title);
tabControl1.Tab Pages.Add(myTab Page);

int index = tabControl1.Tab Pages.IndexOf(m yTabPage);

boxen = new NumberedTextBox UC();
boxen.Dock = DockStyle.Top;
this._richTextB oxToFileName[this.boxen.rich TextBox1.Name] = title;
boxen.richTextB ox1.LoadFile(op enFileDialog1.F ileName,RichTex tBoxStreamType. PlainText);

tabControl1.Tab Pages[index].Controls.Add(b oxen);
}
}

//And the save function

private void saveFile()
{
SaveFileDialog saveFile1 = new SaveFileDialog( );
saveFile1.Initi alDirectory = Application.Sta rtupPath +
@"\Folder";

String fileNameToSave
=(string)this._ richTextBoxToFi leName[boxen.richTextB ox1.Name];

saveFile1.FileN ame = fileNameToSave;
saveFile1.Defau ltExt = "*.txt";
saveFile1.Filte r = "Text (*.txt)|*.txt";

if (saveFile1.Show Dialog() ==
System.Windows. Forms.DialogRes ult.OK &&
saveFile1.FileN ame.Length > 0)
{
boxen.richTextB ox1.SaveFile(sa veFile1.FileNam e,
RichTextBoxStre amType.PlainTex t);

}
}

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.GetFileNam e(openFileDialo g1.FileName);
TabPage myTabPage = new TabPage(title);
myTabPage.Name = "Page" + this._pageCount .ToString();
this._pageCount += 1;
tabControl1.Tab Pages.Add(myTab Page);

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._richTextB oxToFileName[myTabPage.Name] =
openFileDialog1 .FileName;

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

string fileNameToSave =
(string)this._r ichTextBoxToFil eName[tabPage.Name];
SaveFileDialog saveFile1 = new SaveFileDialog( );
saveFile1.FileN ame = Path.GetFileNam e(fileNameToSav e);

saveFile1.Initi alDirectory = Application.Sta rtupPath +
@"\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 NumberedTextBox UC 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
1889
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 and set bullets on some text and save the document, it is displaying everything ok. But when i open the saved file again I notice some small dots...
1
2571
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 other on a panel. The problem is that each control may have varying amounts of data, and so each RichTextBox (within each control) needs to size to...
0
2380
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 openFile() { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Title = "Select a Text File";
3
2177
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 RichTextBox one at a time. The plain text is loaded into the RichTextBoxes via an openFile funktion that creates a new RichTextBox. When the file is opened...
9
4850
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 can I print out this content to printer easily? Thanks! James
3
7166
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 just add plain (e.g.. rtf.Text += "new stuff..."; ) but some text, when added, I want to be in a different color or font size. Find() will not...
0
1387
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? Thanks, Ajm113.
4
13422
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 oldest characters it contains start rolling off the top into the bit bucket, does it simply start silently ignoring appends until some characters,...
1
1254
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 string form using a streamwriter. Later, these attributes are read from the file with a streamreader and the controls are recreated. It seems like a...
0
7720
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8044
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7810
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8096
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6451
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5618
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5299
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3739
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2240
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.