Powershell refresh CheckBox array from ComboBox - arrays

With Powershell, I have a form with several tabs.
In the third tab, I make an array of numbers picked from Windows registry that I'll put in a ComboBox.
Then I make automatically several CheckBox by registry value names depending the ComboBox.
My problem is that I can't refresh/update the CheckBox when I change the text of the ComboBox. I tried ".Refresh()" like below and other things, but it doesn't work.
Here are my registry keys:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\SOFTWARE\MySoftware]
[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder]
[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder\SubFolder]
"ValueName1"=""
"ValueName2"=""
"ValueName4"=""
"ValueName5"=""
"ValueName6"=""
"ValueName8"=""
"ValueName9"=""
"ValueName10"=""
"ValueName11"=""
[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder1]
[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder1\SubFolder]
"ValueName1"=""
"ValueName2"=""
"ValueName6"=""
"ValueName7"=""
"ValueName8"=""
"ValueName9"=""
"ValueName11"=""
[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder10]
[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder10\SubFolder]
"ValueName1"=""
"ValueName2"=""
"ValueName6"=""
"ValueName7"=""
"ValueName8"=""
"ValueName9"=""
"ValueName10"=""
"ValueName11"=""
[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder2]
[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder2\SubFolder]
[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder3]
[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder3\SubFolder]
"ValueName1"=""
"ValueName2"=""
"ValueName3"=""
"ValueName5"=""
"ValueName6"=""
"ValueName7"=""
"ValueName9"=""
"ValueName10"=""
[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder4]
[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder4\SubFolder]
"ValueName1"=""
"ValueName2"=""
"ValueName4"=""
"ValueName5"=""
"ValueName6"=""
"ValueName7"=""
"ValueName10"=""
"ValueName11"=""
[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder5]
[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder5\SubFolder]
"ValueName1"=""
"ValueName4"=""
"ValueName5"=""
"ValueName6"=""
"ValueName8"=""
"ValueName9"=""
"ValueName10"=""
[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder6]
[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder6\SubFolder]
"ValueName1"=""
"ValueName2"=""
"ValueName5"=""
"ValueName6"=""
"ValueName7"=""
"ValueName9"=""
"ValueName11"=""
[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder7]
[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder7\SubFolder]
"ValueName1"=""
"ValueName2"=""
"ValueName5"=""
"ValueName6"=""
"ValueName7"=""
"ValueName10"=""
"ValueName11"=""
[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder8]
[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder8\SubFolder]
"ValueName1"=""
"ValueName2"=""
"ValueName3"=""
"ValueName4"=""
"ValueName7"=""
"ValueName8"=""
"ValueName9"=""
"ValueName11"=""
[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder9]
[HKEY_CURRENT_USER\SOFTWARE\MySoftware\Folder9\SubFolder]
"ValueName1"=""
"ValueName5"=""
"ValueName6"=""
"ValueName10"=""
"ValueName11"=""
And here is my script:
function GenerateForm {
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
$form1 = New-Object System.Windows.Forms.Form
$tabControl = New-Object System.Windows.Forms.tabControl
$ToolTip = New-Object System.Windows.Forms.ToolTip
$ToolTip.BackColor = [System.Drawing.Color]::LightGoldenrodYellow
$ToolTip.IsBalloon = $true
$Tab1 = New-Object System.Windows.Forms.TabPage
$Tab2 = New-Object System.Windows.Forms.TabPage
$tabTab3 = New-Object System.Windows.Forms.TabPage
$labelDossierTab3 = New-Object System.Windows.Forms.Label
$ComboTab3 = New-Object System.Windows.Forms.ComboBox
$checkBoxAllTab3 = New-Object System.Windows.Forms.Button
$checkBoxNothingTab3 = New-Object System.Windows.Forms.Button
$ActiveTab3 = New-Object System.Windows.Forms.CheckBox
$GenerateButtonTab3 = New-Object System.Windows.Forms.Button
$CloseButton = New-Object System.Windows.Forms.Button
$fontDialog1 = New-Object System.Windows.Forms.FontDialog
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
$CloseButton_OnClick=
{
$form1.Close()
}
$OnLoadForm_StateCorrection=
{
$form1.WindowState = $InitialFormWindowState
}
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 464
$System_Drawing_Size.Width = 704
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Name = "form1"
$form1.Text = "Task generator"
$form1.StartPosition = "CenterScreen"
$form1.CancelButton = $CloseButton
$tabControl.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 136
$System_Drawing_Point.Y = 83
$tabControl.Location = $System_Drawing_Point
$tabControl.Name = "tabControl"
$tabControl.SelectedIndex = 0
$tabControl.ShowToolTips = $True
$tabControl.Multiline = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 320
$System_Drawing_Size.Width = 453
$tabControl.Size = $System_Drawing_Size
$tabControl.TabIndex = 4
$form1.Controls.Add($tabControl)
$Tab1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 4
$System_Drawing_Point.Y = 22
$Tab1.Location = $System_Drawing_Point
$Tab1.Name = "Tab1"
$System_Windows_Forms_Padding = New-Object System.Windows.Forms.Padding
$System_Windows_Forms_Padding.All = 3
$System_Windows_Forms_Padding.Bottom = 3
$System_Windows_Forms_Padding.Left = 3
$System_Windows_Forms_Padding.Right = 3
$System_Windows_Forms_Padding.Top = 3
$Tab1.Padding = $System_Windows_Forms_Padding
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 274
$System_Drawing_Size.Width = 445
$Tab1.Size = $System_Drawing_Size
$Tab1.TabIndex = 0
$Tab1.Text = "Tab1"
$Tab1.UseVisualStyleBackColor = $True
$tabControl.Controls.Add($Tab1)
$Tab2.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 4
$System_Drawing_Point.Y = 22
$Tab2.Location = $System_Drawing_Point
$Tab2.Name = "Tab2"
$System_Windows_Forms_Padding = New-Object System.Windows.Forms.Padding
$System_Windows_Forms_Padding.All = 3
$System_Windows_Forms_Padding.Bottom = 3
$System_Windows_Forms_Padding.Left = 3
$System_Windows_Forms_Padding.Right = 3
$System_Windows_Forms_Padding.Top = 3
$Tab2.Padding = $System_Windows_Forms_Padding
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 274
$System_Drawing_Size.Width = 445
$Tab2.Size = $System_Drawing_Size
$Tab2.TabIndex = 4
$Tab2.Text = "Tab2"
$Tab2.UseVisualStyleBackColor = $True
$tabControl.Controls.Add($Tab2)
$tabTab3.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 4
$System_Drawing_Point.Y = 22
$tabTab3.Location = $System_Drawing_Point
$tabTab3.Name = "tabTab3"
$System_Windows_Forms_Padding = New-Object System.Windows.Forms.Padding
$System_Windows_Forms_Padding.All = 3
$System_Windows_Forms_Padding.Bottom = 3
$System_Windows_Forms_Padding.Left = 3
$System_Windows_Forms_Padding.Right = 3
$System_Windows_Forms_Padding.Top = 3
$tabTab3.Padding = $System_Windows_Forms_Padding
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 274
$System_Drawing_Size.Width = 445
$tabTab3.Size = $System_Drawing_Size
$tabTab3.TabIndex = 4
$tabTab3.Text = "Tab3"
$tabTab3.UseVisualStyleBackColor = $True
$tabControl.Controls.Add($tabTab3)
$labelDossierTab3.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 340
$System_Drawing_Point.Y = 6
$labelDossierTab3.Location = $System_Drawing_Point
$labelDossierTab3.Name = "labeTab3"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 40
$System_Drawing_Size.Width = 50
$labelDossierTab3.Size = $System_Drawing_Size
$labelDossierTab3.TabIndex = 2
$labelDossierTab3.Text = "Option :"
$tabTab3.Controls.Add($labelDossierTab3)
$ComboTab3.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 390
$System_Drawing_Point.Y = 3
$ComboTab3.Location = $System_Drawing_Point
$ComboTab3.Name = "labeTab3"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 40
$System_Drawing_Size.Width = 45
$ComboTab3.Size = $System_Drawing_Size
$ComboTab3.TabIndex = 3
$ComboTab3.BeginUpdate()
$Dossierslist = Get-ChildItem "HKCU:\Software\MySoftware\" | Where-Object {$_ -match "Folder."} | Foreach-object {$_ -replace 'HKEY_CURRENT_USER\\Software\\MySoftware\\Folder', ''}
foreach($Dossier in $Dossierslist){$ComboTab3.Items.Add($Dossier) | sort}
$ComboTab3.EndUpdate()
$ComboTab3.SelectedIndex = 0
$ComboTab3.add_TextChanged({
$tabTab3.Refresh()
})
$tabTab3.Controls.Add($ComboTab3)
$CleDossier = "HKCU:\Software\MySoftware\Folder"+($ComboTab3.text)+"\"
$Baseslist = #()
$Baseslist += Get-ChildItem $CleDossier | Where-Object {$_.Name -match "SubFolder"}
$Baseslist += Get-ChildItem "HKCU:\Software\MySoftware\Folder\" | Where-Object {$_.Name -match "SubFolder"}
$CheckBoxLabelsC = $Baseslist.Property -match "'ValueName1'|ValueName3|ValueName5|ValueName7|ValueName9|ValueName11" | sort | Select -Unique
$CheckBoxCounterC = 1
$CheckBoxesC = foreach($LabelC in $CheckBoxLabelsC) {
$CheckBoxC = New-Object System.Windows.Forms.CheckBox
$CheckBoxC.DataBindings.DefaultDataSourceUpdateMode = 0
$CheckBoxC.UseVisualStyleBackColor = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 130
$System_Drawing_Size.Height = 20
$CheckBoxC.Size = $System_Drawing_Size
$CheckBoxC.TabIndex = 2
$CheckBoxC.Text = $LabelC
$System_Drawing_Point = New-Object System.Drawing.Point
if($CheckBoxCounterC -lt 7){
$System_Drawing_Point.X = 130}
elseif($CheckBoxCounterC -ge 7){
$System_Drawing_Point.X = 300}
if($CheckBoxCounterC -lt 7){
$System_Drawing_Point.Y = 75 + (($CheckBoxCounterC - 1) * 20)}
elseif(($CheckBoxCounterC -ge 7) -AND ($CheckBoxCounterC -lt 17)){
$System_Drawing_Point.Y = - 45 + (($CheckBoxCounterC - 1) * 20)}
$CheckBoxC.Location = $System_Drawing_Point
$CheckBoxC.Name = "CheckBoxC$CheckBoxCounterC"
$CheckBoxC.Add_CheckStateChanged({
foreach($CheckBoxC in $CheckBoxesC | Where-Object {$_.checked -eq $false}) {
$GenerateButtonTab3.Enabled = $false}
})
$CheckBoxC.Add_CheckStateChanged({
foreach($CheckBoxC in $CheckBoxesC | Where-Object {$_.checked -eq $true}) {
$GenerateButtonTab3.Enabled = $true}
})
$tabTab3.Controls.Add($CheckBoxC)
$CheckBoxC
$CheckBoxCounterC++
}
$checkBoxAllTab3.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 17
$System_Drawing_Point.Y = 55
$checkBoxAllTab3.Location = $System_Drawing_Point
$checkBoxAllTab3.Name = "checkBoxAllTab3"
$checkBoxAllTab3.Text = "All"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 60
$checkBoxAllTab3.Size = $System_Drawing_Size
$checkBoxAllTab3.TabIndex = 0
$checkBoxAllTab3.Add_Click({
foreach($CheckBoxC in $CheckBoxesC) {
$CheckBoxC.Checked = $true}
})
$tabTab3.Controls.Add($checkBoxAllTab3)
$checkBoxNothingTab3.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 17
$System_Drawing_Point.Y = 85
$checkBoxNothingTab3.Location = $System_Drawing_Point
$checkBoxNothingTab3.Name = "checkBoxNothingTab3"
$checkBoxNothingTab3.Text = "Nothing"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 60
$checkBoxNothingTab3.Size = $System_Drawing_Size
$checkBoxNothingTab3.TabIndex = 0
$checkBoxNothingTab3.Add_Click({
foreach($CheckBoxC in $CheckBoxesC) {
$CheckBoxC.Checked = $false}
})
$tabTab3.Controls.Add($checkBoxNothingTab3)
$ActiveTab3.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 320
$System_Drawing_Point.Y = 240
$ActiveTab3.Location = $System_Drawing_Point
$ActiveTab3.Name = "ActiveTab3"
$ActiveTab3.Text = "Desactivate"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 30
$System_Drawing_Size.Width = 123
$ActiveTab3.Size = $System_Drawing_Size
$ActiveTab3.TabIndex = 12
$ToolTip.SetToolTip($ActiveTab3, "The task will be desactivated. You'll have to activate it by yourself")
$tabTab3.Controls.Add($ActiveTab3)
$handler_GenerateButtonTab3_Click=
{
}
$GenerateButtonTab3.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 147
$System_Drawing_Point.Y = 244
$GenerateButtonTab3.Location = $System_Drawing_Point
$GenerateButtonTab3.Name = "GenerateButtonTab3"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 150
$GenerateButtonTab3.Size = $System_Drawing_Size
$GenerateButtonTab3.TabIndex = 0
$GenerateButtonTab3.Text = "Generate"
$GenerateButtonTab3.Enabled = $false
$GenerateButtonTab3.UseVisualStyleBackColor = $True
$GenerateButtonTab3.add_Click($handler_GenerateButtonTab3_Click)
$tabTab3.Controls.Add($GenerateButtonTab3)
$CloseButton.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 475
$System_Drawing_Point.Y = 420
$CloseButton.Location = $System_Drawing_Point
$CloseButton.Name = "CloseButton"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 75
$CloseButton.Size = $System_Drawing_Size
$CloseButton.TabIndex = 6
$CloseButton.Text = "Close"
$CloseButton.UseVisualStyleBackColor = $True
$CloseButton.add_Click($CloseButton_OnClick)
$form1.Controls.Add($CloseButton)
$fontDialog1.ShowHelp = $True
$InitialFormWindowState = $form1.WindowState
$form1.add_Load($OnLoadForm_StateCorrection)
$form1.ShowDialog()| Out-Null
}
GenerateForm
Thanks for your help!

I don't think you're subscribing to the right event.
Have you looked at SelectedIndexChanged?
This will fire when a selection has changed and then you can look at the ComboBox's Index property to figure out which item changed. An index of negative one means no item in the combobox is selected.
The click event has nothing to do with selected items in a combobox.

Related

Overlap Stacked Area Chart in WinForms

I am trying to overlap a stacked area chart on an existing chart in a WinForm application (using System.Windows.Forms.DataVisualization.Charting library).
As per the above screenshot, the green colored stacked area chart should be at the highlighted (yellow) color line. However, currently the code creates it on top of the existing stacked area chart (blue).
How can I change it such that the blue and green color charts overlap?
Code:
private void DrawChart()
{
var dataTable = new DataTable();
dataTable.Columns.Add("Year");
dataTable.Columns.Add("2020");
dataTable.Columns.Add("2021");
dataTable.Columns.Add("2022");
dataTable.Columns.Add("2023");
dataTable.Columns.Add("2024");
var dr = dataTable.NewRow();
dr = dataTable.NewRow();
dr["Year"] = "Stacked Area 1";
dr["2020"] = 20000;
dr["2021"] = 60000;
dr["2022"] = 130000;
dr["2023"] = 190000;
dr["2024"] = 220000;
dataTable.Rows.Add(dr);
dr = dataTable.NewRow();
dr["Year"] = "Stacked Area 2";
dr["2020"] = 30000;
dr["2021"] = 70000;
dr["2022"] = 140000;
dr["2023"] = 200000;
dr["2024"] = 230000;
dataTable.Rows.Add(dr);
dr = dataTable.NewRow();
dr["Year"] = "Overlapping Stacked Area 1";
dr["2020"] = 10000;
dr["2021"] = 50000;
dr["2022"] = 120000;
dr["2023"] = 180000;
dr["2024"] = 210000;
dataTable.Rows.Add(dr);
dr = dataTable.NewRow();
dr["Year"] = "Overlapping Stacked Area 2";
dr["2020"] = 15000;
dr["2021"] = 60000;
dr["2022"] = 130000;
dr["2023"] = 190000;
dr["2024"] = 220000;
dataTable.Rows.Add(dr);
chart.ChartAreas["Default"].AxisX.Crossing = 0;
chart.ChartAreas["Default"].AxisY.Crossing = 0;
chart.Visible = true;
foreach (DataRow row in dataTable.Rows)
{
string seriesName = row["Year"].ToString();
if (chart.Series.FindByName(seriesName) == null)
{
chart.Series.Add(seriesName);
switch (seriesName)
{
case "Stacked Area 1":
chart.Series[seriesName].ChartType = SeriesChartType.StackedArea;
chart.Series[seriesName].Color = Color.Transparent;
chart.Series[seriesName].BorderDashStyle = ChartDashStyle.Dot;
chart.Series[seriesName].BorderWidth = 2;
chart.Series[seriesName].BorderColor = Color.Black;
break;
case "Stacked Area 2":
chart.Series[seriesName].ChartType = SeriesChartType.StackedArea;
chart.Series[seriesName].Color = Color.LightBlue;
chart.Series[seriesName].BorderDashStyle = ChartDashStyle.Dot;
chart.Series[seriesName].BorderWidth = 2;
chart.Series[seriesName].BorderColor = Color.Black;
break;
case "Overlapping Stacked Area 1":
chart.Series[seriesName].ChartType = SeriesChartType.StackedArea;
chart.Series[seriesName].SetDefault(true);
chart.Series[seriesName].Color = Color.Transparent;
chart.Series[seriesName].BorderDashStyle = ChartDashStyle.Dot;
chart.Series[seriesName].BorderWidth = 2;
chart.Series[seriesName].BorderColor = Color.FromArgb(100, Color.Green);
break;
case "Overlapping Stacked Area 2":
chart.Series[seriesName].ChartType = SeriesChartType.StackedArea;
chart.Series[seriesName].SetDefault(true);
chart.Series[seriesName].Color = Color.FromArgb(100, Color.Green);
chart.Series[seriesName].BorderDashStyle = ChartDashStyle.Dot;
chart.Series[seriesName].BorderWidth = 2;
chart.Series[seriesName].BorderColor = Color.FromArgb(100, Color.Green);
break;
}
}
chart.Series[seriesName].Points.Clear();
var years = dataTable.Columns.Count;
for (int i = 1; i < years; i++)
{
string columnName = dataTable.Columns[i].ColumnName;
if (row[columnName] != null && !String.IsNullOrEmpty(row[columnName].ToString()))
{
var val = Convert.ToInt64(row[columnName].ToString());
chart.Series[seriesName].Points.AddXY(columnName, val);
}
}
}
}
There are a number of ways to solve this, but I'd say that the best place to start would be changing the type of Series from stacked to "Range". If you look into that series type it definitely seems to be what you're looking for. I've created some example code adapted from your original code. I've used tuples because I didn't want to spend a massive amount of time doing this, but I definitely wouldn't recommend using them for this.
private void DrawChart()
{
var dataTable = new DataTable();
dataTable.Columns.Add("Year");
dataTable.Columns.Add("2020",typeof(Tuple<int,int>));
dataTable.Columns.Add("2021",typeof(Tuple<int,int>));
dataTable.Columns.Add("2022",typeof(Tuple<int,int>));
dataTable.Columns.Add("2023",typeof(Tuple<int,int>));
dataTable.Columns.Add("2024", typeof(Tuple<int, int>));
var dr = dataTable.NewRow();
dr = dataTable.NewRow();
dr["Year"] = "Range 1";
dr["2020"] = new Tuple<int,int>(20000, 30000);
dr["2021"] = new Tuple<int,int>(60000, 70000);
dr["2022"] = new Tuple<int,int>(130000, 140000);
dr["2023"] = new Tuple<int,int>(190000, 200000);
dr["2024"] = new Tuple<int,int>(220000, 230000);
dataTable.Rows.Add(dr);
dr = dataTable.NewRow();
dr["Year"] = "Range 2";
dr["2020"] = new Tuple<int,int>(10000, 25000);
dr["2021"] = new Tuple<int,int>(50000, 90000);
dr["2022"] = new Tuple<int,int>(120000, 130000);
dr["2023"] = new Tuple<int,int>(180000, 210000);
dr["2024"] = new Tuple<int,int>(210000, 220000);
dataTable.Rows.Add(dr);
chart.ChartAreas["Default"].AxisX.Crossing = 0;
chart.ChartAreas["Default"].AxisY.Crossing = 0;
chart.Visible = true;
foreach (DataRow row in dataTable.Rows)
{
string seriesName = row["Year"].ToString();
Series series = null;
if (chart.Series.FindByName(seriesName) == null)
{
series = chart.Series.Add(seriesName);
switch (seriesName)
{
case "Range 1":
chart.Series[seriesName].ChartType = SeriesChartType.Range;
chart.Series[seriesName].Color = Color.FromArgb(100,Color.LightBlue);
chart.Series[seriesName].BorderDashStyle = ChartDashStyle.Dot;
chart.Series[seriesName].BorderWidth = 2;
chart.Series[seriesName].BorderColor = Color.Black;
break;
case "Range 2":
chart.Series[seriesName].ChartType = SeriesChartType.Range;
chart.Series[seriesName].SetDefault(true);
chart.Series[seriesName].Color = Color.FromArgb(100, Color.Green);
chart.Series[seriesName].BorderDashStyle = ChartDashStyle.Dot;
chart.Series[seriesName].BorderWidth = 2;
chart.Series[seriesName].BorderColor = Color.FromArgb(100, Color.Green);
break;
}
}
if (series == null) continue;
var years = dataTable.Columns.Count;
for (int i = 1; i < years; i++)
{
string columnName = dataTable.Columns[i].ColumnName;
if (row[columnName] != null && !String.IsNullOrEmpty(row[columnName].ToString()))
{
var val = (Tuple<int,int>) row[columnName];
series.Points.AddXY(columnName, val.Item1,val.Item2);
}
}
}
}

Get minimum value in an array and then get index

I want to get the minimum value in an array and then get the index of that item, in one step without writing my own loop (if I have to please let me know).
I know I can just do the
$b = ($a | Measure -Minimum).Minimum
But then I have to do
[array]::IndexOf($a, $b)
And while that is normally okay, I'm looking for a way to do it once because I'm running this MANY MANY times in a loop.
Thanks!
EDIT: One step meaning without looping through the array twice
Personally, I might consider a different data structure. Maybe something sorted to begin with...
This code may work for your needs:
$myArray = 5,66,4,33,2,9,9,12
$index = 0
$minIndex = 0
$minValue = [int]::MaxValue
$myArray | % { if ($minValue -gt $_) {$minValue = $_; $minIndex = $index}; $index++ }
"MinIndex $minIndex = MinValue $minValue"
its a problem of type, try like this:
$myArray = [int[]]5,66,4,33,2,9,9,12
$minvalue=[int]($myArray | measure -Minimum).Minimum
$myArray.IndexOf($minvalue)
Here are 6 different options for you...
cls
$myArray = #(5,66,4,33,2,9,9,12)
$iterations = 50000
$t = (measure-command{
foreach ($i in 1..$iterations) {
$minValue = [int]($myArray | Measure-Object -Minimum).Minimum
$minIndex = $myArray.IndexOf($minValue)
}
}).TotalSeconds
"measure-object with indexOf: $t"
$t = (measure-command{
foreach ($i in 1..$iterations) {
$index = 0
$minIndex = 0
$minValue = [int]::MaxValue
$myArray | % { if ($minValue -gt $_) {$minValue = $_; $minIndex = $index}; $index++ }
}
}).TotalSeconds
"pipeline with compare: $t"
$t = (measure-command{
foreach ($i in 1..$iterations) {
$minIndex = 0
$minValue = [int]::MaxValue
foreach ($index in 0..($myArray.count-1)) {
if ($myArray[$index] -lt $minValue) {
$minValue = $myArray[$index]
$minIndex = $index
}
}
}
}).TotalSeconds
"foreach-loop with compare: $t"
$t = (measure-command{
foreach ($i in 1..$iterations) {
$h = [System.Collections.ArrayList]::new()
foreach ($index in 0..($myArray.count-1)) {
$null = $h.add([tuple]::Create($myArray[$index], $index))
}
$h.Sort()
$minIndex = $h[0].Item2
}
}).TotalSeconds
"quicksort of a list of tuples: $t"
Add-Type -TypeDefinition #"
using System;
using System.Linq;
public static class My {
public static int indexOfMin(int[] arr){
int min = arr.Min();
return Array.IndexOf(arr, min);
}
}
"#
$t = (measure-command{
foreach ($i in 1..$iterations) {
$minIndex = [my]::indexOfMin($myArray)
}
}).TotalSeconds
"custom type and IndexOf: $t"
Add-Type #"
public static int[] indexOfMin(int[] arr){
int min = int.MaxValue;
int minIndex = 0;
for (int i = 0; i < arr.Length; i++) {
if (arr[i]<min) {
min = arr[i];
minIndex = i;
}
}
return new int[] {min, minIndex};
}
"# -name My2 -Namespace System
$t = (measure-command{
foreach ($i in 1..$iterations) {
$minValue, $minIndex = [my2]::indexOfMin($myArray)
}
}).TotalSeconds
"custom type and looping: $t"

Multiple database- access data from the 3rd database using codeigniter

I am using codeigniter.I want to use 3 database in my project.I set the database.php file us,
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'marketer_fp';
$db['default']['password'] = 'xxxxx';
$db['default']['database'] = 'marketer_fpr';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
$db['wpdb']['hostname'] = "localhost";
$db['wpdb']['username'] = "marketer_fprr";
$db['wpdb']['password'] = "yyyy";
$db['wpdb']['database'] = "marketer_mark";
$db['wpdb']['dbdriver'] = "mysql";
$db['wpdb']['dbprefix'] = "";
$db['wpdb']['pconnect'] = FALSE;
$db['wpdb']['db_debug'] = TRUE;
$db['wpdb']['cache_on'] = FALSE;
$db['wpdb']['cachedir'] = "";
$db['wpdb']['char_set'] = "utf8";
$db['wpdb']['dbcollat'] = "utf8_general_ci";
$db['wpfashn']['hostname'] = "localhost";
$db['wpfashn']['username'] = "marketer_fprr";
$db['wpfashn']['password'] = "yyyy";
$db['wpfashn']['database'] = "marketer_fah";
$db['wpfashn']['dbdriver'] = "mysql";
$db['wpfashn']['dbprefix'] = "";
$db['wpfashn']['pconnect'] = FALSE;
$db['wpfashn']['db_debug'] = TRUE;
$db['wpfashn']['cache_on'] = FALSE;
$db['wpfashn']['cachedir'] = "";
$db['wpfashn']['char_set'] = "utf8";
$db['wpfashn']['dbcollat'] = "utf8_general_ci";
The second and third database have same username and password.
In the controller ,inside construct function these 3 database are called.
$this->db3 =$this->load->database('wpfashn', TRUE);
$this->db2 =$this->load->database('wpdb', TRUE);
$this->db =$this->load->database('default', TRUE);
Inside another function a query is written,which should be executed by using the database 3.
$check=$this->db3->query("select * from mm_use_dat where wp_user_id='2'");
print_r($check->result());
this query return an empty array even if value present in the database.What should be the problem?Please help me to find a solution!

Passing multiple variables to parameter

Syntax:
New-VirtualSwitch [-VMHost] [-Name] [[-NumPorts] ] [[-Nic] String[]]
I am using the above cmdlet to add a new vSwitch in vSphere, where my knowledge lacks is how to use four checkbox that represent four NICs and when checked are passed to the -Nic parameter.
For instance the below wouldn't work
New-VirtualSwitch -VMHost $comboBox611.SelectedItem.toString() -Name $textBox611.Text -NumPorts 200 **-Nic $nic0,$nic1,$nic2,$nic3** -Mtu $textBox612.Text -Confirm
How do I pass the variables of each checkbox to a string array as the syntax shows it can be done?
$handler_linkLabel601_LinkClicked=
{
if ($networkdataGridView.CurrentRow.Cells['VM Host'].Value.toString() -gt " ")
{
Add-Type -AssemblyName System.Windows.Forms
$form601 = New-Object Windows.Forms.Form
$form601.Size = New-Object Drawing.Size (250,270)
$form601.StartPosition = "CenterScreen"
$label611.Size = New-Object Drawing.Size (70,40)
$label611.Location = New-Object System.Drawing.Size (10,15)
$label611.Text = "vSwitch Name:"
$textBox611.Size = New-Object Drawing.Size (100,30)
$textBox611.Location = New-Object System.Drawing.Size (90,15)
$textBox611.Name = "vSwitch Name"
$label612.Size = New-Object Drawing.Size (50,20)
$label612.Location = New-Object System.Drawing.Size (10,55)
$label612.Text = "Host:"
$vmhostlist = Get-VMHost
foreach ($vmhost in $vmhostlist)
{
$comboBox611.Items.add($vmhost.name.toString())
}
$comboBox611.Size = New-Object Drawing.Size (100,20)
$comboBox611.Location = New-Object System.Drawing.Size (90,50)
$checkBox611.Size = New-Object Drawing.Size (20,20)
$checkBox611.Location = New-Object System.Drawing.Size (100,80)
# Add Click-Event
$checkBox611.Add_CheckStateChanged({
If ($checkBox611.Checked) {
$global:nic0 = "vmnic0"
} Else {
$global:nic0 = ""
}
})
$checkBox612.Size = New-Object Drawing.Size (20,20)
$checkBox612.Location = New-Object System.Drawing.Size (170,80)
$checkBox611.Add_CheckStateChanged({
If ($checkBox611.Checked) {
$global:nic1 = "vmnic1"
} Else {
$global:nic1 = ""
}
})
$checkBox613.Size = New-Object Drawing.Size (20,20)
$checkBox613.Location = New-Object System.Drawing.Size (100,100)
$checkBox611.Add_CheckStateChanged({
If ($checkBox611.Checked) {
$global:nic2 = "vmnic2"
} Else {
$global:nic2 = ""
}
})
$checkBox614.Size = New-Object Drawing.Size (20,20)
$checkBox614.Location = New-Object System.Drawing.Size (170,100)
$checkBox611.Add_CheckStateChanged({
If ($checkBox611.Checked) {
$global:nic3 = "vmnic3"
} Else {
$global:nic3 = ""
}
})
$label613.Size = New-Object Drawing.Size (80,20)
$label613.Location = New-Object System.Drawing.Size (10,140)
$label613.Text = "MTU Size:"
$textBox612.Size = New-Object Drawing.Size (100,20)
$textBox612.Location = New-Object System.Drawing.Size (90,140)
$textBox612.Name = "MTU"
$button = New-Object System.Windows.Forms.Button
$button.Size = New-Object Drawing.Size (90,30)
$button.Location = New-Object System.Drawing.Size (70,200)
$selectedvmhost = ($comboBox611.SelectedItem.toString())
$button.add_click({test})
$button.Text = "Add New vSwitch"
$form601.Controls.Add($button)
$form601.Controls.Add($textBox611)
$form601.Controls.Add($textBox612)
$form601.Controls.Add($label611)
$form601.Controls.Add($label612)
$form601.Controls.Add($label613)
$form601.Controls.Add($comboBox611)
$form601.Controls.Add($checkBox611)
$form601.Controls.Add($checkBox612)
$form601.Controls.Add($checkBox613)
$form601.Controls.Add($checkBox614)
$form601.ShowDialog()
}
ELSE
{}
}
function test
{
$nic = #($global:nic0,$global:nic1,$global:nic2,$global:nic3 | ? {-not [string]::IsNullOrEmpty($_)})
if ( $nic.count )
{
New-VirtualSwitch -VMHost $comboBox611.SelectedItem.toString() -Name $textBox611.Text -NumPorts 200 -Nic $nic -Mtu $textBox612.Text -Confirm
}
}
just as C.B. said replace $nic0 with $global:nic0, and $nic1 with $global:nic1 etc.
then test is
function test
{
$nic = #($global:nic0,$global:nic1,$global:nic2,$global:nic3 | ? {-not [string]::IsNullOrEmpty($_)})
if ( $nic.count )
{
New-VirtualSwitch -VMHost $comboBox611.SelectedItem.toString() -Name $textBox611.Text -NumPorts 200 -Nic $nic -Mtu $textBox612.Text -Confirm
}
}

Populate 100 images using an If loop

I'm currently populating 100 images using the following code, but I'm sure there's a more economical way of doing it with an If statement:
mapblock01.image = [UIImage imageNamed:#"block.png"]; mapblock01.hidden = TRUE;
mapblock02.image = [UIImage imageNamed:#"block.png"]; mapblock02.hidden = TRUE;
mapblock03.image = [UIImage imageNamed:#"block.png"]; mapblock03.hidden = TRUE;
mapblock04.image = [UIImage imageNamed:#"block.png"]; mapblock04.hidden = TRUE;
mapblock05.image = [UIImage imageNamed:#"block.png"]; mapblock05.hidden = TRUE;
------>
mapblock99.image = [UIImage imageNamed:#"block.png"]; mapblock99.hidden = TRUE;
mapblock100.image = [UIImage imageNamed:#"block.png"]; mapblock100.hidden = TRUE;
Any ideas?
for it, you can give tag for each UIImageView as 1,2,3..100 and set image as below:
for (int i = 0 ; i < 100 ; i++) {
UIImageView *imageV = (UIImageView *)[self.view viewWithTag:i];
imageV.image = [UIImage imageNamed:#"block.png"];
imageV.hidden = TRUE;
}
}

Resources