question

HEBAYAHIA-4558 avatar image
0 Votes"
HEBAYAHIA-4558 asked HEBAYAHIA-4558 published

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

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Castorix31 avatar image
0 Votes"
Castorix31 answered HEBAYAHIA-4558 published

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

· 9
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Do you mean like this? :

       public partial class Form1 : Form
             {
     **private PrintDocument printDocument1 = new PrintDocument();
     private PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
     private PrintDialog printDialog1 = new PrintDialog();**

         public Form1()
            { 
         InitializeComponent();
         path = null;
            }
0 Votes 0 ·

or just by Drag&Drop from the Tool Window in the Designer
(like when you add Buttons or any control)

0 Votes 0 ·

Thanks a lot you are so helpful. But I still have a problem with my program all my buttons does not work except for the open button can you figure why?






195685-hw2-microsoft-visual-studio-4-23-2022-1-11-47-pm.png


195686-hw2-microsoft-visual-studio-4-23-2022-1-19-23-pm.png


195801-hw2-microsoft-visual-studio-4-23-2022-1-20-12-pm.png


195766-hw2-microsoft-visual-studio-4-23-2022-1-20-22-pm.png


0 Votes 0 ·
Show more comments