Hello everyone

I can export texblock.text and label.text to word. But there is no styling found back in the word document
For example the background-color won't export. I use this as a conformation document.
How can you also export the style of the form and can you export a table (make a table when the form is exported to word? in thios case 1 row 2 columns)

code for export works fine

//Save to document
                String strtxt = textBox3.Text + label5.Text;
                TextWriter sw = new StreamWriter(@"C:\Users\filename");
                sw.WriteLine(strtxt);
                sw.Close();

Thanks everyone

Recommended Answers

If you use a RichTextBox you can export the text as an .rtf which will include any format styling you've done, which can then be read by Word.

Jump to Post

Most likely. The RichTextBox has a method to save the contents to rtf(richTextBox1.SaveFile("myFile.rtf");)

Jump to Post

The RichTextBox can accept a users input. Why do you want to insert a TextBox into thte RichTextBox?

Jump to Post

All 9 Replies

If you use a RichTextBox you can export the text as an .rtf which will include any format styling you've done, which can then be read by Word.

@tinstaafl and others
Thanks, but when I make the richtext box background color blue
and add an textbox in the richtexbox..still the style won't be export to word of wordpad

Is this because it isn't saved as a rtf file?
Or?

Thanks

Most likely. The RichTextBox has a method to save the contents to rtf(richTextBox1.SaveFile("myFile.rtf");)

@all,

It's impossible to insert a textbox.text into a richtextbox. Or am I doing something wrong?

The idea is that there are some textboxes in the richtextbox where a user can insert some valuables and automaticaly count the valuables and save them in a txt file with styling. Or is there an other way?

Saving the richtextbox isn't a problem..!

Thanks

The RichTextBox can accept a users input. Why do you want to insert a TextBox into thte RichTextBox?

Hello Tinstaafl,

This is a quick form I build for the example. When saving, the file is 0 kb

Why? I would like to make a winform, but also style this, so its printable and looks nice.

When I export the textblock.Text to a file I can't style them.

Thanks

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == String.Empty || textBox2.Text == String.Empty || textBox6.Text == String.Empty)
            {
                MessageBox.Show("Please insert a value (label 1 and 2)");
            }
            else
            {

                int a = Convert.ToInt32(textBox1.Text);
                int b = Convert.ToInt32(textBox2.Text);

                int c = a * b;
                textBox3.Text = c.ToString();

                int d = Convert.ToInt32(textBox6.Text);
                int k = 19;
                int l = d * k;
                textBox9.Text = l.ToString();


                MessageBox.Show("Total km is ('textBox1.Text') insert" + textBox3.ToString());


                //save richtextbox
            string Saved_File = "";

                saveFD.InitialDirectory = "C:";
                saveFD.Title = "Save the file";
               saveFD.FileName = "";


                saveFD.Filter = "Text Files|*.txt|All Files|*.*";

                if (saveFD.ShowDialog() != DialogResult.Cancel)
              {

                 Saved_File = saveFD.FileName;
                   richTextBox1.SaveFile(Saved_File, RichTextBoxStreamType.PlainText);
                }


            }
        }

        private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }


    }

}

I think you're missing the point. The RichTextBox replaces textBox1. Assuming the RichtTextBox is named RichTextBox1, you would set the style of the contents using the methods that are a part of the RichTextBox class. When you're ready to save the contents you use the SaveFile method of the RichTextBox class to create a file with the name of your choice:

'Set the text
RichTextBox1.Text = "Test"

'Use a Select method to select some text
RichTextBox1.SelectAll()

'Format the selected text
RichTextBox1.SelectionBackColor = Color.LightGray
RichTextBox1.SelectionColor = Color.Blue

'Clear the selection to leave just the formatted text
RichTextBox1.SelectionLength = 0

'Save the formatted text in a .rtf type file that can be opened by most if not all word processing applications(Word, WordPad,OpenOffice Writer, etc.)
RichTextBox1.SaveFile("C:\Temp\test.rtf")

For more information on the RichtextBox here's the Help page for it

Richtextbox can be used for exporting styles like font,Color,Background colr etc.Check the below example for richTextbox.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        richTextBox1.Font = new Font("Consolas", 18f, FontStyle.Bold);
        richTextBox1.BackColor = Color.AliceBlue;
        string[] words =
        {
        "Dot",
        "Net",
        "Perls",
        "is",
        "a",
        "nice",
        "website."
        };
        Color[] colors =
        {
        Color.Aqua,
        Color.CadetBlue,
        Color.Cornsilk,
        Color.Gold,
        Color.HotPink,
        Color.Lavender,
        Color.Moccasin
        };
        for (int i = 0; i < words.Length; i++)
        {
        string word = words[i];
        Color color = colors[i];
        {
            richTextBox1.SelectionBackColor = color;
            richTextBox1.AppendText(word);
            richTextBox1.SelectionBackColor = Color.AliceBlue;
            richTextBox1.AppendText(" ");
        }
        }
    }
    }

Super...I really missed the point!
But now Its working like a charm

Now we can start the build...
Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.