I'm creating a ToolStripMenuItem's DropDownItems at runtime.
Me.mnuExtrasSecondaryLCID.DropDownItems.Clear()
Dim iCount As Integer = -1
For Each nLang As clsLanguage In g_LCIDs
If nLang.IsLeader Then
iCount += 1
Dim n As New ToolStripMenuItem
n.Name = "mnuSecondaryLCID" & iCount.ToString()
n.Text = nLang.Title
n.Tag = nLang.LCID
n.Available = True
n.CheckOnClick = True
Me.mnuExtrasSecondaryLCID.DropDownItems.Add(n)
AddHandler n.Click, AddressOf Me.SecondaryLCIDClick
End If
Next
This works fine.
When I then check one of the DropDownItems at runtime, any other DropDownItems in the same "list" stay checked.
I would instead like to have only one checked (=the last clicked one).
Is there a property that would let me do this automatically, or do I need to code this by unchecking all other DropDropItems manually?
You need to code it manually. When clicking on a sub menu, uncheck all other siblings:
For index = 1 To 5
Dim subMenu = New ToolStripMenuItem(index.ToString())
subMenu.CheckOnClick= True
AddHandler subMenu.Click, Sub(obj, arg)
Dim item = DirectCast(obj, ToolStripMenuItem)
For Each sibling In item.Owner.Items.OfType(Of ToolStripMenuItem).Except({obj})
sibling.Checked = False
Next sibling
End Sub
Menu1ToolStripMenuItem.DropDownItems.Add(subMenu)
Next
Related
I have a ContextMenuStrip called: DGVContextStrip its displayed when the user right clicks on my datagridview.
That MenuStrip contains an item called AddUpgradeTagToolStripMenuItem
which contains sub items(dropdownitems), these sub items are all named with a number in their name.
eg: Add1ToolStripMenuItem, Add2ToolStripMenuItem, Add3ToolStripMenuItem.... and so on until Add25ToolStripMenuItem.
When a user right clicks, on the Datagridview, I want to check if a cell contains the number "1" then if it does make Add1ToolStripItem.checked = true
I figured I would loop through the numbers 1 to 25, and in each loop check if the cell contains 1 and if true, change the checked value of the menu item. something like...
For i = 1 to 25
If DataGridView1.SelectedRows(0).Cells("Text_Field").Value.ToString.Contains(i) then
CType("Add" & i & "ToolStripMenuItem", ToolStripMenuItem).Checked = True
Next
but this doesn't work, iv seen examples online that use the control.find method but i couldn't get that to work for my use.
for example
Dim ControlName As String = "Add" & i & "ToolStripMenuItem"
CType(Me.Controls.Find(ControlName, True), ToolStripMenuItem).Checked = True
any ideas how I get this to work? I realise I could have used 25 if then else statements but I kind of wanted to keep the code far neater.
The ToolStripItem is not a control to search for one in a Control.ControlCollection. You need to search a ToolStripItemCollection where it belongs.
Just like the Control.ControlCollection.Find method, the ToolStripItemCollection.Find method can perform a deep search for an item.
Examples for your case:
Dim itemName As String = $"Add{i}ToolStripMenuItem"
Dim tsmi = yourContextMenuStrip.Items.
Find(itemName, True).
OfType(Of ToolStripMenuItem).
FirstOrDefault()
If tsmi IsNot Nothing Then
tsmi.Checked = True
End If
Alternatively, if you already know that the target item is one of the AddUpgradeTagToolStripMenuItem drop down items, then you can do:
Dim itemName As String = $"Add{i}ToolStripMenuItem"
Dim tsmi = DirectCast(AddUpgradeTagToolStripMenuItem, ToolStripMenuItem).
DropDownItems.OfType(Of ToolStripMenuItem).
FirstOrDefault(Function(x) x.Name.Equals(itemName, StringComparison.OrdinalIgnoreCase))
If tsmi IsNot Nothing Then
tsmi.Checked = True
End If
In case you need to check only one item from the collection:
Dim itemName As String = $"Add{i}ToolStripMenuItem"
For Each tsmi In DirectCast(AddUpgradeTagToolStripMenuItem, ToolStripMenuItem).
DropDownItems.OfType(Of ToolStripMenuItem)
If tsmi.Name.Equals(itemName, StringComparison.OrdinalIgnoreCase) Then
tsmi.Checked = True
Else
tsmi.Checked = False
End If
Next
I have been searching for a method to group multiple objects to change a common value but have not been successful. I have been forced to do things like this:
Label10.Visible = True
Label11.Visible = True
Label12.Visible = True
Label13.Visible = True
Label14.Visible = True
RectangleShape8.Visible = True
RectangleShape9.Visible = True
RectangleShape10.Visible = True
RectangleShape11.Visible = True
Ect, Is there a method to group, or declare multiple objects to refer to all of them at the same time? I have attempted to declare but i was unsuccessful. Thanks for your help in advanced.
You can use the following:
Dim RectangleShapeGroup() As String = {"RectangleShape8", "RectangleShape9", "RectangleShape10", "RectangleShape11"}
Dim LabelGroup() As String = {"Label10", "Label11", "Label12", "Label13", "Label14"}
For Each ctrl As Control In Me.Controls
If Array.IndexOf(RectangleShapeGroup, ctrl.Name) > -1 Or Array.IndexOf(LabelGroup, ctrl.Name) > -1 Then
ctrl.Visible = True
End If
Next
In case you want to show all Label controls you can use the following:
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is Label Then
ctrl.Visible = True
End If
Next
... or all controls with names starting with Label or RectangleShape:
For Each ctrl As Control In Me.Controls
If ctrl.Name.StartsWith("Label") Or ctrl.Name.StartsWith("RectangleShape") Then
ctrl.Visible = True
End If
Next
Just add your controls to List(Of ControlType) then loop through to change any properties. This code is for Windows Forms. If this is a different type of application plead indicate that in your question.
Private lstLabels As New List(Of Label) From {Label10, Label11, Label12, Label13, Label14}
Private Sub MakeLabelsVisible()
For Each l In lstLabels
l.Visible = True
Next
End Sub
This is one of the form, all the usercontrol value in this form will store in My.Settings
I have another form with a FlowLayoutPanel, everytime when the application start,
if Active checked then it will add a Button with discount value to the FlowLayoutPanel.
Should I add those usercontrol to a list and then loop through the list? Or what is the best way to solve this kind of problem?
UPDATED
How can I add multiple item to list in 1 code? I getting this error when system run to line 5
An exception of type 'System.NullReferenceException' occurred in XXX.exe but was not handled in user code
Additional information: Object reference not set to an instance of an object.
Public Sub RefreshDiscount(ByRef ref As scr_mainDiscount)
Dim li_disName As New List(Of TextBox)
Dim li_disValue As New List(Of TextBox)
Dim li_disType As New List(Of ComboBox)
Dim li_active As New List(Of CheckBox)
Dim tb_disName As TextBox() = {ref.tb_name1, ref.tb_name2, ref.tb_name3, ref.tb_name4, ref.tb_name5, ref.tb_name6, ref.tb_name7, ref.tb_name8, ref.tb_name9, ref.tb_name10}
Dim tb_disValue As TextBox() = {ref.tb_value1, ref.tb_value2, ref.tb_value3, ref.tb_value4, ref.tb_value5, ref.tb_value6, ref.tb_value7, ref.tb_value8, ref.tb_value9, ref.tb_value10}
Dim cb_disType As ComboBox() = {ref.cb_type1, ref.cb_type2, ref.cb_type3, ref.cb_type4, ref.cb_type5, ref.cb_type6, ref.cb_type7, ref.cb_type8, ref.cb_type9, ref.cb_type10}
Dim chkb_active As CheckBox() = {ref.CheckBox1, ref.CheckBox2, ref.CheckBox3, ref.CheckBox4, ref.CheckBox5, ref.CheckBox6, ref.CheckBox7, ref.CheckBox8, ref.CheckBox9, ref.CheckBox10}
li_disName.AddRange(tb_disName)
li_disValue.AddRange(tb_disValue)
li_disType.AddRange(cb_disType)
li_active.AddRange(chkb_active)
For index As Integer = 0 To li_active.Count - 1
If li_active(index).Checked = False Then
li_disName.RemoveAt(index)
li_disValue.RemoveAt(index)
li_disType.RemoveAt(index)
li_active.RemoveAt(index)
Else
Dim btn As New ctrl_DiscountButton
With btn
.Text = li_disName(index).Text
.Price = li_disValue(index).Text
.Type = li_disType(index).Text
End With
scr_sales.flp_discount.Controls.Add(btn)
End If
Next
li_disName.Clear()
li_disValue.Clear()
li_disType.Clear()
li_active.Clear()
End Sub
Here's a simple example showing how to find CheckBox1 thru CheckBox10, "by name", using the "searchAllChildren" option of Controls.Find():
For i As Integer = 1 To 10
Dim ctlName As String = "CheckBox" & i
Dim matches() As Control = Me.Controls.Find(ctlName, True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is CheckBox Then
Dim cb As CheckBox = DirectCast(matches(0), CheckBox)
' do something with "cb"
If cb.Checked Then
' ... code ...
' possibly use code just like this to find the matching discount value control?
End If
End If
Next
This is code am trying to solve
Dim i As Integer
i = ListBox1.SelectedIndex
If ListBox1.SelectedIndex = 0 Then
TextBox10.Enabled = False
TextBox25.Enabled = False
TextBox30.Enabled = False
TextBox40.Enabled = False
TextBox55.Enabled = False
TextBox65.Enabled = False
TextBox73.Enabled = False
TextBox84.Enabled = False
TextBox95.Enabled = False
TextBox100.Enabled = False
TextBox185.Enabled = False
Else
TextBox(??).Enabled = True
End If
How to other textboxes to enable True?
I mean
10,25,30,40... 185 are false and all other textboes Enabled true ?
You can find control of type textbox and assign default enabled= true and then write your code to set enabled=false.
For Each c As Control In Me.Controls
If c.GetType Is GetType(TextBox) Then
c.Enabled=true
End If
Next
OR
Dim allTextBox As New List(Of Control)
For Each c As TextBox In FindControl(allTextBox, Me,GetType(TextBox))
c.Enabled=true
Next
Public Shared Function FindControl(ByVal list As List(Of Control), ByVal parent As Control, ByVal ctrlType As System.Type) As List(Of Control)
If parent Is Nothing Then Return list
If parent.GetType Is ctrlType Then
list.Add(parent)
End If
For Each child As Control In parent.Controls
FindControl(list, child, ctrlType)
Next
Return list
End Function
I'd go with two lists:
var falseList = new List<ListBox> { TextBox10, TextBox25, TextBox30, ... };
var trueList = new List<ListBox> { TextBox1, TextBox2, TextBox3, ... };
Then in each condition block in the if enumerate each list and set false:
foreach(var box in falseList) box.Enabled = false;
foreach(var box in trueList) box.Enabled = true;
Another option is to setup a DataSource with two booleans and set DataBinding on each of the true ListBoxes to one of the properties and the other set to the false property then setting the found property on on the datasource they all update in one statement.
Note: Sorry it's c# syntax but it's really close to VB.NET. I just don't know the exact VB syntax
You can iterate over the forms Controls collection. Something like:
Dim i As Integer
i = ListBox1.SelectedIndex
for each c in me.controls
if TypeOf c is TextBox then
dim Textbox as textbox = DirectCast(c, TextBox)
'Determine state of your textbox here.
'Perhaps use the TAG property to assign a selectedindex to each textbox
Textbox.enabled = (Textbox.Tag.ToString() <> i.ToString())
end if
next
I'm very new to programming and vb.net, trying to self teach more so as a hobby, as I have an idea for a program that I would find useful, but I am having trouble getting past this issue and I believe it is to do with the timer.
I have a form of size.(600,600) with one button of size.(450,150) that is set location(100,50) on the form. When clicked I want to move down it's own height, then add a new button in it's place. The code included below works as desired for the first two clicks, but on the third click the button keeps moving and the autoscroll bar extends. I initially thought it was the autoscroll function or the location property, but realised that as the button keeps moving, the timer hasn't stopped. I am aware that the code is probably very clunky in terms of achieving the outcome, and that there are a few lines/variables that are currently skipped over by the compiler (these are from older attempts to figure this out).
I have looked around and can't find the cause of my problem. Any help would be greatly appreciated. Apologies if the code block looks messy - first go.
Public Class frmOpenScreen
Dim intWButtons, intCreateButtonY, intCreateButtonX 'intTimerTick As Integer
Dim arrWNames() As String
Dim ctrlWButtons As Control
Dim blnAddingW As Boolean
Private Sub btnCreateW_Click(sender As System.Object, e As System.EventArgs) Handles btnCreateW.Click
'Creates new Button details including handler
Dim strWName, strWShort As String
Dim intCreateButtonY2 As Integer
Static intNumW As Integer
Dim B As New Button
strWName = InputBox("Please enter the name name of the button you are creating. Please ensure the spelling is correct.", "Create W")
If strWName = "" Then
MsgBox("Nothing Entered.")
Exit Sub
End If
strWShort = strWName.Replace(" ", "")
B.Text = strWName
B.Width = 400
B.Height = 150
B.Font = New System.Drawing.Font("Arial Narrow", 21.75)
B.AutoSizeMode = Windows.Forms.AutoSizeMode.GrowAndShrink
B.Anchor = AnchorStyles.Top
B.Margin = New Windows.Forms.Padding(0, 0, 0, 0)
'Updates Crucial Data (w name array, number of w buttons inc Create New)
If intNumW = 0 Then
ReDim arrWNames(0)
Else
intNumW = UBound(arrWNames) + 1
ReDim Preserve arrWNames(intNumW)
End If
arrWNames(intNumW) = strWShort
intNumW = intNumW + 1
intWButtons = WButtonCount(intWButtons) + 1
'updates form with new button and rearranges existing buttons
intCreateButtonY = btnCreateW.Location.Y
intCreateButtonX = btnCreateW.Location.X
‘intTimerTick = 0
tmrButtonMove.Enabled = True
‘Do While intTimerTick < 16
‘ 'blank to do nothing
‘Loop
'btnCreateW.Location = New Point(intCreateButtonX, intCreateButtonY + 150)
B.Location = New Point(intCreateButtonX, intCreateButtonY)
Me.Controls.Add(B)
B.Name = "btn" & strWShort
intCreateButtonY2 = btnCreateW.Location.Y
If intCreateButtonY2 > Me.Location.Y Then
Me.AutoScroll = False
Me.AutoScroll = True
Else
Me.AutoScroll = False
End If
'MsgBox(intCreateButtonY)
End Sub
Function WButtonCount(ByRef buttoncount As Integer) As Integer
buttoncount = intWButtons
If buttoncount = 0 Then
Return 1
End If
Return buttoncount
End Function
Public Sub tmrButtonMove_Tick(sender As System.Object, e As System.EventArgs) Handles tmrButtonMove.Tick
Dim intTimerTick As Integer
If intTimerTick > 14 Then
intTimerTick = 0
End If
If btnCreateW.Location.Y <= intCreateButtonY + 150 Then
btnCreateW.Top = btnCreateW.Top + 10
End If
intTimerTick += 1
If intTimerTick = 15 Then
tmrButtonMove.Enabled = False
End If
End Sub
End Class
So my current understanding is that the tick event handler should be increasing the timertick variable every time it fires, and that once it has hits 15 it should diable the timer and stop the button moving, but it is not doing so.
Thanks in advance.
IntTimerTick is initialized to 0 at the beginning of every Tick event. This won't happen if you declare it to be static:
Static Dim intTimerTick As Integer