0

hi there,
is there a browse control in C# windows application form ,
if so , plz, how can I add it to my toolBox and how can I use it to allow the user to browse any file she wants,
if not how can I implement the idea og browsing ( that the user choose a file and then I do what ever I need with its path)

urgent help is neede pleease
Thanks in advance

6
Contributors
6
Replies
8
Views
8 Years
Discussion Span
Last Post by mmaster
0

Ahhh Codeproject - love that site Jerry! I think (not tested it) this will work:

OpenFileDialog op = new OpenFileDialog();
op.ShowDialog();

Hope that helps! Just put that in the event handler of the button you wish to be the browse button!

0

Did this solve your issue? If it did could you mark the thread as solved please so that others know it is solved!:)

0
namespace GenerateHtmlNamespace 
{
    using System;
    using System.Drawing;
    using System.ComponentModel;
    using System.WinForms;
    using System.IO;

    public class MakeHtml : Form 
    {
		DialogResult s;
		private System.ComponentModel.Container components;
	    private StatusBar statusBar1;
	
	    private Label label1;
	    private Label label2;
	
	    private TextBox textBox1;
	    private TextBox textBox2;
	
	    private Button button1;
	    private Button button2;
	    private Button button3;
	    private Button button4;	
	    private Button button5;		        
	    private GroupBox groupBox1;
	    private GroupBox groupBox2;
	    	    	
	    private string nm = "";
	    private string filename = "";
	    private StreamWriter writer = null;

	    public MakeHtml() 
	    {
	    	InitializeComponent();
        }

        public override void Dispose() 
 		{
	        base.Dispose();
	        components.Dispose();
	    }
	
        public static void Main(string[] args) 
        {
	        Application.Run(new MakeHtml());
	    }

        private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();

			this.label1 = new Label();
			this.label2 = new Label();
			
			this.textBox1 = new TextBox();
			this.textBox2 = new TextBox();			
			
			this.groupBox1 = new GroupBox();
			this.groupBox2 = new GroupBox();
			
			this.button1 = new Button();
			this.button2 = new Button();
			this.button3 = new Button();
			this.button4 = new Button();
			this.button5 = new Button();
									
			this.statusBar1 = new StatusBar();
			
			label1.Location = new System.Drawing.Point(88, 56);
			label1.Text = "Enter Your Name";
			label1.Size = new System.Drawing.Size(192, 32);
			label1.BorderStyle = System.WinForms.BorderStyle.FixedSingle;
			label1.ForeColor = System.Drawing.SystemColors.ActiveCaption;
			label1.Font = new System.Drawing.Font("Comic Sans MS", 12f);
			label1.BackColor = System.Drawing.Color.Bisque;
			label1.TextAlign = System.WinForms.HorizontalAlignment.Right;
			
			label2.Location = new System.Drawing.Point(88, 104);
			label2.Text = "Enter HTML File Name ";
			label2.Size = new System.Drawing.Size(192, 32);
			label2.BorderStyle = System.WinForms.BorderStyle.FixedSingle;
			label2.ForeColor = System.Drawing.SystemColors.ActiveCaption;
			label2.Font = new System.Drawing.Font("Comic Sans MS", 12f);
			label2.BackColor = System.Drawing.Color.Bisque;
			label2.TextAlign = System.WinForms.HorizontalAlignment.Right;
			
			textBox1.Location = new System.Drawing.Point(296, 56);
			textBox1.ForeColor = System.Drawing.SystemColors.ActiveCaption;
			textBox1.Font = new System.Drawing.Font("Lucida Sans Unicode", 12f);
			textBox1.Text = "Subhagata";
			textBox1.Size = new System.Drawing.Size(208, 32);
			textBox1.BackColor = System.Drawing.Color.NavajoWhite;
	
			textBox2.Location = new System.Drawing.Point(296, 104);
			textBox2.ForeColor = System.Drawing.SystemColors.ActiveCaption;
			textBox2.Font = new System.Drawing.Font("Lucida Sans Unicode", 12f);
			textBox2.Text = "myFile.html";
			textBox2.Size = new System.Drawing.Size(208, 32);
			textBox2.BackColor = System.Drawing.Color.NavajoWhite;
	
			button1.Location = new System.Drawing.Point(144, 280);
			button1.ForeColor = System.Drawing.SystemColors.ControlDarkDark;
			button1.BackColor = System.Drawing.SystemColors.ControlLight;
			button1.Size = new System.Drawing.Size(112, 40);
			button1.Font = new System.Drawing.Font("Times New Roman", 12f);
			button1.Text = "Make HTML";
			button1.Click += new System.EventHandler(button1_Click);
	
			button2.Location = new System.Drawing.Point(256, 280);
			button2.ForeColor = System.Drawing.SystemColors.ControlDarkDark;
			button2.BackColor = System.Drawing.SystemColors.ControlLight;
			button2.Size = new System.Drawing.Size(112, 40);
			button2.Font = new System.Drawing.Font("Times New Roman", 12f);
			button2.Text = "Clear";
			button2.Click += new System.EventHandler(button2_Click);

			button3.Location = new System.Drawing.Point(368, 280);
			button3.ForeColor = System.Drawing.SystemColors.ControlDarkDark;
			button3.BackColor = System.Drawing.SystemColors.ControlLight;
			button3.Size = new System.Drawing.Size(104, 40);
			button3.Font = new System.Drawing.Font("Times New Roman", 12f);
			button3.Text = "Exit";
			button3.Click += new System.EventHandler(button3_Click);
			
			button4.Location = new System.Drawing.Point(184, 184);
			button4.ForeColor = System.Drawing.Color.DarkSlateGray;
			button4.BackColor = System.Drawing.SystemColors.ControlLight;
			button4.Size = new System.Drawing.Size(120, 32);
			button4.Font = new System.Drawing.Font("Times New Roman", 8f);
			button4.Text = "Change Window Color";
			button4.Click += new System.EventHandler(button4_Click);			
			
			button5.Location = new System.Drawing.Point(304, 184);
			button5.ForeColor = System.Drawing.Color.DarkSlateGray;
			button5.BackColor = System.Drawing.SystemColors.ControlLight;
			button5.Size = new System.Drawing.Size(120, 32);
			button5.Font = new System.Drawing.Font("Times New Roman", 8f);
			button5.Text = "Change Label Color";
			button5.Click += new System.EventHandler(button5_Click);			
				
			groupBox1.Location = new Point(96, 256);
			groupBox1.Text = "Action";
			groupBox1.Size = new Size(416, 88);

			groupBox2.Location = new Point(85, 168);
			groupBox2.Text = "Customize Window Settings";
			groupBox2.Size = new Size(432, 64);
			
			statusBar1.Location = new Point(0, 200);
			statusBar1.Size = new Size(200, 16);
			statusBar1.Text = "Enter Details";
									
			this.Text = "MakeHtml";
			this.Cursor = Cursors.PanNW;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.BackColor = System.Drawing.SystemColors.GrayText;
			this.ClientSize = new System.Drawing.Size(592, 485);
			this.StartPosition = FormStartPosition.CenterScreen;

			this.Controls.Add(label1);
			this.Controls.Add(label2);
			
			this.Controls.Add(textBox1);
			this.Controls.Add(textBox2);
			
			this.Controls.Add(button1);
			this.Controls.Add(button2);
			this.Controls.Add(button3);
			this.Controls.Add(button4);
			this.Controls.Add(button5);			
			
			this.Controls.Add(groupBox1);
			this.Controls.Add(groupBox2);
			
			this.Controls.Add(statusBar1);
		}
	
	    protected void button1_Click(object sender, System.EventArgs e)
		{
			nm = textBox1.Text.Trim();
			filename = textBox2.Text.Trim();

			if((textBox1.Text == "") || (textBox2.Text == ""))
			{
				statusBar1.Text = "Please enter the required details.";
				textBox1.Focus();
			}
			
			else
			{
				if(GenerateCode())
				{
					statusBar1.Text = "Html code generated to file "+filename;
				}
				else
				{
					statusBar1.Text = "Error";
				}
			}
		}
		
	    protected void button2_Click(object sender, System.EventArgs e)
		{
			textBox1.Text = "";
			textBox2.Text = "";
			statusBar1.Text = "Cleared All";
		}	

	    protected void button3_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

	    protected void button4_Click(object sender, System.EventArgs e)
		{
			ColorDialog colorDialog1 = new ColorDialog();
			colorDialog1.AllowFullOpen=false;
	
			colorDialog1.ShowDialog();
			Color objColor = colorDialog1.Color;
			this.BackColor=objColor;
		}

	    protected void button5_Click(object sender, System.EventArgs e)
		{
			ColorDialog colorDialog1 = new ColorDialog();
			colorDialog1.AllowFullOpen=false;
	
			colorDialog1.ShowDialog();
			Color objColor = colorDialog1.Color;
			label1.ForeColor=objColor;
			label2.ForeColor=objColor;
		}
								
		private bool GenerateCode()
		{
			File f = new File(filename);
			if(f.Exists)
			{
				s=MessageBox.Show("This fils already exists. Do you want to delete it ???" , "Worning" , MessageBox.YesNo | MessageBox.IconExclamation);
				if(String.Format("{0}" , s) == "Yes")
				{
					f.Delete();
				}
				else
				{
					s=MessageBox.Show("Enter a different file name." , "Worning");
					textBox2.Focus();
				}				
				
			}
	
			FileStream outputfile = null;
			try
			{
				outputfile = new FileStream(filename,FileMode.OpenOrCreate, FileAccess.Write);

	     		writer = new StreamWriter(outputfile);

	     		writer.BaseStream.Seek(0, SeekOrigin.End);

	    		DoWrite ("<HTML>");
	    		DoWrite ("<HEAD>");
	    		DoWrite ("<TITLE>");
	    		DoWrite ("This HTML file is create in C#");
	    		DoWrite ("</TITLE>");
	    		DoWrite ("</HEAD>");
				DoWrite ("<BODY BGCOLOR='Tan'>");
				DoWrite ("<CENTER>");
				DoWrite ("<BR><BR><H2>Hello " + nm + "</H2>");
				DoWrite ("<BR><H4><A HREF='http://www.csharphelp.com'>www.csharphelp.com</A></H4>");
				DoWrite ("</CENTER>");				
	    		DoWrite ("</BODY>");
	    		DoWrite ("</HTML>");
	    		writer.Close();
	    	}
	    	catch(Exception ex)
	    	{
	    		Console.WriteLine("Exception GenerateCode = "+ex);
	    		statusBar1.Text = "Error";
	    		outputfile = null;
	    		writer = null;
	    		return false;
	    	}
	    	return true;
		}

		private void DoWrite (String line)
	  	{
	    	writer.WriteLine(line);
	    	writer.Flush();
		}
	}
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.