Changing the alignment of dynamic labels - vb.net

I have made a program that deals with labels' text alignment.
I have used:
Dim con7 As Control
For Each con7 In Me.Controls
con7.TextAlign = 'whatever is needed
Next
But the problem is the TextAlign part. The problem that arises is that TextAlign is not a property of all controls. What is the best way to fix this?

You're trying to assign each child control to a variable of type Label but, as the error message indicates, you clearly have at least one control that is not a Label. There are a couple of ways you can handle that but the simplest is to use Me.Controls.OfType(Of Label)(), which will filter out all but Label controls.

You don't need the following code:
Dim con7 As Control
con7 is declared within the loop.
Since you only want to loop through labels use the following code:
For Each lbl In Me.Controls.OfType(Of Label)()
lbl.TextAlign = ...
Next

Related

Word CheckBox ContentContol OnChange Event

I try to make a word document with two checkboxes where each checkbox will show/hide a part of the document with a custom style.
I plan to set value Style.Font.Hidden = True/False depending from checkbox values, but...
I found that there are 3 types of controls:
Legacy Controls - This seems old the ugly.
ActiveX Controls - I can easyly attach to checkbox onChange events, but these are also ugly and I think it's not that secure, also this is probably now working on mac.
ContentControls - This seems like the right way to do this, but I just can't attach to the right event. (Also there is some XML attachment described, but I'm not using this, this seem too complicated, I don't know.)
Can you tell me how to atach to onChange event of CheckBox ContentContol? I need the same behaviour like it's ActiveX CheckBox.
Content Controls do not have "onChange" events, so you can't get a content control to behave like the ActiveX checkbox in a simple manner. Similarly to form fields, the code for ContentControls fires when the control is entered/exited.
The only way to emulate "onChange" for a Content Control is to link the content control to a node in a CustomXMLPart in the document then work with the Document_ContentControlBeforeStoreUpdate event that triggers when the content of the node in the CustomXMLPart is going to be changed.
If, as your question indicates, this is too complex for your purposes you could use a MacroButton field that displays a font character (Symbol) that looks like a checkbox. Clicking the field would exchange that character for a different one, that looks checked. And the reverse again for the next click. Here's some sample code to get you started. If you don't like the checkboxes I chose, you can pick something else from Insert/Symbols/Symbol. Just change the character numbers and the font name.
By default a MacroButton field triggers on double click. You can change this to a single click when the document is opened in an AutoOpen macro.
Sub AutoOpen()
Application.Options.ButtonFieldClicks = 1
End Sub
Sub ToggleCheckBox()
Dim iNotChecked As Integer, iChecked As Integer
Dim rngCheck As word.Range
Dim sBkmName As String, sFontName as String
iNotChecked = 111
iChecked = 253
sBkmName = "bkmCheck"
sFontName = "Wingdings"
Set rngCheck = ActiveDocument.Bookmarks(sBkmName).Range
If Asc(rngCheck.Text) = iNotChecked Then
rngCheck.Text = Chr(iChecked)
ActiveDocument.Bookmarks.Add sBkmName, rngCheck
rngCheck.Font.Name = sFontName
ElseIf Asc(rngCheck.Text) = iChecked Then
rngCheck.Text = Chr(iNotChecked)
rngCheck.Font.Name = sFontName
ActiveDocument.Bookmarks.Add sBkmName, rngCheck
End If
End Sub

Adapt label width to text

I basically programmatically generate many labels with a text read from a text file so I would like to know how I can programmatically adapt the width of a Label to its text.
Controls come in two flavors. The ActiveX version of a label has an AutoSize property. For example, with a ActiveX label control named Label1 in Sheet1
Private Sub test()
Sheet1.Label1.WordWrap = False
Sheet1.Label1.AutoSize = True
Sheet1.Label1.Caption = "This is a lot of text to put in a label"
End Sub
will automatically adjust the width to fit.

Set a group of controls visible with one line of code?

Is it possible to clump a group of controls together and be able to set it visible with one line rather than having to do each individual control's .visible property? I know it doesn't hurt anything but would like to keep it looking neat and not clump up a function with a page full of .visible control calls.
Just group your controls in a List(Of Control) or an array and set the Visible property using either the ForEach-method or a simple For Each-loop.
e.g.:
Dim toToggle = {OkButton, CancelButton, ControlPanel, SelectionComboBox}
For Each ctrl in toToggle
ctrl.Visible = False
Next
or
Dim toToggle = {OkButton, CancelButton, ControlPanel}.ToList()
toToggle.ForEach(Sub(c) c.Visible = False)
I like Dominic's solution. Another approach (and this depends on how your Winform is laid out) would be to group the controls into a panel:
For Each ctrl as Control in MyPanel.Controls
c.Visible = False
Next
Really all this approach does is keeps you from having to create a new list, but maybe that would be better so you can choose precisely which controls to add.

Invalid Conversion error

I recently upgraded a VB 6 project to .net. I'm having a problem with this block of code:
Dim CtrlName As System.Windows.Forms.MenuItem
For Each CtrlName In Form1.Controls
'Some code here
Next CtrlName
Now this code compiles but throws the following runtime error:
Unable to cast object of type 'System.Windows.Forms.Panel' to type 'System.Windows.Forms.MenuItem.
I have a panel control on the subject form. How do I resolve this?
Thanks.
You are iterating over all controls that are directly inside the form, not just the MenuItems. However, your variable is of type MenuItem. This is causing the problem.
For normal controls (e.g. Buttons), you’d want to use the following, easy fix; test inside the loop whether the control type is correct:
For Each control As Control In Form1.Controls
Dim btt As Button = TryCast(control, Button)
If btt IsNot Nothing Then
' Perform action
End If
Next
However, this does not work for MenuItems since these aren’t controls at all in WinForms, and they aren’t stored in the form’s Controls collection.
You need to iterate over the form’s Menu.MenuItems property instead.
The items in the Controls property of a form, which may or may not be MenuItem. Assuming that you just want to iterate over MenuItem objects you can change your code to:
For Each menuControl As MenuItem In Me.Controls.OfType(Of MenuItem)
' Some code
Next
Note that the menuControl variable is declared in the For so is only accessible within the block and is disposed automatically.
for each ctrl as control in me.controls
if typeof ctrl is menuitem then
' do stuff here
end if
next
typeof keyword allows you to test the type of control being examined in the control collection.
Found the answer after a bit of research, you need to search for the menu strip first and then loop through the items collection.
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is MenuStrip Then
Dim mnu As MenuStrip = DirectCast(ctrl, MenuStrip)
For Each x As ToolStripMenuItem In mnu.Items
Debug.Print(x.Name)
Next
End If
Next

Listview in vb.net

Is it possible to display the contents of a two dimensional array vertically on a form in vb.net using listview, and if so how would I do it?
So, if my array is declared as
dim myarray (2,10) how would I display the contents vertically in listview. All and any help much apprciated, thank you.
This method should do the trick for you (I have assumed we are talking about a winforms app, but I realize that it could just as well be an ASP.NET app, in which case my answer might no longer be applicable):
Private Sub ShowArrayInListView(ByVal listView As ListView, ByVal dataArray As String(,))
listView.Items.Clear()
For y As Integer = dataArray.GetLowerBound(1) To dataArray.GetUpperBound(1)
Dim lvi As New ListViewItem
For x As Integer = dataArray.GetLowerBound(0) To dataArray.GetUpperBound(0)
If x = 0 Then
lvi.Text = dataArray(x, y)
Else
lvi.SubItems.Add(dataArray(x, y))
End If
Next
listView.Items.Add(lvi)
Next
End Sub
Every control like ListView in .NET has a marvelous template mechanism what you can use to put HTML inside of it. Moreover you can handle ItemDataBound event and fill a Label located inside the ItemTemplate section of your control with HTML code. Then, use a nested loop to generate your <tr> and <td>, place them on a string and assign it to the Label.Text property.
Hope that helps,