Tekst editor

  • Onderwerp starter Onderwerp starter Scoox
  • Startdatum Startdatum
Status
Niet open voor verdere reacties.

Scoox

Gebruiker
Lid geworden
22 jan 2013
Berichten
100
Hallo, Voor mijn stage moet ik een simpele tekst editor in elkaar zetten.
het enigste wat is tot nu toe kan is:

  • Opslaan
  • Laden
  • Clearen
Maar nu wil ik ook dat ik de tekst Bold en zo kan maken.
Kan iemand mij hier mee helpen

Dit is mijn code:

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using Novacode;
using System.Diagnostics;
using System.Drawing.FontStyle;

namespace Data_Verplaatsen_v1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            FormBorderStyle = FormBorderStyle.FixedDialog;
        }
        public void btn_save_Click(object sender, EventArgs e)
        {
            string pat = Convert.ToString(txt_zoeken.Text);
            if (txt_zoeken.Text == "")
            {
                MessageBox.Show("Missing file name!", "File name ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (txt_zoeken.Text == pat)
            {
                int i;
                pb_loading.Value = 0;
                pb_loading.Maximum = 100;
                for (i = 0; i <= 100; i++)
                {
                    pb_loading.Value = i;
                    using (StreamWriter writer = new StreamWriter("C:\\Users\\ExitReizen\\Documents\\" + pat + ".doc"))
                    {
                        writer.Write(mtxt_schrijfen.Text);
                    }
                }
            }
        }

        private void btn_load_Click(object sender, EventArgs e)
        {
            pb_loading.Visible = true;
            string pat = Convert.ToString(txt_zoeken.Text);
            string sourcepath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string[] words = new string[3];

            
            if (txt_zoeken.Text == "")
            {
                MessageBox.Show("Missing file name!", "File name ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (txt_zoeken.Text == pat)
            {
                int i;
                pb_loading.Value = 0;
                pb_loading.Maximum = 100;
                for (i = 0; i <= 100; i++)
                {
                    pb_loading.Value = i;
                string loadd = File.ReadAllText("C:\\Users\\ExitReizen\\Documents\\" + pat +".doc");
                mtxt_schrijfen.Text = (loadd);
                      }
            }

        }

        private void btn_clear_Click(object sender, EventArgs e)
        {
            mtxt_schrijfen.Clear();
            txt_zoeken.Clear();
        }

        private void Cboc_bold_CheckedChanged(object sender, EventArgs e)
        {
            if (Cboc_bold.Checked)
            {
                mtxt_schrijfen.Font.Bold = true;
            }
        }
    }

Untitled.jpg
 
Laatst bewerkt:
Heb het gevonden, nu is mijn probleem alleen nog hoe sla ik de ge edite text (bold text) op?

dit was de oplossing trouwens:

[CPP] private void btn_BOLD_Click(object sender, EventArgs e)
{
if (mtxt_schrijfen.SelectionFont != null)
{
System.Drawing.Font currentFont = mtxt_schrijfen.SelectionFont;
System.Drawing.FontStyle newFontStyle;

if (mtxt_schrijfen.SelectionFont.Bold == true)
{
newFontStyle = FontStyle.Regular;
}
else
{
newFontStyle = FontStyle.Bold;
}

mtxt_schrijfen.SelectionFont = new Font(
currentFont.FontFamily,
currentFont.Size,
newFontStyle
);
}
}[/CPP]
 
Zo ver is het nu

Hallo, dit is mijn text editor tot nu toe, ff klijne update:

[CPP]namespace Data_Verplaatsen_v1
{
public partial class Form1 : Form
{
Font myFont = new Font("Microsoft Sans Serif", 12, FontStyle.Italic);
public Form1()
{
InitializeComponent();
FormBorderStyle = FormBorderStyle.FixedDialog;
MessageBox.Show("Hover over the buttons to see what they do! \nPress 'OK' to continue. \nBold texts are not able to be saved wil be updated soon!", "TIP!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
this.KeyPreview = true;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Modifiers == Keys.Control && e.KeyCode == Keys.B)
{
mtxt_schrijfen.SelectionFont = myFont;
}
}
public void btn_save_Click(object sender, EventArgs e)
{
int i;

pb_loading.Minimum = 0;
pb_loading.Maximum = 200;

SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt";

if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
&& saveFileDialog1.FileName.Length > 0)
{
mtxt_schrijfen.SaveFile(saveFileDialog1.FileName,
RichTextBoxStreamType.PlainText);
}
for (i = 0; i <= 200; i++)
{
pb_loading.Value = i;
}
}

private void btn_load_Click(object sender, EventArgs e)
{
int i;

pb_loading.Minimum = 0;
pb_loading.Maximum = 200;

OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "txt files (*.txt)|*.txt";

if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
&& openFileDialog1.FileName.Length > 0)
{
mtxt_schrijfen.LoadFile(openFileDialog1.FileName,
RichTextBoxStreamType.PlainText);
}
for (i = 0; i <= 200; i++)
{
pb_loading.Value = i;
}
}

private void btn_clear_Click(object sender, EventArgs e)
{
mtxt_schrijfen.Clear();
}

private void btn_edit_Click(object sender, EventArgs e)
{
FontDialog fontDialog1 = new FontDialog();
if (fontDialog1.ShowDialog() == DialogResult.OK & !String.IsNullOrEmpty(mtxt_schrijfen.Text))
{
mtxt_schrijfen.SelectionFont = fontDialog1.Font;
fontDialog1.Color = mtxt_schrijfen.ForeColor;
}
else
{
// richtextbox.SelectionFont = ?
}
}
}
}[/CPP]

Groot nadeel alleen is ik kan de opmaak maar niet opslaan, iemand tips???

editor.png font.png load.png save.png
 
Voor opmaak moet je opslaan in RTF (rich text format) niet TXT (platte tekst).
 
aah okey, ik heb het nu net aangepast zo dat ik het als RTF kan opslaan en openen. maar als ik het doc wil openen in mijn programma of gewoon via het bestand op te zoeken dan zie ik dat de opmaak niet is toegepast.
 
Ik heb het gevonden Echt heel erg bedankt. Heb net even mijn code door gelezen met jouw comment er naast
Voor opmaak moet je opslaan in RTF (rich text format) niet TXT (platte tekst).

En echt bedankt dat je er even bij zetten rtf rich text is daar door zag ik in de code dat er in mijn save load gedeelte nog plaintext stond.

Hier ff voor de mensen die het ook maby willen of kunnen gebruiken:

LOAD:
[CPP]

OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "rtf files (*.rtf)|*.rtf";

if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
&& openFileDialog1.FileName.Length > 0)
{
mtxt_schrijfen.LoadFile(openFileDialog1.FileName,
RichTextBoxStreamType.RichText);
}
[/CPP]

SAVE:
[CPP] pb_loading.Minimum = 0;
pb_loading.Maximum = 200;

SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "rtf files (*.rtf)|*.rtf";

if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
&& saveFileDialog1.FileName.Length > 0)
{
mtxt_schrijfen.SaveFile(saveFileDialog1.FileName,
RichTextBoxStreamType.RichText);
}[/CPP]

De editor :

[CPP]FontDialog fontDialog1 = new FontDialog();
if (fontDialog1.ShowDialog() == DialogResult.OK & !String.IsNullOrEmpty(mtxt_schrijfen.Text))
{
mtxt_schrijfen.SelectionFont = fontDialog1.Font;
fontDialog1.Color = mtxt_schrijfen.ForeColor;
}
else
{
// richtextbox.SelectionFont = ?
}[/CPP]
 
Laatst bewerkt:
Nu heb ik helaas nog een vraag aan jullie.
zou iemand mij kunnen vertellen hoe ik .docx bestanden zou kunnen openen en opslaan?
 
Daar heb je word voor nodig, in principe. Is nog wel te doen, maar dan ga je een hele andere orde van moeilijkheid in.
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan