How do I show multiple legends on a VB.Net Chart - vb.net

My code creates a MS Chart with one or more ChartAreas and I want to have a separate legend for each ChartArea
My code is as below:
For tblNum = 0 To (dsData.Tables.Count - 1)
tblName = dsData.Tables(tblNum).TableName
caTag = dtReport.Rows(tblNum).Item("caTag")
If (dtReport.Rows(tblNum).Item("CAnum") > CAnum) Then
CAnum = dtReport.Rows(tblNum).Item("CAnum")
myChart.ChartAreas.Add(caTag)
myChart.Legends.Add(caTag)
With myChart.Legends(caTag)
.Docking = Docking.Bottom
.DockedToChartArea = caTag
.IsDockedInsideChartArea = False
.TableStyle = LegendTableStyle.Wide
.Alignment = StringAlignment.Center
.Enabled = True
End With
If (caCount > 1) Then
CAindex += 1
myChart.Titles.Add(caTag)
With myChart.Titles(CAindex)
.Docking = Docking.Top
.DockedToChartArea = caTag
.IsDockedInsideChartArea = False
.Alignment = ContentAlignment.TopCenter
.Font = New System.Drawing.Font("Times New Roman", 14, FontStyle.Bold)
.Text = caTag
End With
End If
Else
myChart.Legends(caTag).Enabled = True
End If
However when I run the code only a single Legend is visible containing all series from both ChartAreas as below:
Legend-1.jpg
Can anyone help me please ?

I have found the answer to my own question.
I needed to assign the correct legend to each series as follows:
myChart.Series(tblName).Legend = caTag
Now both legends are shown with the appropriate series names

Related

Panel control stuck on visible=false

Im using vb2008 express edition.
I have a form with panel control set to visible=false( i add it for copy it properties).
I tried to add panels dynamically but when i do it, i cant see it on the form.
I even set code line for panel.visble=true but when i run the watch window it stuck on visible =false
Sub addPlayer(ByVal p As Form2.player)
' Timer1.Enabled = False
Dim gap As Integer = 20
Dim pan123 As New Panel
Dim nick As New Label, colorFrame As New Label
pan123.Size = New Size(100, 100)
pan123.BackColor = Color.AliceBlue
pan123.ForeColor = Color.Aquamarine
pan123.Location = New Point(200, 200)
'pan.Size = Panel1.Size
'pan.Location = Panel1.Location
'pan.Top = Panel1.Top + Panel1.Height * playersNum + gap * playersNum
nick.Text = p.nick
nick.Size = Label2.Size
nick.Location = Label2.Location
nick.Font = Label2.Font
nick.AutoSize = True
colorFrame.AutoSize = False
colorFrame.Size = Label3.Size
colorFrame.Location = Label3.Location
colorFrame.BackColor = p.colorP
pan123.Visible = True
'pan.Controls.Add(nick)
'pan.Controls.Add(colorFrame)
Me.Controls.Add(pan123)
'nick.Visible = True
playersNum = playersNum + 1
End Sub

Unexpected TableLayoutPanel Behavour

Hi having some issues with some nested tablelayoutpanels in a custom control
I have a tablelayoutpanel in a tablelayoutpanel in a tablelayoutpanel Crazy i know but it keeps it uniform and ordered
The custom control consists of 2 tablelayoutpanels that is placed in a table layout panel on a form and in a preview area of my main form.
Having them set at design time works fine but dynamically adding / removing the topmost ones with 1 row 2 columns into the one below it that has 1 column and x rows only Seems to break the autosizing behavior I'm chasing.
So I want the cells & rows to resize automatically based on the contents in this case labels but still remain in a neat ordered layout
There's no docking anywhere in the hierarchy of controls just anchors here and there
Here's my code to add the tablelayoutpanels below
"https://i.stack.imgur.com/vRfhE.png"
Private Sub AddControl(ByRef Counter As Counter)
Dim Gpanel As New TableLayoutPanel
Dim tlabel As New Label
Dim clabel As New Label
Dim pad As Integer = Counter.Cpad
TLPanel.AutoSizeMode = AutoSizeMode.GrowAndShrink
Gpanel.AutoSizeMode = AutoSizeMode.GrowAndShrink
Gpanel.AutoSize = True
Gpanel.GrowStyle = TableLayoutPanelGrowStyle.AddRows
Gpanel.BorderStyle = BorderStyle.FixedSingle
Gpanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset
TLPanel.BorderStyle = BorderStyle.FixedSingle
Gpanel.Dock = DockStyle.None
Gpanel.Padding = New Padding(0)
Gpanel.Anchor = AnchorStyles.Top Or AnchorStyles.Left
Gpanel.BackColor = Color.Transparent
Gpanel.RowCount = 1
Gpanel.ColumnCount = 2
Gpanel.RowStyles.Add(New RowStyle(SizeType.AutoSize))
Gpanel.ColumnStyles.Add(New ColumnStyle(SizeType.AutoSize))
Gpanel.ColumnStyles.Add(New ColumnStyle(SizeType.AutoSize))
tlabel.Dock = DockStyle.None
clabel.Dock = DockStyle.None
tlabel.GetPreferredSize(Size.Empty)
clabel.GetPreferredSize(Size.Empty)
tlabel.Text = Counter.Clabel
clabel.Text = Counter.Ccount.ToString
tlabel.Padding = New Padding(pad)
clabel.Padding = New Padding(pad)
tlabel.Anchor = AnchorStyles.None
clabel.Anchor = AnchorStyles.None
tlabel.ForeColor = Color.FromName(Counter.Clcolor)
clabel.ForeColor = Color.FromName(Counter.Ccolor)
Dim fontstyle As New FontStyle
fontstyle = Counter.ClfontStyle
tlabel.Font = New Font(Counter.Clfont, Counter.Clfontsize, fontstyle)
fontstyle = Counter.CcfontStyle
clabel.Font = New Font(Counter.Ccfont, Counter.Ccfontsize, fontstyle)
Gpanel.Controls.Add(tlabel, 0, 0)
Gpanel.Controls.Add(clabel, 1, 0)
TLPanel.Controls.Add(Gpanel, 0, Counter.ID)
TLPanel.RowCount += 1
TLPanel.RowStyles.Add(New RowStyle(SizeType.AutoSize))
Dim Styles As TableLayoutRowStyleCollection = TLPanel.RowStyles
Dim Cstyles As TableLayoutColumnStyleCollection = Gpanel.ColumnStyles
Gpanel.RowStyles.Item(0) = New RowStyle(SizeType.AutoSize)
TLPanel.ColumnStyles.Item(0) = New ColumnStyle(SizeType.AutoSize)
For i = 0 To Cstyles.Count - 1
Cstyles.Item(i) = New ColumnStyle(SizeType.AutoSize)
Next
For i = 0 To Styles.Count - 1
Styles.Item(i) = New RowStyle(SizeType.AutoSize)
Next
TLPanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset
AutoSize = True
AutoSizeMode = AutoSizeMode.GrowAndShrink
End Sub
'

displayindex datatable/dgv columns not working

I set up some datatables via the Visual Studio table/columns collection editor. Sadly, it appears MS didn't include any way to re-order the data columns in the collection editor once you've done (no up/down facility). Thus, I'm having to programmatically re-order my columns (as out of order in the collection).
Done a lot of reading on this and for the life of me, can't locate the source of the problem. Basically, despite the code below, the datacolumns still show out-of-order on the datagridview. Code (abridged):
Private Sub LoadTextStylesDGV()
With _TextStylesDGV
.DefaultCellStyle.Padding = New Padding(0, 5, 0, 5)
.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
.AllowUserToAddRows = False
.AllowUserToDeleteRows = False
.AllowUserToResizeColumns = True
.Columns.Add(New DataGridViewComboBoxColumn With {.DataPropertyName = "TextAlign", .Visible = True,
.Name = "TextAlign",
.HeaderText = "TextAlign",
.DataSource = MarqueeEditor._EnumDDContentAlign,
.FlatStyle = FlatStyle.Flat,
.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells})
' And other column setups similar to above
' ....
.DataSource = Global.CPWBAdmin.MarqueeEditor.MarqueeData.Tables("TextStyles")
.MultiSelect = False
.AllowUserToDeleteRows = False
With .Columns("ID")
.ReadOnly = True
.DefaultCellStyle.BackColor = Color.LightGray
.Visible = False
.DisplayIndex = 0
.Frozen = True
End With
With .Columns("Name")
.DisplayIndex = 1
.Frozen = True
.DefaultCellStyle.BackColor = Color.Linen
End With
.Columns("RandomStyle?").DisplayIndex = 2
With .Columns("Font")
.ReadOnly = True
.DefaultCellStyle.BackColor = Color.LightBlue
.AutoSizeMode = DataGridViewAutoSizeColumnMode.None
.DisplayIndex = 3
End With
'.... other column display index settings
.Columns("Stroke3LineJoin").DisplayIndex = 49
.Columns("Stroke3Wrap").DisplayIndex = 50
.Columns("TextAutoFit").DisplayIndex = 51
'Sort:
.Sort(_TextStylesDGV.Columns("Name"), System.ComponentModel.ListSortDirection.Ascending)
End With
End Sub
I'm wondering whether the DGV columns are getting re-ordered elsewhere in the code. Of course - no way to check this with step-by-step debugging as happens before form show. Have trawled through the code but can't see anything obvious. Does anyone have any ideas as this one is killing me! If only MS had allowed column collection re-ordering. :(
Oooops. Didn't search hard enough! Finally found the solution:
dgvReservedStalls.AutoGenerateColumns = True
dgvReservedStalls.DataSource = clsReservedStalls.LoadDataTable()
dgvReservedStalls.AutoGenerateColumns = False
auto generates the columns from the DT, then frees it for re-ordering the columns. A little obscure, MS... :)
From here

Programmatically change "Don't add space between paragraphs of the same style"

I'm trying to programmatically change "Don't add space between paragraphs of the same style." To approach the problem, I recorded a macro during which I opened the Paragraph dialog box (Page Layout > Paragraph), checked the checkbox (don't add space) and a macro during which I unchecked the checkbox (add space). Neither affects "Don't add space between paragraphs of the same style" . . . and they have identical code:
Sub AddSpaceBetweenParagraphsOfSameStyle()
'
' AddSpaceBetweenParagraphsOfSameStyle Macro
' Add space between paragraphs of the same style.
'
With Selection.ParagraphFormat
.LeftIndent = InchesToPoints(0.5)
.RightIndent = InchesToPoints(0)
.SpaceBefore = 12
.SpaceBeforeAuto = False
.SpaceAfter = 12
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceMultiple
.LineSpacing = LinesToPoints(1)
.Alignment = wdAlignParagraphLeft
.WidowControl = True
.KeepWithNext = False
.KeepTogether = False
.PageBreakBefore = False
.NoLineNumber = False
.Hyphenation = True
.FirstLineIndent = InchesToPoints(-0.25)
.OutlineLevel = wdOutlineLevelBodyText
.CharacterUnitLeftIndent = 0
.CharacterUnitRightIndent = 0
.CharacterUnitFirstLineIndent = 0
.LineUnitBefore = 0
.LineUnitAfter = 0
.MirrorIndents = False
.TextboxTightWrap = wdTightNone
End With
End Sub
Sub RemoveSpaceBetweenParagraphsOfSameStyle()
'
' RemoveSpaceBetweenParagraphsOfSameStyle Macro
' Remove space between paragraphs of the same style.
'
With Selection.ParagraphFormat
.LeftIndent = InchesToPoints(0.5)
.RightIndent = InchesToPoints(0)
.SpaceBefore = 12
.SpaceBeforeAuto = False
.SpaceAfter = 12
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceMultiple
.LineSpacing = LinesToPoints(1)
.Alignment = wdAlignParagraphLeft
.WidowControl = True
.KeepWithNext = False
.KeepTogether = False
.PageBreakBefore = False
.NoLineNumber = False
.Hyphenation = True
.FirstLineIndent = InchesToPoints(-0.25)
.OutlineLevel = wdOutlineLevelBodyText
.CharacterUnitLeftIndent = 0
.CharacterUnitRightIndent = 0
.CharacterUnitFirstLineIndent = 0
.LineUnitBefore = 0
.LineUnitAfter = 0
.MirrorIndents = False
.TextboxTightWrap = wdTightNone
End With
End Sub
The code produced by the macro recorder is long, so I reduced it to a minimal version that I've verified also fails to affect "Don't add space between paragraphs of the same style":
Sub AddSpaceBetweenParagraphsOfSameStyle()
'
' AddSpaceBetweenParagraphsOfSameStyle Macro
' Add space between paragraphs of the same style.
'
End Sub
Sub RemoveSpaceBetweenParagraphsOfSameStyle()
'
' RemoveSpaceBetweenParagraphsOfSameStyle Macro
' Remove space between paragraphs of the same style.
'
End Sub
I looked at the documentation for ParagraphFormat and searched for a relevant property but found nothing that works. How can I programmatically change "Don't add space between paragraphs of the same style"?
This property is connected with Style, not with Paragraph (which suggests window title where you set this property). This is code which you look for:
ActiveDocument.Styles("Normal").NoSpaceBetweenParagraphsOfSameStyle = False
ActiveDocument.Styles("Normal").NoSpaceBetweenParagraphsOfSameStyle = True
The macro recorder recognizes changing spacing but not "Don't add space between paragraphs of the same style" (Page Layout > Paragraph). To change paragraph formatting without modifying a built-in style (or creating a new style), I can use Selection.Style:
Selection.Style.NoSpaceBetweenParagraphsOfSameStyle = False
or fall back to the built-in dialog:
With Dialogs(wdDialogFormatParagraph)
.Before = 12
.After = 12
.NoSpaceBetweenParagraphsOfSameStyle = False
.Execute
End With
winword.ActiveDocument.Styles["Normal"].NoSpaceBetweenParagraphsOfSameStyle = true;
winword.ActiveDocument.Styles["List Paragraph"].NoSpaceBetweenParagraphsOfSameStyle = false;
On word doc press Alt+Ctl+Shift+S to check all the styles
If anyone stumbles on this and is looking for a C# example, this is what worked for me. Hope it helps someone else.
string signaturesPath = Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + #"\Microsoft\Signatures\";
Directory.CreateDirectory(signaturesPath + "Test");
Word.Application oWord = new Word.Application();
//oWord.Visible = true;
Word.Document oDoc = oWord.Documents.Add();
//Insert a paragraph at the beginning of the document.
Word.Paragraph paragraph1 = oDoc.Content.Paragraphs.Add();
object oStyleName1 = Word.WdBuiltinStyle.wdStyleNormal;
//NoSpaceBetweenParagraphsOfSameStyle set on style then assign to doc
oWord.ActiveDocument.Styles[oStyleName1].NoSpaceBetweenParagraphsOfSameStyle = true;
//Setting style on paragraph here
paragraph1.Format.set_Style(oStyleName1);
paragraph1.Range.Font.Bold = 1;
paragraph1.Range.InsertAfter("Testing 123");
//Save as htm
object htmlFormat = (int)Word.WdSaveFormat.wdFormatFilteredHTML;
oDoc.SaveAs2(signaturesPath + #"\test.htm", htmlFormat);
oWord.Quit();

Disable cell-border highlight DataGridView

I'm trying to disable the border around a cell when selected! (The black rectangle, not the blue background).
Is that possible?
this is my grid initialization code (maybe will help):
With DBGrid
.RowHeadersVisible = False
.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing
.RowTemplate.Height = internal_RowHeight
' Set property values appropriate for read-only display and
' limited interactivity.
.AllowUserToAddRows = False
.AllowUserToDeleteRows = False
.AllowUserToOrderColumns = False
.ReadOnly = True
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
.AllowUserToResizeColumns = False
.AllowUserToResizeRows = False
.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None
' Set the row height
.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
.ColumnHeadersHeight = internal_RowHeight
.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
' Set the selection background color for all the cells.
.DefaultCellStyle.BackColor = internal_BackColor
.DefaultCellStyle.ForeColor = internal_ForeColor
.DefaultCellStyle.SelectionBackColor = internal_BackColorSel
.DefaultCellStyle.SelectionForeColor = internal_ForeColorSel
' Set RowHeadersDefaultCellStyle.SelectionBackColor so that its default
' value won't override DataGridView.DefaultCellStyle.SelectionBackColor.
.RowHeadersDefaultCellStyle.SelectionBackColor = Color.Empty
.RowsDefaultCellStyle.BackColor = Color.Empty
' Set the row and column header styles.
.ColumnHeadersDefaultCellStyle.ForeColor = Color.White
.ColumnHeadersDefaultCellStyle.BackColor = Color.Black
End With
Since you want to disable the active cell, I would assume you only intend to use the grid as a way to select a row and not edit the contents.
In that case, use the following settings:
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
DataGridView1.ReadOnly = True