content_txt must be the name of your RichTextBox control
and
add printDialog1, printDocument1, printPreviewDialog1 in the Designer
How to solve : The name does not exist in the current context in c#
HEBA YAHIA
21
Reputation points
This is asimple windows application forms program which deals with files..etc, I have linked a photo to give a clear view of the errors I get and another one to describe my program.
I also added both libraries:
System.Drawing.Printing
System.IO
public partial class Form1 : Form
{
string path;
public Form1()
{
InitializeComponent();
path = null;
}
private void btn_open__Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
path = ofd.FileName;
foreach (string line in File.ReadAllLines(path))
content_txt.Text += line + "\n";
}
}
private void btn_save_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
path = ofd.FileName;
File.WriteAllLines(path, content_txt.Lines);
}
}
private void btn_clear_Click(object sender, EventArgs e)
{
content_txt.Text = "";
textBox1.Text = "";
}
private void btn_AVG_Click(object sender, EventArgs e)
{
double result = 0;
int n = 0;
if (path == null)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
path = ofd.FileName;
else
return;
}
foreach (string line in File.ReadAllLines(path))
{
string[] num = line.Split();
if (!string.IsNullOrEmpty(num[0]))
{
result += Int32.Parse(num[0]);
n++;
}
}
textBox1.Text = (result / n).ToString();
}
private void btn_print_Click(object sender, EventArgs e)
{
printDialog1.ShowDialog();
printDocument1.Print();
}
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawString(content_txt.Text + "\n" + textBox1.Text, this.Font, Brushes.Green, new PointF(130, 130));
}
private void btn_preview_Click(object sender, EventArgs e)
{
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
}
}
Accepted answer
-
Castorix31 71,876 Reputation points
2022-04-23T08:19:48.497+00:00