Creating a Combobox from a Class - In VB.net - vb.net

im new to vb.net, and my query is:
file: read.vb
Friend Function Nav()
Dim NavBox As New ComboBox()
NavBox.Size = New System.Drawing.Size(44, 21)
NavBox.Location = New System.Drawing.Point(135, 305)
NavBox.DropDownStyle = ComboBoxStyle.DropDownList
NavBox.Items.Add("1")
NavBox.Items.Add("2")
NavBox.Items.Add("3")
NavBox.Items.Add("4")
NavBox.Items.Add("5")
NavBox.Items.Add("6")
NavBox.Items.Add("7")
NavBox.Items.Add("8")
NavBox.Items.Add("9")
NavBox.Items.Add("10")
NavBox.Items.Add("11")
AddHandler (NavBox.SelectionChangeCommitted), AddressOf MSGB
NavBox.Show()
Return NavBox
End Function
Public Sub MSGB(ByVal sender As Object, ByVal e As System.EventArgs)
Dim cb As ComboBox = DirectCast(sender, ComboBox)
MsgBox(cb.SelectedItem)
End Sub
And im calling this function or displaying this combobox in mainFrom.vb
file: mainFrom.vb
Dim l As New read
Me.Controls.Add(CType(l.Nav(), Control))
Now what i need is, a access to its control so i can parss value from mainFrom.vb to read.vb to manipulation the SelectionChangeCommitted activity.
I hope my question is clear....

Since you control is a simple ComboBox, I don't see the point of having common code to create this control. You can have a common function to populate it's contents though.
If you want to add functionality to the ComboBox, just create your own combo box class, let's say ExtendedComboBox, which inherits from ComboBox, and use it in your forms.
If you don't want to change your existing code, you can assign your ComboBox returned from Nav to a variable, and then hook into events from this ComboBox:
Public Class MainForm
'Declare NavBox as a form member
Private NavBox As ComboBox
Public Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Create NavBox
Dim read As New read
NavBox = read.Nav()
'Hook NavBox event so MainForm knows when selection is changed
AddHandler NavBox.SelectionChangeCommitted, AddressOf NavBox_SelectionChangedCommited
'Add NavBox to MainForm
Me.Controls.Add(NavBox)
End Sub
Public Sub NavBox_SelectionChangedCommited(ByVal sender As Object, ByVal e As System.EventArgs)
Dim cb As ComboBox = DirectCast(sender, ComboBox)
'Do something when selection changes.
End Sub
End Class
You should also remove the useless AddHandler line inside the Nav function.

Related

ContextMenuStrip Requires Two Right Clicks to Display

I like to create my contextmenu's programmatically. I generally do not add the items to the contextmenustrip until it is opening as the items that get displayed are dependent on other aspects of the design that are variable.
I have found that the contextmenustrips seem to require two right clicks to display. I've tried adding the menu items in different events (opening, opened, etc) and also manually setting the contextmenustrip's visibility to true to no avail.
I can't for the life of me figure out why two right clicks are necessary. If you create a blank winforms project and then replace all the code with this, it'll demonstrate the issue.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim currContextMenuStrip As New ContextMenuStrip
Me.ContextMenuStrip = currContextMenuStrip
AddHandler currContextMenuStrip.Opening, AddressOf ContextMenuStrip1_Opening
End Sub
Private Sub ContextMenuStrip1_Opening(sender As Object, e As CancelEventArgs)
Dim currContextMenuStrip As ContextMenuStrip = sender
Dim menuTxt As String = "&Find"
'only add the menu if it doesn't already exist
If (From f In currContextMenuStrip.Items Where f.text = menuTxt).Count = 0 Then
Dim newMenuItem As New ToolStripMenuItem
newMenuItem.Text = menuTxt
currContextMenuStrip.Items.Add(newMenuItem)
End If
End Sub
End Class
EDIT: Just figured out it seems to be connected to the fact that the contextmenustrip doesn't have any items on the first right click. If I add a dummy item, then hide it once other items are added, it works on the first right click. So confused!
This works:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim currContextMenuStrip As New ContextMenuStrip
Me.ContextMenuStrip = currContextMenuStrip
AddHandler currContextMenuStrip.Opening, AddressOf ContextMenuStrip1_Opening
'add a dummy item
Dim newMenuItem As New ToolStripMenuItem
newMenuItem.Text = "dummy"
currContextMenuStrip.Items.Add(newMenuItem)
End Sub
Private Sub ContextMenuStrip1_Opening(sender As Object, e As CancelEventArgs)
Dim currContextMenuStrip As ContextMenuStrip = sender
Dim menuTxt As String = "&Find"
'only add the menu if it doesn't already exist
If (From f In currContextMenuStrip.Items Where f.text = menuTxt).Count = 0 Then
Dim newMenuItem As New ToolStripMenuItem
newMenuItem.Text = menuTxt
currContextMenuStrip.Items.Add(newMenuItem)
End If
'hide the dummy item
Dim items As List(Of ToolStripMenuItem) = (From f As ToolStripMenuItem In currContextMenuStrip.Items Where f.Text = "dummy").ToList
items.First.visible = False
End Sub
End Class
If you really need to do things this way, one option is to create your own custom ContextMenuStrip that accounts for the behaviour when there are no items and the requirement for a dummy item. I used this code:
Imports System.ComponentModel
Public Class Form1
Private WithEvents menu As New ContextMenuStrip
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ContextMenuStrip = menu
End Sub
Private Sub menu_Opening(sender As Object, e As CancelEventArgs) Handles menu.Opening
If menu.Items.Count = 0 Then
menu.Items.AddRange({New ToolStripMenuItem("First"),
New ToolStripMenuItem("Second"),
New ToolStripMenuItem("Third")})
End If
End Sub
Private Sub menu_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles menu.ItemClicked
MessageBox.Show(e.ClickedItem.Text)
End Sub
End Class
and saw the same behaviour you describe. I then defined this class:
Public Class ContextMenuStripEx
Inherits ContextMenuStrip
Private dummyItem As ToolStripItem
Public ReadOnly Property IsInitialised As Boolean
Get
Return dummyItem Is Nothing
End Get
End Property
Public Sub New()
dummyItem = Items.Add(CStr(Nothing))
End Sub
''' <inheritdoc />
Protected Overrides Sub OnItemAdded(e As ToolStripItemEventArgs)
If Not IsInitialised Then
Items.Remove(dummyItem)
dummyItem = Nothing
End If
MyBase.OnItemAdded(e)
End Sub
End Class
and changed my form code to this:
Imports System.ComponentModel
Public Class Form1
Private WithEvents menu As New ContextMenuStripEx
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ContextMenuStrip = menu
End Sub
Private Sub menu_Opening(sender As Object, e As CancelEventArgs) Handles menu.Opening
If Not menu.IsInitialised Then
menu.Items.AddRange({New ToolStripMenuItem("First"),
New ToolStripMenuItem("Second"),
New ToolStripMenuItem("Third")})
End If
End Sub
Private Sub menu_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles menu.ItemClicked
MessageBox.Show(e.ClickedItem.Text)
End Sub
End Class
and it worked as desired. Note that the last code snippet uses the custom type and its custom property.
Thanks for all the help and suggestions! I ultimately decided to build the superset of menus in the Designer and then just show/hide at run time. That's probably faster on each right click then rebuilding the menu each time.
Microsoft has old style and new style context menus. The Popup event was used for the old style context menus and it received a plain EventArgs object. The new context menus use the Opening event which receives a CancelEventArgs object. If currContextMenuStrip.Items doesn't contain any items, e.Cancel will be set to True when the event handler is called (which caused the problem you encountered). The fix is to add the menu items and then set e.Cancel to False. It should display fine after that. To make sure items were actually added, the assignment of e.Cancel can be guarded with an if statement as follows:
If currContextMenuStrip.Items.Count <> 0 Then
e.Cancel = False
End If

How to trigger a gotfocus datagrid combobox event while grid is readonly?

I have a datagridview and I'm trying to get the gotfocus event for my comboboxcells to still call despite the datagrid being on readonly.
This gotfocus event changes the colour of every combobox with a similar value (including itself) and I would like to it to still call while on readonly (the datagridview comboboxes can still be selected/highlighted while on readonly but it is not called(?))
Below is the basics of how I am currently handling the events (for some reason this shortened version makes multiple event calls at an exponential rate & also requires three clicks to trigger... I'm not sure why it does this but my full project works fine in this regard)
Public Class Form1
Private Sub cell_gotfocus(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
Dim CurrentCombo = TryCast(e.Control, ComboBox)
If Not CurrentCombo Is Nothing Then
RemoveHandler CurrentCombo.GotFocus, AddressOf Combo_Focus 'remove event handler so we don't accumulate multiples
AddHandler CurrentCombo.GotFocus, AddressOf Combo_Focus 'add eventhandler
End If
End Sub
Private Sub Combo_Focus()
Console.WriteLine("Got Focus")
Console.beep
End Sub
Private Sub Make_Readonly_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Make_Readonly.Click
DataGridView1.Rows(0).ReadOnly = True 'toggle readonly
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For y As Integer = 1 To 4 'add extra columns into datagrid
Dim cmb As New DataGridViewComboBoxColumn
cmb.HeaderText = "c" & DataGridView1.Columns.Count
cmb.Name = "c" & DataGridView1.Columns.Count
cmb.FlatStyle = FlatStyle.Flat
DataGridView1.Columns.Add(cmb)
Next
End Sub
End Class

Get combobox selected from another form dynamically

I have a combobox in a form1 and a datagridview in another form2.
I want to get the combobox selected with a value from the datagridview in the second form
I use the code below in form2 and it works:
Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
form1.CBO_fournisseur.Text = DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString
Me.Close()
End Sub
But what I want to do is that the name of the form is passed dynamically to avoid using and IFELSE clause to enumerate all the forms I have in my project that use form2
Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
If formbon2.Name = "FRM_BN_RECEPTION_CUIR" Then
FRM_BN_RECEPTION_CUIR.CBO_fournisseur.Text = DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString
ElseIf formbon2.Name = "frm_reception_acc_provisoire" Then
frm_reception_acc_provisoire.CBO_1.Text = DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString
End If
Me.Close()
End Sub
I think I've got what you want to do. I strongly suggest you stop using the Form as a Shared resource.
Use a constructor like this in your Form2:
Private ParentFormCombo as Combobox
Public Sub New(ByVal pCmb as Combobox)
ParentFormCombo = pCmb
End Sub
Then in your doubleclick you just change the text of ParentFormCombo
ParentFormCombo.Text = DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString
Then you have to stop using:
FrmList_View.Show()
Now you should always use the constructor instead (New()). So do the following instead:
Dim f As New FrmList_View(CBO_fournisseur)
'or
Dim f As New FrmList_View(CBO_1)
f.Show()

Can I declare a dynamically adding new button globally?

I need help in my VB.NET project. I declare a dynamic textbox which is "dim textbox as new textbox()" inside the button and it will create a new textbox when I press button1 and I also put text on the new textbox created. Now when a dynamic textbox already created. When I press button2 I want to compare the text on the label to the text in dynamic textbox created but I got an error that says, OBJECT REFERENCE NOT SET AN INSTANCE OF AN OBJECT.
I suppose you create the textbox in the click event handler of the button. this way, you can only access it in this event handler method. In order to use it in the other event handler, you need to move the declaration to class level, e.g.:
Private txtBox As Textbox
Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
txtBox = New Textbox()
' ...
Me.Controls.Add(txtBox)
End Sub
Private Sub btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click
If txtBox IsNot Nothing Then
MsgBox(txtBox.Text)
End If
End Sub
Try it like this. You have to store the reference to your textbox in a variable, e. g. in the scope of the form instance.
Public Class Form1
Private dynTextbox As TextBox
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Me.dynTextbox = New TextBox
With Me.dynTextbox
.Top = Me.Button1.Top
.Left = Me.Button1.Right + 5
.Text = "test"
End With
Me.Controls.Add(Me.dynTextbox)
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
If Me.dynTextbox IsNot Nothing Then MessageBox.Show(Me.Label1.Text = Me.dynTextbox.Text)
End Sub
End Class
If you create multiple textboxes dynamically, you may store them in some Array or List (e. g. in a List(Of TextBox)) and then you'll have to find a way to reference the one you need, that depends on your specific project.

in vb.net how do I declare a public variable from a private sub

I am creating Labels dynamically from a private sub, and I want to be able to do something when the user clicks on them. However, I can't use "Dim withEvents blah..." because it says withEvents can't be used on a local variable but I also can't use "Public withEvents blah" from within my Private Sub. How do I accomplish this?
Thanks.
When you create dynamic control, you can add a handler for it
Dim mylbl As New Label
mylbl.Name = "button1"
mylbl.Text = "hi"
Me.Controls.Add(mylbl)
AddHandler lbl.Click, AddressOf AllLabels_Click
This is your Handler Sub
Sub AllLabels_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim lbl As Label = CType(sender, Label)
MsgBox(lbl.Text)
End Sub