473,320 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to get info from form control methods.

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


Nov 17 '05 #1
1 1849
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" wrote:
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

Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Michal Mieszkowski | last post by:
i have a block of html code looking like this <FORM name=myform> <INPUT type=text name=firstname> <DIV id=mydiv> <INPUT type=text name=address> </DIV> </FORM> i can access firstname field...
3
by: Tom Turner | last post by:
Here's the background on my situation. The question follows --- We have 600 units of mail going from our business to various Post Offices every morning. Every unit is accompanied by a paper...
16
by: TD | last post by:
This is the code under a command button - Dim ctl As Control For Each ctl In Me.Controls If ctl.BackColor <> RGB(255, 255, 255) Then ctl.BackColor = RGB(255, 255, 255) End If Next ctl
4
by: normb | last post by:
My name is Norm, I changed something that caused this problem, and I do not what it was. I also do not know how to debug this problem! The line where the crash occures is marked by an *. The...
2
by: Franky | last post by:
Threre is a Form containing a usercontrol In the form's Load event it references a usercontrol property, say, zz The first showdialog(formx) causes 1 usercontrol_load event 2 form_load event...
12
by: Rob | last post by:
Let's say you open Form1 that contains TabControl1 There are several tabs on TabControl1 Now you open a new Form2 that contains a User Control How can you determine the Selected tab in Form1...
6
by: Franck | last post by:
I know in vb6 it was super easy to pass parameter or either read example the textbox1.text on the already open form form2 like all object were as public. but is there a way to do something that...
5
by: John Kotuby | last post by:
Hi all, After more than a year programming with ASP.NET 2.0 and VB I am still finding it difficult to leave some habits from classic ASP behind. this is particularly true with cross-page posting....
9
by: dhtml | last post by:
I have written an article "Unsafe Names for HTML Form Controls". <URL: http://jibbering.com/faq/names/ > I would appreciate any reviews, technical or otherwise. Garrett --...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.