how to add browser in a form?

kippei

Newcomer
Joined
Oct 12, 2005
Messages
5
hi, i am just totally a newbie for vb.net, here is a simple question which i don't know how to solve it. i have a button here, how am i able to add a dialog box to perform browser function? thx !
 
You want to browse Files ou folders??? i will put the to of them here

Browse for Files
Code:
private void button1_Click(object sender, System.EventArgs e)
{
	string sFullPath = string.Empty;
	string sFileName = string.Empty;

	if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
	{
		sFullPath = openFileDialog1.FileName;
		sFileName = System.IO.Path.GetFileName(sFullPath);
	}	
}

Browse for Folders
Code:
private void button2_Click(object sender, System.EventArgs e)
{
	string sFullPath = string.Empty;

	if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
	{
		sFullPath = folderBrowserDialog1.SelectedPath;
	}	
}

hope this helps
 
hi, rfazendeiro . thx for your reply. it works fine. there is another simple question is if i click on that button, how is going to show that browse dialog box ? maybe it's just a normal way to open a browse. i just need to show that browse dialog box.it's actually simple, but i just don't get it. T_T.. thx.
 
yes. on the left side of your visual studio there is a tool box where you can add (in design mode) the controls to your form. Just look in there for the control openFileDialog (for files) or the folderBrowserDialog (for folders)
 
thx a lot to help me. i just get to start vb.net. everything just new to me. another problem is how to show a form inside a form. for example, i have some texts, buttons inside a form, what i need to do is show the exactly the same form by clicking one button. thx ..
 
to show a form inside another form you need to use mdi forms. it's quite simple to use and i'll explain how to use them.


1. In the main form go the the properties window. You should find a property named IsMdiContainer. Set it to true (in design view). You have set this form as the parent of the other forms

2. Done that you have let have a look at the code to open a form. Let's say i have MainForm as the mdi container and SubForm as the child form. Add this code to your button event click

[csharp]
private void button1_Click(object sender, System.EventArgs e)
{
try
{
Form formRequired = ExistsForm(this, typeof(SubForm));
if (formRequired == null )
{
SubForm sub= new SubForm();
sub.MdiParent = this;
sub.Show();
}
else
formRequired .Activate();
}
catch(Exception ex)
{
MessageBox.Show(this, String.Format(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error));
}
}

[/csharp]

3. You noticed there is a function there named ExistsForm. This function tries to see if the child form is already open. if so it activated it. if not it opens it. add this code

[csharp]
public Form ExistsForm(Form mainForm, Type typeForm)
{
Form formRequired = null;
foreach (Form form in mainForm.MdiChildren)
{
if (form.GetType() == typeForm)
{
formRequired = form;
break;
}
}
return formRequired ;
}

[/csharp]
 
thanks rfazendeiro, but i don't really understand C#, can you give me some examples in VB.net? :(
 
of course....i will translate to VB. But just for the record, if you find c# code you want to translate just look for translatores in google. Instant VB is a great tool :)

Here is the code in vb

Visual Basic:
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
	Try
		Dim formRequired As Form = ExistsForm(Me, GetType(SubForm))
		If formRequired Is Nothing Then

			Dim sub1 As SubForm= New SubForm()
			sub1.MdiParent = Me
			sub1.Show()
		Else
			formRequired.Activate()
		End If
	Catch ex As Exception
		MessageBox.Show(Me, String.Format(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error))
	End Try
End Sub


Public Function ExistsForm(ByVal mainForm As Form, ByVal typeForm As Type) As Form
	Dim formRequired As Form = Nothing
	For Each form As Form In mainForm.MdiChildren
		If form.GetType() Is typeForm Then
			formRequired = form
			Exit For
		End If
	Next form
	Return formRequired
End Function
 
oh :eek: i get it ..i see i see, thanks a lot rfazendeiro. you are really a helpful person. well, here is my another problem. for example, i have a button inside a tabcontrol, question is how should i add each tabpage by using code once click on that button? everytime increase one more tabpage by clicking. not in desgin mode, but using code to do this. :(
 
well. here's the code to add new tab pages into a tabcontrol via button;


Visual Basic:
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
	Dim newTabPage As System.Windows.Forms.TabPage = New TabPage()

	newTabPage.Location = New System.Drawing.Point(4, 22)
	newTabPage.Name = "newTabPage" & iNumTapPages
	newTabPage.Size = New System.Drawing.Size(304, 246)
	newTabPage.TabIndex = iNumTabPages
	newTabPage.Text = "tabPage " & iNumTabPages
	Me.tabControl1.Controls.Add(newTabPage)

	iNumTabPages += 1
End Sub


now tabControl1 is the Tab control.
iNumTabPages is a integer just to put diferente names into the the tab pages

hope this helps
 
Back
Top