How to solve : The name does not exist in the current context in c#

HEBA YAHIA 21 Reputation points
2022-04-23T07:40:52.137+00:00

195709-hw2-microsoft-visual-studio-4-23-2022-9-48-33-am.png195753-hw2-microsoft-visual-studio-4-23-2022-9-48-13-am.png

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();  
}  

}

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,838 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 81,831 Reputation points
    2022-04-23T08:19:48.497+00:00

    content_txt must be the name of your RichTextBox control
    and
    add printDialog1, printDocument1, printPreviewDialog1 in the Designer

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful