I'm new at C# and I created a simple text editor. I figured out have to load files in different formats such as txt and rtf and save content as well. Now I'm facing a new problem. searched around and didn't found solution. Under ToolStripMenuItem I have a link that supposed call for a new blank document. currently I user this.clear(); but it's not the way how it should be done. now I wonder how to call a new empty RichTextBox and when user have a unsaved entry how to check and alert user for saving entries.
here is the code:
private void ????????ToolStripMenuItem_Click_1(object sender, EventArgs e)
{
richTextBox1.Clear();
}
then is there a way to use a separated language file for localization purpose and how should I do or where should I look for solving these issues.
Thanks in advance.
FroglegPosted Mar 29, 2011, 5:58 PM
If you open a file in form1, and then open another file in form1 you get an alert ?
Maybe upload your code
Rzgar EspoPosted Mar 29, 2011, 5:18 PM
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void toolStripContainer1_ContentPanel_Load(object sender, EventArgs e)
{
}
private void Save_As_mToolStripMenuItem_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(richTextBox1.Text))
{
MessageBox.Show("Empty Form. Nothing to save");
}else {
SaveFileDialog sfd = new SaveFileDialog();
// open savefile dialog
sfd.DefaultExt = "*.rtf";
sfd.Filter = " RTF Files | *.rtf";
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
sfd.FileName.Length > 0)
{
richTextBox1.SaveFile(sfd.FileName, RichTextBoxStreamType.RichText);
richTextBox1.Clear();
}
}
}
private void Exit_mToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void Open_File_mToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Richtext Files (*.rtf;*.txt)|*.rtf;*.txt" ;
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string ext = System.IO.Path.GetExtension(ofd.FileName);
if (ext == ".rtf")
{
richTextBox1.LoadFile(ofd.FileName);
}
else if (ext == ".txt")
{
richTextBox1.Text = System.IO.File.ReadAllText(ofd.FileName);
}
}
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
richTextBox1.SelectionFont = new Font("Tahoma", 12);
}
private void New_File_mToolStripMenuItem_Click_1(object sender, EventArgs e)
{
if (richTextBox1.TextLength > 0)
{
MessageBox.Show("Please save your entry first");
}
else {
richTextBox1.Clear();
}
}
}
}
FroglegPosted Mar 29, 2011, 4:42 PM
if (string.IsNullOrEmpty(richTextBox1.Text)
{
// maybe a messagebox or whatever
return:
}
Rzgar EspoPosted Mar 29, 2011, 4:08 PM
here is what I done so far:
I wanted to create an Alert box with Save and cancel button after checking unsaved entries. I guess as you mentioned above, I've to create new form and link buttons and user choice to first form.(I'll try out you advice)
here is the code:
private void New_File_mToolStripMenuItem_Click_1(object sender, EventArgs e)
{
if (richTextBox1.TextLength > 0)
{
MessageBox.Show("Please save your entry first");
}
else {
richTextBox1.Clear();
}
and here is Save as.. code, it will check the file length and if it's not empty then it try to save it. but if user want to save an empty file before typing any note It will make a conflict with above code(New_File_m):
private void Save_AsToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
// open savefile dialog
sfd.DefaultExt = "*.rtf";
sfd.Filter = " RTF Files | *.rtf";
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
sfd.FileName.Length > 0)
{
richTextBox1.SaveFile(sfd.FileName, RichTextBoxStreamType.RichText);
//How can I control if SaveFile got through? if user press cancel then
//automatically richTextBox1.Clear will clear the form without saving.
richTextBox1.Clear();
}
}
FroglegPosted Mar 29, 2011, 2:37 PM
In the new button click put
Form2 Frm2 = new Form2();
Frm2.Show();
This will work on Form2 also - you can have multiple form2 open
localization
http://www.codeproject.com/KB/dotnet/Localization.aspx