Resize UserControls in every instance of TabPage - vb.net

This has been killing me for a couple of weeks now. I have a browser that I've made and it's using a TabControl. Every time the TabPage "+" is clicked, a new one is added. Each one of these TabPages has userControls (WebBrowser, Address Bar, Back Button, Forward Button, etc..). When the user resizes Form1, I want all userControls to fit to the TabPage/Form1.
Basically, I want my userControls to fit the current size of the Form but I can't figure it out. Right now, I have the TabControls working but that was done by simply using the Anchor property. That option isn't avialable for the UserControls. I manually anchored it programatically but it doesn't resize. It just sticks everything right in the middle with the smaller size....
here's the Code:
'THIS IS THE CODE FOR THE FORM
Imports System.IO
Public Class Form1
'The Global Variables
Dim theControls1 As New theControls
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TabPage1.Controls.Add(theControls1)
theControls1.theBrowser.Navigate("http://google.com")
End Sub
Private Sub Form1_SizeChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.SizeChanged
'For Auto Sizing theControls to Form1
theControls1.Width = TabControl1.Width - 8
theControls1.Height = TabControl1.Height - 25
End Sub
Private Sub TabControl1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.Click
'Add new tab with the same controls.
Dim theNewTab As New TabPage
Dim theOtherControls As New theControls
Dim theTabCounter As Integer = TabControl1.TabPages.Count
theOtherControls.AutoSize = True
theOtherControls.Width = TabControl1.Width
theOtherControls.Height = TabControl1.Height
theOtherControls.Anchor = AnchorStyles.Right & AnchorStyles.Left & AnchorStyles.Bottom & AnchorStyles.Top
Dim theSelectedTab As String = TabControl1.SelectedTab.Text
If theSelectedTab = "+" Then
TabControl1.TabPages.Insert(theTabCounter - 1, theNewTab)
theNewTab.Controls.Add(theOtherControls)
theControls1.theBrowser.Navigate("http://google.com")
theOtherControls.theBrowser.Navigate("http://google.com")
TabControl1.SelectTab(theTabCounter - 1)
End If
End Sub
End Class
'THIS IS THE CODE FOR THE USERCONTROLS
Imports System.IO
Imports System.Data.OleDb
Public Class theControls
'The History Database Connection String
Dim theHistoryDatabaseConn As New OleDbConnection
Private Sub ComboBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles theAddressBar.KeyDown
'Navigate to Webpage stated in theAddressBar
If e.KeyValue = Keys.Enter Then
theBrowser.Navigate(theAddressBar.Text)
e.SuppressKeyPress = True
End If
End Sub
Private Sub goForward_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles goForward.Click
theBrowser.GoForward()
End Sub
Private Sub goBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles goBack.Click
theBrowser.GoBack()
End Sub
Private Sub theBrowser_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles theBrowser.DocumentCompleted
'Set Tab Text to current web page and Address Bar
Form1.TabControl1.SelectedTab.Text = theBrowser.Url.Host.ToString
Me.theAddressBar.Text = Me.theBrowser.Url.AbsoluteUri.ToString
'Read the History
theHistoryDatabaseConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Marc Wilson\Documents\Visual Studio 2010\Projects\myBrowser\myBrowser\bin\Debug\TheHistoryDB.accdb"
theHistoryDatabaseConn.Open()
'Populate theAddressBar with the contents from the History
theAddressBar.Items.Clear()
Dim readTheHistory As String
Dim getTheHistory As OleDbCommand
readTheHistory = "SELECT [Host Name] FROM TheHistory"
getTheHistory = New OleDbCommand(readTheHistory, theHistoryDatabaseConn)
Dim theData As New OleDbDataAdapter(getTheHistory)
Dim theTable As New DataTable("TheHistory")
'theHistoryDatabaseConn.Open()
theData.Fill(theTable)
For Each row As DataRow In theTable.Rows
theAddressBar.Items.Add(row.Item("Host Name"))
Next
'Writes history to TheHistory Database (No Duplicates!)
If theAddressBar.Items.Contains(theBrowser.Url.Host.ToString) Then
Else
'Write The History
Dim writeTheHistory As OleDbCommand = New OleDbCommand("INSERT INTO TheHistory ([Host Name], [Absolute Path]) VALUES (theBrowser.URL.Host.ToString, theBrowser.URL.AbsoluteUri.ToString)", theHistoryDatabaseConn)
writeTheHistory.Parameters.Add("#Host Name", OleDbType.Char, 255).Value = theBrowser.Url.Host.ToString
writeTheHistory.Parameters.Add("#Absolute Path", OleDbType.Char, 255).Value = theBrowser.Url.AbsoluteUri.ToString
theHistoryDatabaseConn.Close()
theHistoryDatabaseConn.Open()
writeTheHistory.ExecuteNonQuery()
theHistoryDatabaseConn.Close()
End If
theHistoryDatabaseConn.Close()
End Sub
Private Sub theBrowser_ProgressChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserProgressChangedEventArgs) Handles theBrowser.ProgressChanged
'Status Bar Text
Label1.Text = theBrowser.StatusText.ToString
End Sub
Private Sub theAddressBar_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles theAddressBar.SelectedValueChanged
Me.theBrowser.Navigate(Me.theAddressBar.Text)
End Sub
End Class

Whenever you create an instance of your UserControl, set its Dock() property to Fill:
Dim theOtherControls As New theControls
theOtherControls.Dock = DockStyle.Fill

Related

A question about adding a click event to a dynamically added right-click menu

I want to add click events to the dynamically added right-click menu.The mybutton is a dynamical added button. my codes are as follows:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim tsm As ToolStripMenuItem = New ToolStripMenuItem("change icon")
AddHandler tsm.Click, AddressOf mybutton_changeicon
mybutton_cms.Items.Add(tsm)
sender.ContextMenuStrip = mybutton_cms
End Sub
Private Sub mybutton_changeicon(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs)
changeIcon.changeicon(sender, e)
End Sub
Module changeIcon
Function changeicon(ByRef Sender As System.Object, ByVal e As
System.Windows.Forms.MouseEventArgs, Optional ByVal Offset As Integer = 10)
As Boolean
Dim openfiledialog1 As New OpenFileDialog
Dim imagepath As String
If openfiledialog1.ShowDialog = DialogResult.OK Then
imagepath = openfiledialog1.FileName
Sender.Image = Image.FromFile(imagepath)
End If
End Function
End Module
when I run it , there is a problem that:
Unable to cast object of type 'System.EventArgs' to type 'System.Windows.Forms.MouseEventArgs?
what's wrong with my code?

I want to use variables in Form2 which I have declared in Form1

This is my form 1
Public Class Form1
Dim excelapp As Microsoft.Office.Interop.Excel.Application
Dim excelwb As Microsoft.Office.Interop.Excel.Workbook
Dim excelws As Microsoft.Office.Interop.Excel.Worksheet
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Calling form for New
Dim new1 As New Form2
new1.Show()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'To Open File
Using FileDialog As New OpenFileDialog
FileDialog.Title = "Select your Excel file"
FileDialog.Filter = "Microsoft Excel|*.xl*|All Files|*.*"
FileDialog.ShowDialog()
Process.Start(FileDialog.FileName)
Dim s As String = FileDialog.FileName
excelwb = excelapp.Workbooks.Open(FileDialog.FileName)
excelws = excelwb.Worksheets(1)
'End Using
' Using FileProcess As Process = Process.Start(FileDialog.FileName)
End Using
'Calling form for Open
Dim new1 As New Form2
new1.Show()
This is my Form 2
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'New Entry in existing excel file
excelws.Cells(3, 1).Value = TextBox1.Text
excelws.Cells(3, 2).Value = TextBox2.Text
excelws.Save()
End Sub
I want to use all the variables and all the things from form1 in form2.
Please help me how to use those things in my form2.
Im preparing an application for taking user input and generate a pdf for it.
im using Excel to save the data and VB Express 2010 for creating the forms.
Please guide me :) Thank you very much.
Change your declarations of excelws, etc in Form 1 to be Public:
Public excelapp As Microsoft.Office.Interop.Excel.Application
Public excelwb As Microsoft.Office.Interop.Excel.Workbook
Public excelws As Microsoft.Office.Interop.Excel.Worksheet
Then, assuming your Form1 is still called UserForm1, you can change your Form 2 code as follows:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'New Entry in existing excel file
UserForm1.excelws.Cells(3, 1).Value = TextBox1.Text
UserForm1.excelws.Cells(3, 2).Value = TextBox2.Text
UserForm1.excelws.Save()
End Sub
If it isn't called UserForm1, just substitute the appropriate name.

VB.NET - Working with Multiple Forms

I'm trying to make a login window and a main window. The problem is when I close the login form and show the main window, the whole program stops.
The login form:
Imports System.IO
Imports System.Text
Imports System.Net
Public Class frmLogin
Dim address As String = "http://puu.sh/jKJ**Zq/d613de****29.txt"
Dim client As WebClient = New WebClient()
Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'frmMain.Close()
End Sub
Private Sub frmLogin_Close(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.FormClosed
frmMain.Show()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If attemptLogin() Then
MsgBox("Login Successful")
Me.Close()
'frmMain.Show()
Else
MsgBox("Username or password is incorrect")
End If
End Sub
Private Function attemptLogin()
Dim reader As StreamReader = New StreamReader(client.OpenRead(address))
Dim line As String
Dim username As String
Dim password As String
line = reader.ReadLine()
Do While Not line Is Nothing
username = line.Split(":")(0)
password = line.Split(":")(1)
If (username = TextBox1.Text And password = TextBox2.Text) Then
Return True
End If
line = reader.ReadLine()
Loop
reader.Close()
client.Dispose()
Return False
End Function
Private Function Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim client As WebClient = New WebClient()
Dim reader As StreamReader = New StreamReader(client.OpenRead(address))
Dim line As String
line = reader.ReadLine
Do While Not line Is Nothing
line = reader.ReadLine()
Loop
reader.Close()
Dim writer As StreamWriter = New StreamWriter(client.OpenRead(address))
writer.Write(TextBox1.Text & ":" & TextBox2.Text)
writer.Close()
client.Dispose()
Return False
End Function
End Class
The Main Window:
Public Class frmMain
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
'frmMain.Close()
End Sub
Private Sub frmMain_closed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.FormClosing
End Sub
End Class
I am fairly new to VB.NET so sorry about the lack of knowledge/ugly coding. I'm used to programming GUIs in Java.
You need to set your 'Shutdown mode' to 'When last form closes'.
You can find this setting by going to My project and then clicking on Application (the first tab).
You can then find the setting at the bottom.

Create list box in runtime and change its color via menu in runtime

I need to write this small program in visual basic, but i face a problem which is i can't change the color or add list form text file during runtime.
this is picture of what i wont to do
http://i62.tinypic.com/30tghh0.png
And this is the code i wrote so far,,
Public Class Form1
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
Dim listBox1 As New System.Windows.Forms.ListBox
listBox1.Text = "New List"
listBox1.Location = New Point(25, 25)
listBox1.Size = New Size(380, 280)
Me.Controls.Add(listBox1)
OpenToolStripMenuItem.Enabled = True
SaveToolStripMenuItem.Enabled = True
SaveAsToolStripMenuItem.Enabled = True
CloseToolStripMenuItem.Enabled = True
EditToolStripMenuItem.Enabled = True
End Sub
Private Sub ExirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExirToolStripMenuItem.Click
End
End Sub
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
If (OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
Dim myfile As String = OpenFileDialog1.FileName
Dim allLines As String() = File.ReadAllLines(myfile)
For Each line As String In allLines
ListBox1.Items.Add(line)
Next
End If
End Sub
End Class
The problem as you see is in OpenToolStripMenuItem sub with line ListBox1.Items.Add(line)
is there is no listbox1 because is not created yet.the same with color, save and the rest.
so, please help me to solve it.
Move the declaration of ListBox1 out to the Class level:
Public Class Form1
Private ListBox1 As System.Windows.Forms.ListBox
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
ListBox1 = New System.Windows.Forms.ListBox
...
End Sub
...
End Class
*But why create it at run-time like this? You could add it at design-time and set the Visible() property to False. When the New button is clicked, you change Visible() to True. Unless you need a new ListBox to be created each time so you have more than one? If so, how will they be laid out?...

Resizing userControls in newly initiated TabPage

When a new TabPage is added in my TabControls, I would like it to autosize to Form1's current height/width. I can do this on Form Load. However, when I add a new tab then resize the window, it does nothing. Thanks for any helpful input.
Imports System.IO
Public Class Form1
'The Global Variables
Dim theControls As New theControls
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'For Auto Sizing theControls to Form1
TabPage1.Controls.Add(theControls)
theControls.theBrowser.Navigate("http://google.com")
End Sub
Private Sub Form1_SizeChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.SizeChanged
'For Auto Sizing theControls to Form1
Me.TabControl1.Width = Me.Width - 20
Me.TabControl1.Height = Me.Height
theControls.Width = Me.TabControl1.Width - 20
theControls.Height = Me.TabControl1.Height - 20
End Sub
Private Sub TabControl1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.Click
'Add new tab with the same controls.
Dim theNewTab As New TabPage
Dim theOtherControls As New theControls
Dim theTabCounter As Integer = TabControl1.TabPages.Count
Dim theSelectedTab As String = TabControl1.SelectedTab.Text
If theSelectedTab = "+" Then
TabControl1.TabPages.Insert(theTabCounter - 1, theNewTab)
theNewTab.Controls.Add(theOtherControls)
theControls.theBrowser.Navigate("http://google.com")
theOtherControls.theBrowser.Navigate("http://google.com")
TabControl1.SelectTab(theTabCounter - 1)
End If
End Sub
End Class
The tabPageCounter variable can be ignore. I started to think a For Each loop would be needed but I think there's an easier way. From what I have found on the web, I have gotten to this Dock function but I guess I'm not exactly clear on how to use it...
Do it in Form_sizechanged event ..
Private Sub Form1_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
Me.TabControl1.Width = Me.Width - 20
myUserControl.Width = Me.TabControl1.Width -20
End Sub