Results 1 to 4 of 4

Thread: how to link datagridviews events created using multiple tabcontrol

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    8

    Question how to link datagridviews events created using multiple tabcontrol

    Hello to all the programmers on vb.net, I have a difficulty with tabcontrol that creates multiple pages like datagridview too.

    pressing the "add" icon next to the tabpage name creates a datagridview, but my drawback is that it doesn't recognize dgv as datagridview2, in events

    "Resource1.close" --> close.png
    "Resource1.add" --> add.png

    Code:
    Sub TabControl1DrawItem(sender As Object, e As DrawItemEventArgs)
    	Dim tabpage = Me.tabControl1.TabPages(e.Index)
    	Dim tabRect = Me.tabControl1.GetTabRect(e.Index)
    	tabRect.Inflate(-2, -2)
    	'--------- only tab selected 
    	Dim font As Font = tabControl1.Font
    	Dim brush As New SolidBrush(Color.Black)
    	Dim tabTextArea As RectangleF = RectangleF.op_Implicit(tabControl1.GetTabRect(e.Index))
    		
    	If tabControl1.SelectedIndex = e.Index Then
    		font = New Font(font, FontStyle.Bold)
    		brush = New SolidBrush(Color.Red)
    	End If
    	
    		
    	If e.Index = Me.tabControl1.TabCount - 1 Then
    		Dim addImage As Object = Resource1.add
    		e.Graphics.DrawImage(addImage, tabRect.Left + (tabRect.Width - addImage.Width) \ 2, tabRect.Top + (tabRect.Height - addImage.Height) \ 2)
    			' imagen de add o el +
    			
    	Else
    		Dim closeImage As Object = Resource1.close
    		e.Graphics.DrawImage(closeImage, (tabRect.Right - closeImage.Width), tabRect.Top + (tabRect.Height - closeImage.Height) \ 2)
    			'TextRenderer.DrawText(e.Graphics, tabpage.Text, tabpage.Font, tabRect, tabpage.ForeColor, TextFormatFlags.Left)
    	End If
    		
    	e.Graphics.DrawString(tabControl1.TabPages(e.Index).Text, font, brush, tabTextArea)
    		
    End Sub
    	
    Sub TabControl1MouseDown(sender As Object, e As MouseEventArgs)		
    	Dim lastIndex = Me.tabControl1.TabCount - 1
    	If Me.tabControl1.GetTabRect(lastIndex).Contains(e.Location) Then
    	        Me.tabControl1.TabPages.Insert(lastIndex, "New Tab")
    			Me.tabControl1.SelectedIndex = lastIndex
    		Me.tabControl1.TabPages(lastIndex).UseVisualStyleBackColor = True
    			
    		Dim dgv As New DataGridView
    		Dim estilo1 As New DataGridViewCellStyle
    			
    	With estilo1
    		.Alignment = System.Windows.Forms.DataGridViewHeaderBorderStyle.None
    		.BackColor = System.Drawing.Color.FromArgb(CType(CType(52,Byte),Integer), CType(CType(52,Byte),Integer), CType(CType(52,Byte),Integer))
    		.Font = New System.Drawing.Font("Consolas",9.75!,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point, CType(0,Byte))
    		.ForeColor = System.Drawing.Color.White
    		.SelectionBackColor = System.Drawing.SystemColors.Highlight
    		.SelectionForeColor = System.Drawing.SystemColors.HighlightText
    		.WrapMode = System.Windows.Forms.DataGridViewTriState.[True]
    	End With
    			
    	With dgv
    		.BackgroundColor = System.Drawing.SystemColors.ButtonShadow
    		.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.None
    		.EnableHeadersVisualStyles = False
    		.Name = "dataGridView2"
    		.Dock = System.Windows.Forms.DockStyle.Fill
    		.RowHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.None
    		.RowHeadersVisible = False
    		.ColumnHeadersDefaultCellStyle	= estilo1
    		.ColumnCount = 5
    		.Columns(0).Name = "Column 1"
    		.Columns(1).Name = "Column 2"
    		.Columns(2).Name = "Column 3"
    		.Columns(3).Name = "Column 4"
    		.Columns(4).Name = "Column 5"
    	End With
    			
    	AddHandler dgv.DataSourceChanged, AddressOf dgvDataSourceChanged
    	AddHandler dgv.KeyDown, AddressOf dgvKeydown
    			
    	Me.tabControl1.TabPages(lastIndex).Controls.Add(dgv)
    			
    			
    			
           Else
    	      For i As Integer = 0 To Me.tabControl1.TabPages.Count - 1
    			Dim tabRect = Me.tabControl1.GetTabRect(i)
    			tabRect.Inflate(-2,-2)
    			Dim closeImage = Resource1.close
    			Dim imageRect = New Rectangle((tabRect.Right - closeImage.Width), tabRect.Top + (tabRect.Height -closeImage.Height)/2, closeImage.Width, closeImage.Height)
    				
    			If imageRect.Contains(e.Location) Then
    				If MsgBox("┬┐Desea eliminar este hoja de datos?", MsgBoxStyle.OkCancel,"Eliminar") = MsgBoxResult.Ok Then
    					tabControl1.TabPages.RemoveAt(i)
    					Exit For
    				End If
    					
    				Exit For
    				
    		     End If
    		Next
    		End If
    		
    	End Sub
    
    ' =============  !! error reference to dgv  !!  ====
    ' ------ add functions events
    
    	Sub dgvDataSourceChanged(sender As Object, e As EventArgs)
    		
    	End Sub
    	
    Sub dgvKeydown(sender As Object, e As KeyEventArgs)	
    	'Me.ActiveControl
    	Dim fuente As Single = dgv.DefaultCellStyle.Font.Size
    	If (e.Control AndAlso (e.KeyCode = Keys.C)) Then
    		' event copy
    	ElseIf (e.Control AndAlso e.KeyCode = Keys.V)
    		Try
    			For Each line As String In Clipboard.GetText.Split(vbNewLine)
    				If Not line.Trim.ToString = "" Then
    					' line.Split('\t');
    					Dim item() As String = line.Split(vbTab(0))
    					'dataGridView2.RowS
    					Me.dgv.Rows.Add(item)
    					End If
    				Next
    		Catch ex As Exception
    			MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
    		End Try
    	End If
    	If (e.Control AndAlso (e.KeyCode = Keys.Add Or e.KeyCode = Keys.Oemplus)) Then
    		dgv.DefaultCellStyle.Font = New Font("Calibri", fuente + 1, FontStyle.Regular)
    		dgv.AutoResizeColumns(dgv.AutoSizeColumnsMode.AllCells)
    		dgv.AutoResizeRows(dgv.AutoSizeRowsMode.AllCells)
    		'MsgBox("add ID book", MsgBoxStyle.Exclamation, "Book no add")
    	ElseIf (e.Control AndAlso (e.KeyCode = Keys.Subtract Or e.KeyCode = Keys.OemMinus))
    		dgv.DefaultCellStyle.Font = New Font("Calibri", fuente - 1, FontStyle.Regular)
    		dgv.AutoResizeColumns(dgv.AutoSizeColumnsMode.AllCells)
    		dgv.AutoResizeRows(dgv.AutoSizeRowsMode.AllCells)
    	End If
    		
    End Sub

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,461

    Re: how to link datagridviews events created using multiple tabcontrol

    It's not completely clear what you're asking but, as you're talking about controls created at run time, I'm going to guess you're having the same type of problem that many other people do. Inside any event handler, the sender parameter is a reference to the object that raised the event. If you are creating DataGridView controls at run time and using AddHandler to attach event handlers, you can refer to the DataGridView that raised the event using that parameter, e.g.
    vb.net Code:
    1. Sub dgvKeydown(sender As Object, e As KeyEventArgs)
    2.     Dim grid = DirectCast(sender, DataGridView)
    3.  
    4.     'Use grid here.
    5. End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    8

    Re: how to link datagridviews events created using multiple tabcontrol

    Quote Originally Posted by jmcilhinney View Post
    It's not completely clear what you're asking but, as you're talking about controls created at run time, I'm going to guess you're having the same type of problem that many other people do. Inside any event handler, the sender parameter is a reference to the object that raised the event. If you are creating DataGridView controls at run time and using AddHandler to attach event handlers, you can refer to the DataGridView that raised the event using that parameter, e.g.
    vb.net Code:
    1. Sub dgvKeydown(sender As Object, e As KeyEventArgs)
    2.     Dim grid = DirectCast(sender, DataGridView)
    3.  
    4.     'Use grid here.
    5. End Sub
    thank you very much @jmcilhinney
    it works and it's perfect

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,461

    Re: how to link datagridviews events created using multiple tabcontrol

    You might also want to consider creating your own custom tab pages with the grid built in. For an example of that type of thing, follow the Codebank link in my signature and check out my Tabbed Web Browser thread. That uses a custom TabControl and custom TabPages with a WebBrowser control built in.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width