danamically creating controlltabs that can be used - vb.net

I'm trying to dynamically create tabcontrols which works fine; the tabs are being created however. Once created I would also like them to become clickable and execute other code, this now posing a problem.
The code I’m using to create a tab is as follows
' do whatever wtih filename
Dim myTabPage As New TabPage()
myTabPage.Text = TextBox4.Text
TabControl1.TabPages.Add(myTabPage)
TabPage1.Hide()
Not so nice cause I can now fill my form with as many tabs as I like however none of them can be clicked to execute futher code???
EDIT:
Private Sub TabControl_SelectedIndexchaged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
Dim TabName As String
TabName = TabControl1.SelectedTab.Name
If TabName.Contains("TabPage") Then
' Do something
MsgBox("new tab created")
End If
End Sub

You have to add an event handler for the TabPage click event:
Dim myTabPage As New TabPage
myTabPage.Text = TextBox4.Text
AddHandler myTabPage.Click, AddressOf TabPage1_Click
TabControl1.TabPages.Add(myTabPage)
Which will call this code:
Private Sub TabPage1_Click(sender As Object, e As EventArgs)
MessageBox.Show(DirectCast(sender, TabPage).Text)
End Sub
Per your edit, you would have to add the name property:
myTabPage.Name = TextBox4.Text
And your SelectedIndexChanged event:
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As EventArgs) _
Handles TabControl1.SelectedIndexChanged
If TabControl1.SelectedTab IsNot Nothing Then
MessageBox.Show(TabControl1.SelectedTab.Name)
End If
End Sub

Related

combobox rightclick file deletion (vb.net)

Im trying to delete an item from a combobox ussing a rightclick event but i can seam to get the code for it
what I do have is the following:
Private Sub combobox1_RightClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.RightClick
Dim DELETEITEM as string= combobox1.text
System.IO.File.Delete( "C:\" & DELETEITEM & ".txt" )
MsgBox("File Deleted")
End Sub
If you are trying to delete the file with that name, try this:
Private Sub combobox1_MouseDown(sender As Object, e As MouseEventArgs) Handles combobox1.MouseDown
If e.Button = MouseButtons.Right Then
Dim DELETEITEM as string= combobox1.text
Kill("C:\" & DELETEITEM & ".txt")
MsgBox("File Deleted")
End if
End Sub
I have searched for an answer to the same question to no avail. I have however used the following workaround. Instead of using right click event, it runs a different procedure if you hold down the Ctrl Button and left click. Not perfect but it works.
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Try
If My.Computer.Keyboard.CtrlKeyDown Then
' run function a using ComboBox1 selected item
Else
' run function b using ComboBox1 selected item
End If
Catch
add err handler
End Try
End Sub

VB Clicked Links Open In New Tab

I've been searching around for a while for some code to do this, and I found a couple. most of them didn't work but I'm trying to get this one to work.
Private Sub WebBrowser1_NewWindow(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
'This creates a new tab
Dim tp As New TabPage
TabControl1.Controls.Add(tp)
'This creates a new webbrowser with the NewWindow Event
'And navigates it to the link wanting to be opened
Dim wb As New WebBrowser
Dim myElement As HtmlElement = WebBrowser1.Document.ActiveElement
Dim target As String = myElement.GetAttribute("href")
With wb
.Navigate(target)
.Dock = DockStyle.Fill
End With
AddHandler wb.NewWindow, AddressOf WebBrowser_NewWindow
tp.Controls.Add(wb)
'This prevents ie from popping up
e.Cancel = True
End Sub
But then I get a error on here WebBrowser_NewWindow, and when I check and see what it says and I am told WebBrowser_NewWindow Is Not Declared. It may be inaccessible due to protection level How am I supposed to fix this?
Full Code
Public Class Form2
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.BringToFront()
WebBrowser1.Navigate("www.google.com")
End Sub
Private Sub IClarityButton2_Click_1(sender As Object, e As EventArgs) Handles IClarityButton2.Click
If TextBox2.Text = "Close" Then
End
Else
TextBox2.Text = "Invalid"
End If
End Sub
Private Sub WebBrowser1_NewWindow(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
'This creates a new tab
Dim tp As New TabPage
TabControl1.Controls.Add(tp)
'This creates a new webbrowser with the NewWindow Event
'And navigates it to the link wanting to be opened
Dim wb As New WebBrowser
Dim myElement As HtmlElement = WebBrowser1.Document.ActiveElement
Dim target As String = myElement.GetAttribute("href")
With wb
.Navigate(target)
.Dock = DockStyle.Fill
End With
'AddHandler wb.NewWindow, AddressOf WebBrowser_NewWindow
tp.Controls.Add(wb)
'This prevents ie from popping up
e.Cancel = True
End Sub
End Class
Try removing AddHandler wb.NewWindow, AddressOf WebBrowser_NewWindow

.Net WindowsForms MouseDown event not firing

As a new user to VB, I am struggling to see why this code works in one project but not in another. This code works fine if I create a new project and 2 new forms but when I place in my project, it doesn't fire at all on either left or right click.
I have tried a try/catch statement, but no errors are being reported. How do I go about troubleshooting this to find out the error. I have tried to rem out code and run after each comment but still the same. I have even tried removing all other code on the form leaving just the 2 subs but no joy. Any help would be greatly appreciated.
frmMain
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'StorageDataSet1.Customers' table. You can move, or remove it, as needed.
Me.CustomersTableAdapter.Fill(Me.StorageDataSet1.Customers)
'TODO: This line of code loads data into the 'StorageDataSet.User' table. You can move, or remove it, as needed.
Me.UserTableAdapter.Fill(Me.StorageDataSet.User)
'Dim frmDepartmentsLive As New frmDepartment
'frmDepartmentsLive.Owner = Me
'frmDepartmentsLive.ShowDialog()
lblDate.Text = Now
Timer1.Start()
rdoCustomer.Enabled = False
rdoCustomer.Checked = True
rdoDepartment.Enabled = False
rdoDepartment.Checked = False
For Each ctrl In Me.Controls
If TypeOf ctrl Is Button Then
AddHandler CType(ctrl, Button).MouseDown, AddressOf btn_MouseDown
End If
Next
End Sub
Private Sub btn_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If (e.Button = MouseButtons.Right) Then
Dim btn = CType(sender, Button)
frmRacks.buttonName = btn.Name.Replace("btn", "")
frmRacks.Show()
ElseIf (e.Button = MouseButtons.Left) Then
MessageBox.Show("To be coded")
End If
End Sub
frmRacks
Public Class frmRacks
Public buttonName As String
Private Sub racksfrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lblRacks.Text = buttonName
End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Me.Close()
End Sub
End Class
Since the controls are on a panel, they are members of that panel's controls array, not the form's. This -- and other things -- are apparent if you look thru the form's designer (on solution explorer, click Show All, then open formXXX.designer.vb). DOnt change anything, but it shows how controls are created and added. So...
For Each ctrl In thepanelName.Controls
If TypeOf ctrl Is Button Then
AddHandler CType(ctrl, Button).MouseDown, AddressOf btn_MouseDown
End If
Next
If it is ONLY those buttons on the panel you can short cut it:
For Each btn As Button In thepanelName.Controls
AddHandler CType(ctrl, Button).MouseDown, AddressOf btn_MouseDown
Next

Exposing DataGridViewComboBoxEditingControl

I would like to know how to use the DataGridViewComboBoxEditingControl with vb.net
i need a routteen to run when the user select an item from the datagridviewcomboboxcolumn that i have configured. I am unsure how to attach the object to the column i create manually
I have implemented the following from examples on the internet but this only appears to trigger when the user click on the combobox within the column.
Private Sub dgvTicketDetail_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgvTicketDetail.EditingControlShowing
Dim editingComboBox As ComboBox = TryCast(e.Control, ComboBox)
If editingComboBox IsNot Nothing Then
AddHandler editingComboBox.SelectedValueChanged, AddressOf EditingComboBox_DropDown
End If
End Sub
Private Sub EditingComboBox_DropDown(ByVal sender As System.Object, ByVal e As System.EventArgs)
Debug.WriteLine("A ComboBox in the DataGridView just dropped down.")
End Sub
any help would be appreciated as i cant seem to find much reference material for this
Thanks in advance
Just realised that i have never posted the answer to my question
Placed the following in the EditingControlShowing to capture request
Private Sub dgvTicketDetail_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgvTicketDetail.EditingControlShowing
Try
If dgvTicketDetail.CurrentCell.ColumnIndex = 1 Then
Dim cmbox As ComboBox = TryCast(e.Control, ComboBox)
AddHandler cmbox.SelectionChangeCommitted, AddressOf Update_StockInfo
strSelectedText = cmbox.SelectedText
End If
Catch ex As Exception
End Try
End Sub
then this added items to the combobox cell
Private Sub Update_StockInfo(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim cmbClickedCell = DirectCast(sender, DataGridViewComboBoxEditingControl)
Dim cmbComboBox = DirectCast(sender, ComboBox)
If dgvTicketDetail.CurrentRow.Index = cmbClickedCell.EditingControlRowIndex And dgvTicketDetail.CurrentCell.ColumnIndex = 1 Then
Debug.WriteLine(cmbClickedCell.EditingControlRowIndex & cmbComboBox.SelectedValue)
Dim dtStock As DataTable = CropTrackMod.GetWeight(cmbComboBox.SelectedValue)
Dim dgvcc As DataGridViewComboBoxCell
dgvcc = dgvTicketDetail.Rows(cmbClickedCell.EditingControlRowIndex).Cells(2)
dgvcc.Items.Clear()
For Each row As DataRow In dtStock.Rows
dgvcc.Items.Add(row.Item("UnitName"))
Next row
If CropTrackMod.IsStockVATAble(cmbComboBox.SelectedValue) = True Then
dgvTicketDetail.Rows(cmbClickedCell.EditingControlRowIndex).Cells("VATRate").Value = CropTrackMod.dblVATRate
Else
dgvTicketDetail.Rows(cmbClickedCell.EditingControlRowIndex).Cells("VATRate").Value = "0.00"
End If
End If
End Sub
It works really well for me, unfortunately the user wanted something different in the end ha. Oh well at least i learnt something

how to run a function/sub after loading the form window in VB?

I have a function that gets User ID from USB badge reader, used to log in an application.
when I run the app, the log in window does not appear until I swipe the tag.
I need to know if it`s possible to load the windows, then to start running the function that gets the data from the USB.
Thanks :)
Private Sub SerialPort1_DataReceived()
'Threading.Thread.SpinWait(1000)
OpenPort()
If SerialPort1.IsOpen() Then
byteEnd = SerialPort1.NewLine.ToCharArray
'read entire string until .Newline
readBuffer = SerialPort1.ReadLine()
readBuffer = readBuffer.Remove(0, 1)
readBuffer = readBuffer.Remove(8, 1)
WWIDTextBox.AppendText(readBuffer)
End If
End Sub
Private Sub Form1_Activated(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Activated
SerialPort1_DataReceived()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'SerialPort1_DataReceived()
End Sub
The problem is that you are calling the ReadLine method, which is a blocking (synchronous) method. In other words, when you call it, the method does not return the value until it has the value to return. Because of that, it stops execution on the current thread until a complete line is read (when the badge is swiped). Since you are on the UI thread when you call it, it will lock up the UI until the badge is swiped.
Instead of calling your SerialPort1_DataReceived method from the UI thread, you can do the work from a different thread. The easiest way to do that is to drag a BackgroundWorker component onto your form in the designer. Then you can add code like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
OpenPort()
If SerialPort1.IsOpen() Then
byteEnd = SerialPort1.NewLine.ToCharArray
Dim readBuffer As String = SerialPort1.ReadLine()
readBuffer = readBuffer.Remove(0, 1)
readBuffer = readBuffer.Remove(8, 1)
e.Result = readBuffer
End If
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
WWIDTextBox.AppendText(CStr(e.Result))
End Sub
Working on VS2013, I came across the same issue, I needed to to a datagridview refresh (colors in the gridrows). This worked for me.
Sub MyForm_VisibleChanged(sender As Object, e As EventArgs) Handles Me.VisibleChanged
If Me.Visible Then
'do action...
End If
End Sub
Try Form Activated Event
Private Sub Form1_Activated(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Activated
'Call your function here
End Sub
It call the function After the Form Loads...
Private Sub loadCombo()
Dim sqlconn As New OleDb.OleDbConnection
Dim connString As String
connString = ""
Dim access As String
access = "select slno from atable"
Dim DataTab As New DataTable
Dim DataAdap As New OleDbDataAdapter(access, connString)
DataAdap.Fill(DataTab)
ComboBox1.DataSource = DataTab
ComboBox1.DisplayMember = "slno"
End Sub