How to get info from form control methods.


G

Guest

Hi, I am fiarly new to C#.

I have been trying to test the results of 2 button controls in a form.
A directory is selected with botton1_Click and a filename is selected with
button2_Click.
I want to press botton3 (the "OK" button) and test to make sure that
fileName1 and folderName1 exist or are valid. And then execute the code to
dir.GetFiles and write the filenames to the test file.
Apparently fileName1 and folderName1 are no longer available when button3 is
pressed ( button3_Click does not process the code).

The button_Click declaration is the default of the control so I am not sure
if I need something different than what I post.
I have spent a lot of time searching the archives and tutorials but don't
know enough yet to figure this one out.
Can someone tell me what would be the appropriate way to handle this?
I have looked at Delegates and property methods but need help on this.
Feel free to point out any better ways.

TIA

Bill

/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Application.Run(new Form1());
}
public string folderName1, fileName1 ;

public void button1_Click(object sender, System.EventArgs e) //open folder
selection.
{
DialogResult result = folderBrowserDialog1.ShowDialog();
if(result == DialogResult.OK ) //folder selected
{
folderName1 = folderBrowserDialog1.SelectedPath;
label1.Text = folderName1 ;
}
}

public void button2_Click(object sender, System.EventArgs e)
{
saveFileDialog1.Filter = "txt files (*.txt)|*.txt" ; //display dialog
saveFileDialog1.OverwritePrompt = true ;
saveFileDialog1.ValidateNames = true ;
saveFileDialog1.InitialDirectory = "g:\\lsptemp" ;
saveFileDialog1.RestoreDirectory = true ;
saveFileDialog1.CheckFileExists = false ; //will not add
..ext if true.
saveFileDialog1.DereferenceLinks = false ;
saveFileDialog1.AddExtension = true ;
saveFileDialog1.DefaultExt = "TXT" ;
DialogResult result2 = saveFileDialog1.ShowDialog();
if (result2 == DialogResult.OK )
{
fileName1 = saveFileDialog1.FileName;
label2.Text = fileName1 ;
}
}

public void button3_Click(object sender, System.EventArgs e)
{
if (folderName1 != null && fileName1 != null)
{
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(folderName1);
StreamWriter writer = new StreamWriter(fileName1);
foreach (System.IO.FileInfo file in dir.GetFiles("*.*"))
{
writer.WriteLine(" {0}", file.FullName);
}
writer.Close();
MessageBox.Show("File names from: \n" +
folderName1 +
"\n "
+ "\n Written to: \n"
+ fileName1);

Application.Exit();
}
}

private void Form1_Load(object sender, System.EventArgs e)
{
}
} //end main
 
Ad

Advertisements

G

Guest

Again, Sorry for any inconvenience.
For some reason, when I added the botton3 control in the design form, the
this.button3.Click += new System.EventHandler(this.button3_Click);
was not automatically added to the InitializeComponent section of code as
the other 2 buttons were.

I added it manually and all works as expected.


Thanks again.

Bill

BillZondlo said:
Hi, I am fiarly new to C#.

I have been trying to test the results of 2 button controls in a form.
A directory is selected with botton1_Click and a filename is selected with
button2_Click.
I want to press botton3 (the "OK" button) and test to make sure that
fileName1 and folderName1 exist or are valid. And then execute the code to
dir.GetFiles and write the filenames to the test file.
Apparently fileName1 and folderName1 are no longer available when button3 is
pressed ( button3_Click does not process the code).

The button_Click declaration is the default of the control so I am not sure
if I need something different than what I post.
I have spent a lot of time searching the archives and tutorials but don't
know enough yet to figure this one out.
Can someone tell me what would be the appropriate way to handle this?
I have looked at Delegates and property methods but need help on this.
Feel free to point out any better ways.

TIA

Bill

/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Application.Run(new Form1());
}
public string folderName1, fileName1 ;

public void button1_Click(object sender, System.EventArgs e) //open folder
selection.
{
DialogResult result = folderBrowserDialog1.ShowDialog();
if(result == DialogResult.OK ) //folder selected
{
folderName1 = folderBrowserDialog1.SelectedPath;
label1.Text = folderName1 ;
}
}

public void button2_Click(object sender, System.EventArgs e)
{
saveFileDialog1.Filter = "txt files (*.txt)|*.txt" ; //display dialog
saveFileDialog1.OverwritePrompt = true ;
saveFileDialog1.ValidateNames = true ;
saveFileDialog1.InitialDirectory = "g:\\lsptemp" ;
saveFileDialog1.RestoreDirectory = true ;
saveFileDialog1.CheckFileExists = false ; //will not add
.ext if true.
saveFileDialog1.DereferenceLinks = false ;
saveFileDialog1.AddExtension = true ;
saveFileDialog1.DefaultExt = "TXT" ;
DialogResult result2 = saveFileDialog1.ShowDialog();
if (result2 == DialogResult.OK )
{
fileName1 = saveFileDialog1.FileName;
label2.Text = fileName1 ;
}
}

public void button3_Click(object sender, System.EventArgs e)
{
if (folderName1 != null && fileName1 != null)
{
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(folderName1);
StreamWriter writer = new StreamWriter(fileName1);
foreach (System.IO.FileInfo file in dir.GetFiles("*.*"))
{
writer.WriteLine(" {0}", file.FullName);
}
writer.Close();
MessageBox.Show("File names from: \n" +
folderName1 +
"\n "
+ "\n Written to: \n"
+ fileName1);

Application.Exit();
}
}

private void Form1_Load(object sender, System.EventArgs e)
{
}
} //end main
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top