Sub-sub-Treeview (WPF) - c#

I am attempting to make a treeview out of a list of requirements - the requirements are in a list and go like "1.2.1.a_1" and "1.2.2.b_2". The code below currently makes a nice treeview that creates "Requirement 1" as a primary node, and nice "1.x" as subnodes with all of the requirements nicely listed under that "1.x" bit. My problem is that I want to go deeper - make expandable nodes for all "1.x.x" items (where both Xs are numbers) - so "1.2.3.a_1" and "1.2.3.b_1" are under the same parent node or "1.2.3". Not all requirements have a number for the 3rd non-period character (i.e. 2.1.a_1 might exist). I am just at a loss on how to make this happen... Please help.
private void LoadtheNarrativeTreeView() // load up the treeview on the narrative page
{
Narrative_one_Treeview.Items.Clear();
for (int i = 1; i <= 12; i++)
{
TreeViewItem rootnode = new TreeViewItem();
rootnode.Header = "Requirement " + i.ToString();
//TreeNode rootnode = new TreeNode("Requirement " + i.ToString(), 9, 9);
Narrative_one_Treeview.Items.Add(rootnode);
for (int s = 1; s <= 12; s++)
{
bool testsub = (theROC.FieldVariablesList.Exists(x => x.name.StartsWith(i.ToString() + "." + s.ToString())));
if (testsub == false)
{ break; }
List<FieldVariable> clist = new List<FieldVariable>(theROC.FieldVariablesList.Where(x => x.name.StartsWith(i.ToString() + "." + s.ToString())));
FieldVariable firstsub = new FieldVariable(theROC.FieldVariablesList.First(x => x.name.StartsWith(i.ToString() + "." + s.ToString()))); // 1.1, 1.2
TreeViewItem subrootnode = new TreeViewItem() { Header = i.ToString() + "." + s.ToString()};
rootnode.Items.Add(subrootnode);
foreach (FieldVariable FV in clist.ToList())
{
TreeViewItem cnode = new TreeViewItem() { Header = FV.name };
subrootnode.Items.Add(cnode);
}
}
}
}

Related

Panel.Controls.Remove not deleting all the controls It should(.NET)

I'm making a database manager program and the program creates only as much textbox
as needed (counts the columns in the table). But for some reason the program is not removing all the textboxes I want.
New textboxes should be created and old ones should be removed on every "ComboBoxSelectedIndexChange".
Here is my code snippet:
var elm = panel20.Controls.OfType<System.Windows.Forms.TextBox>();
foreach (var item in elm)
{
DeleteControlsWithTag(item.Tag.ToString());
}
button19.Enabled = true;
one_cond = string.Empty;
if (comboBox19.Text != string.Empty)
{
one_cond = comboBox19.Text.ToUpper();
if (one_cond == "ÖSSZES")
{
string allcolumn = "(";
colnames.Remove("Összes");
for (int i = 0; i < colnames.Count - 1; i++)
{
allcolumn += colnames.ElementAt(i) + ",";
}
allcolumn += colnames.Last() + ")";
button19.Enabled = false;
textBox16.Text = ManualQuery + " " + from + " " + allcolumn + " " + "VALUES" + " ";
if (3 < colnames.Count)
{
for (int i = 0; i < colnames.Count; i++)
{
System.Windows.Forms.TextBox textbox = new System.Windows.Forms.TextBox();
textbox.Name = $"textbox_{i}";
textbox.AccessibleName = $"textbox_{i}";
textbox.Tag = $"textbox_{i}";
panel20.Controls.Add(textbox);
textbox.Parent = panel20;
//"textbox_" + colnames[i] + i
if (2<i)
{
if (i == 3)
{
textbox.Location = new System.Drawing.Point(489, 49);
}
else
{
textbox.Location = new System.Drawing.Point(489 + ((i - 3) * 71), 49);
}
}
else
{
if (0 < i)
{
textbox.Location = new System.Drawing.Point(467 + (i * 71), 17);
}
else
{
textbox.Location = new System.Drawing.Point(467, 17);
}
}
textbox.Size = new System.Drawing.Size(64, 20);
}
}
else
{
for (int i = 0; i < colnames.Count; i++)
{
System.Windows.Forms.TextBox textbox = new System.Windows.Forms.TextBox();
textbox.Name = $"textbox_{i}";
textbox.AccessibleName = $"textbox_{i}";
textbox.Parent = panel20;
panel20.Controls.Add(textbox);
textbox.Tag = $"textbox_{i}";
if (0<i)
{
textbox.Location = new System.Drawing.Point(467 + (i*71), 17);
}
else
{
textbox.Location = new System.Drawing.Point(467, 17);
}
textbox.Size = new System.Drawing.Size(64, 20);
}
}
}
else
{
onecondinsert = "(" + one_cond + ")";
textBox16.Text = ManualQuery + " " + from + " " + onecondinsert + " " + "VALUES" + " ";
System.Windows.Forms.TextBox textbox = new System.Windows.Forms.TextBox();
textbox.Name = $"textbox";
textbox.AccessibleName = $"textbox";
textbox.Location = new System.Drawing.Point(464, 20);
textbox.Size = new System.Drawing.Size(100, 20);
textbox.Parent = panel20;
textbox.Tag = "textbox";
panel20.Controls.Add(textbox);
//tbox.TextChanged += tbox_TextChanged;
}
}
MessageBox.Show(elm.Count().ToString());
}
and here is some screenshot of how the problem looks like
on the first select it works
on this picture you can see the program did not delete all the text box.
longer textbox is the new one
On the third picture I selected another column (only one textbox should be visible) but the program deletes one textbox
as you can see here
After the third selection it deletes the last extra textbox too
3. try it "works"
I have tried different ways to delete from the panel20.controls
but all produced the same bug
Hope someone can help me, Thanks
Update:
I tried another method from here:
C#, deleting all the controls with the same .Tag
private List<Control> GetTaggedControls(string tag, Control parent)
{
var taggedControls = new List<Control>();
foreach (Control control in parent.Controls)
{
if (control.Tag?.ToString() == tag)
{
taggedControls.Add(control);
}
// Recursively call this method in case this is a container
taggedControls.AddRange(GetTaggedControls(tag, control));
}
return taggedControls;
}
// Deletes all controls with the specified tag
private void DeleteControlsWithTag(string tag)
{
foreach (Control control in GetTaggedControls(tag, this))
{
panel20.Controls.Remove(control);
}
}
and I tried to delete the textboxes like this:
var elm = panel20.Controls.OfType<System.Windows.Forms.TextBox>();
foreach (var item in elm)
{
DeleteControlsWithTag(item.Tag.ToString());
}
but it is still not working
Your GetTaggedControls() function is finding ANY type of control, RECURSIVELY starting at the main Form container level, that has a specific tag. So returned controls might not be from many containers, but your DeleteControlsWithTag() method tries to remove all matches only from panel20. That may or may not be a problem; it's unclear from the pictures and possibly incomplete posted code.
When you say remove the old textboxes, can we simply remove ALL existing TextBoxes, or do you really need to match tags?
From your original attempt:
var elm = panel20.Controls.OfType<System.Windows.Forms.TextBox>();
foreach (var item in elm)
{
DeleteControlsWithTag(item.Tag.ToString());
}
This is finding ALL textboxes in panel20, then trying to delete them based on matching tags with the helper function. So the tag doesn't even matter since you are finding all textboxes to begin with...
Just try removing all TextBoxes from panel20?
var elm = panel20.Controls.OfType<System.Windows.Forms.TextBox>().ToList();
elm.ForEach(tb => panel20.Controls.Remove(tb));
Okay, now I tried to tie together Idle_Mind's and LarsTech's recommendation
and finally it's working fine with "Dispose".
Thanks guys for your quick help.
here is the previous code:
var elm = panel20.Controls.OfType<System.Windows.Forms.TextBox>();
foreach (var item in elm)
{
DeleteControlsWithTag(item.Tag.ToString());
}
and here is the new one:
var elm = panel20.Controls.OfType<System.Windows.Forms.TextBox>().ToList();
elm.ForEach(tb => tb.Dispose());

C# Error when searching for control in UpdatePanel

I have a little problem. First of all, some info:
- On my page, I have an UpdatePanel with a button inside it.
- When you click this button, I generate a new row with dropdown lists. Each time I have to generate a table from scratch, because it resets after the click, so I update [ViewState] value and generate as many rows as clicks.
- Outside the panel, I have another button. After clicking this button, I want to collect data from drop-down lists. To do it, I have to get to these controls.
I tried to use function FindControl(), but I guess I can't - as far as I know, it does not perform a deep search. This means I have to pass as a parameter the exact container with this control. Because control is inside the table, I should get to the <td> value and I can't do that (<td> does not have ID - yes, I can add it but <td> is also dynamically created. That means I would need to get first to <td>, then to my control (guess what - <tr> is also created dynamically).
Because I can't use FindControl function, I use FindRecursiveControl function (code below) The problem is, that this function neither finds anything. Any suggestions about what might be the reason? I added this whole info in case that the reason is for example usage of UpdatePanel and page life cycle.
private Control FindControlRecursive(Control rootControl, string controlID)
{
if (rootControl.ID == controlID)
{
return rootControl;
}
foreach (Control controlToSearch in rootControl.Controls)
{
Control controlToReturn =
FindControlRecursive(controlToSearch, controlID);
if (controlToReturn != null)
{
return controlToReturn;
}
}
return null;
}
My usage of this function:
string control_id = "parametr" + i;
DropDownList dropdown = (DropDownList)FindControlRecursive(UpdatePanel1, control_id);
Script generating table in UpdatePanel after button click
protected void generuj_tabele(int il_klik)
{
il_par.Text = "Ilość parametrów: " + il_klik.ToString();
TableRow table_head = new TableRow();
table_head.Attributes.Add("class", "w3-green");
Table1.Rows.Add(table_head);
for (int j = 0; j < 5; j++)
{
TableCell cell = new TableCell();
table_head.Cells.Add(cell);
}
Table1.Rows[0].Cells[0].Text = "Parametr";
Table1.Rows[0].Cells[1].Text = "Wartość początkowa";
Table1.Rows[0].Cells[2].Text = "Inkrementacja?";
Table1.Rows[0].Cells[3].Text = "Zwiększ o:";
Table1.Rows[0].Cells[4].Text = "Zwiększ co:";
RootObject obj = (RootObject)Session["get_offer"];
for (int i = 0; i < il_klik; i++)
{
parametr = new DropDownList();
wartosc = new TextBox();
inkrementacja = new CheckBox();
inkrementacja_numer = new TextBox();
skok = new TextBox();
//inkrementacja_numer.Enabled = false;
// skok.Enabled = false;
inkrementacja_numer.Attributes.Add("Type", "number");
skok.Attributes.Add("Type", "number");
//inkrementacja.CheckedChanged += new EventHandler((s, eventarg) => checkbox_change(s, eventarg, i));
//inkrementacja.AutoPostBack = true;
//parametr.AutoPostBack = true;
TableRow row = new TableRow();
Table1.Rows.Add(row);
parametr.EnableViewState = true;
wartosc.EnableViewState = true;
inkrementacja.EnableViewState = true;
inkrementacja_numer.EnableViewState = true;
skok.EnableViewState = true;
for (int j = 0; j < 5; j++)
{
TableCell cell = new TableCell();
row.Cells.Add(cell);
}
Table1.Rows[i + 1].Cells[0].Controls.Add(parametr);
Table1.Rows[i + 1].Cells[1].Controls.Add(wartosc);
Table1.Rows[i + 1].Cells[2].Controls.Add(inkrementacja);
Table1.Rows[i + 1].Cells[3].Controls.Add(inkrementacja_numer);
Table1.Rows[i + 1].Cells[4].Controls.Add(skok);
if (i == il_klik - 1)
{
wystaw_liste(obj);
Price pr = obj.sellingMode.price;
parametr.Items.Add(pr.amount.ToString());
List<Parameter> par = obj.parameters;
foreach (Parameter p in par)
{
List<string> val = p.values;
if (val.Count() > 0)
{
foreach (string v in val)
{
parametr.Items.Add(v);
}
}
}
foreach (string p in parametry_list)
{
parametr.Items.Add(p);
}
parametry_list.Clear();
}
parametry.Add(parametr);
wartosci.Add(wartosc);
inkrementacje.Add(inkrementacja);
inkrementacje_numery.Add(inkrementacja_numer);
skoki.Add(skok);
if (i == il_klik - 1)
{
Session["v_parametr"] = parametry;
Session["v_wartosc"] = wartosci;
Session["v_inkrementacja"] = inkrementacje;
Session["v_ink_nr"] = inkrementacje_numery;
Session["v_skok"] = skoki;
}
parametr.ID = "parametr" + i;
wartosc.ID = "wartosc" + i;
inkrementacja.ID = "inkrementacja" + i;
inkrementacja_numer.ID = "inkrementacja_numer" + i;
skok.ID = "skok" + i;
}
}
When I try to check parameters of DropDownList (e.g. SelectedValue) I get error "Object reference not set to an instance of an object"

create dynamic Keyboard telegram bot in c# with keyboard button not inline button in c#

How can I create a dynamic keyboard button I found some codes but they are for inline buttons?
I want to get data from database and create one button in each row with keyboard button
var keyboard = new ReplyKeyboardMarkup(
new[] {
new[]{
new KeyboardButton("a"),
},
new[]{
new KeyboardButton("b"),
},
});
The first part (i.e. adding the buttons from db) is OK with the same code you wrote.
The second part (i.e. changing button layout) I'm afraid, is not possible as far as I know. Because layout is done by Telegram app in client device and based on many things including device screen size and may change in different app versions.
Use InlineKeyboardMarkup and InlineKeyboardButton instead of it, you can refer to this example.
Please look up your library document next time.
private static ReplyKeyboardMarkup calendarMenu;
SqlDataAdapter sc3 = new SqlDataAdapter("select KeyboardName from dbo.Keyboards", SqlConnection);
DataTable dt3 = new DataTable();
sc3.Fill(dt3);
int keyboardRows = 0;
if (dt3.Rows.Count % 2 == 0)
{
keyboardRows = dt3.Rows.Count / 2;
}
else
{
keyboardRows = (dt3.Rows.Count / 2) + 1;
}
KeyboardButton[][] kbc = new KeyboardButton[(keyboardRows + 1)][];
KeyboardButton[] keys = new KeyboardButton[dt3.Rows.Count];
var i = 0;
foreach (DataRow cn3 in dt3.Rows)
{
keys[i] = new KeyboardButton(cn3["KeyboardName"].ToString());
i++;
}
for (int r = 0, s = 0; r < keyboardRows; r++, s++)
{
if (dt3.Rows.Count % 2 == 0)
{
kbc[r] = new KeyboardButton[] {keys[r + s], keys[r + s + 1]};
}
else
{
if ((r + s) != keys.Length)
{
kbc[r] = new KeyboardButton[] { keys[r + s], keys[r + s + 1] };
}
else
{
kbc[r] = new KeyboardButton[] { keys[r + s] };
}
}
}
kbc[keyboardRows] = new KeyboardButton[] { new KeyboardButton("Return to Main Menu"), };
calendarMenu = new ReplyKeyboardMarkup
{
Keyboard = kbc
};

Kendo Tree list removes the css classes after expand and collapsing nodes

I have added CSS classes to particular child element in kendo tree list in DataBound Event as:
dataBound: function (e) {
var treeview = $("#grid").data("kendoTreeList");
var treeData = treeview.dataSource.data();
for(var i =0; i< treeData.length; i++) {
if(treeData[i].parentId ==1 || treeData[i].parentId == 2 || treeData[i].parentId==3) {
var row = treeview.content.find("tr[data-uid=" + treeData[i].uid + "]");
row.addClass("grossMarginSegment");
}
}
}
Also same class is applied to three parent elements as follows:
var dataItem = treeview.dataSource.get(1);
var row = treeview.content.find("tr[data-uid=" + dataItem.uid + "]");
row.addClass("grossMarginSegment");
var dataItem = treeview.dataSource.get(2);
var row = treeview.content.find("tr[data-uid=" + dataItem.uid + "]");
row.addClass("grossMarginSegment");
var dataItem = treeview.dataSource.get(3);
var row = treeview.content.find("tr[data-uid=" + dataItem.uid + "]");
row.addClass("grossMarginSegment");
When I expand and collapse some of the node is in tree view the
classes applied to some of the nodes gets removed.
Code in exapnd event :
expand: function (e) {
Title = e.model.Title;
ParentId = e.model.parentId;
}
Couldn't find the reason

How do I edit the name of an item in a CheckedListBox?

I have a CheckedListBox that has X number of items. These items are placed there at runtime. These items are supposed to represent reports that can be displayed in the DataGridView. What I need to do now is display the record count for each report in parenthesis right next to the report name. I tried, not for too long, to edit the actual name of the item but couldn't find out how to do it. So then, I brute forced it. Saved the items to an array, cleared the items, appended the record counts to each item in the array, created new items. Well, this has caused issues because now it's not retaining my checks and the reason why is because whenever I generate the reports, I clear the items and recreate them. Well, rather than doing another foreach loop to save the checked status, does anyone know of a way to change the text of existing items in a CheckedListBox?
Here is the code I currently have:
In the MainForm.Designer.cs:
this.clbReports.Items.AddRange(new object[] {
"Report 1",
"Report 2",
"Report 3",
"Report 4",
"Report 5",
"Report 6",
"Report 7",
"Report 8",
"Report 9",
"Report 10",
"Report 11"});
And it looks like:
And I want it to look like (but there won't all be 0's):
Here is the SelectedIndexChanged function:
private void clbReports_SelectedIndexChanged(object sender, EventArgs e)
{
string strCheckBox = clbReports.SelectedItem.ToString();
bool bShowAllIsChecked = clbReports.GetItemChecked(clbReports.FindString("Show All Error Reports"));
bool bSelected = clbReports.GetItemChecked(clbReports.FindString(strCheckBox));
int nIndex = -1;
if (strCheckBox.Contains("Show All Error Reports"))
{
foreach (string str in _strReports)
{
if (!str.Contains("Show All Error Reports") && !str.Contains("Show Tagged Records"))
{
nIndex = clbReports.FindString(str);
if (nIndex > -1)
{
clbReports.SetItemChecked(nIndex, bSelected);
}
}
}
}
else
{
if (strCheckBox.Contains("Show All Error Reports") || bShowAllIsChecked)
{
foreach (string str in _strReports)
{
nIndex = clbReports.FindString(str);
if (nIndex > -1)
{
clbReports.SetItemChecked(nIndex, false);
}
}
}
nIndex = clbReports.FindString(strCheckBox);
if (nIndex > -1)
{
clbReports.SetItemChecked(nIndex, bShowAllIsChecked ? true : bSelected);
}
}
string[] strCheckedItems = new string[clbReports.CheckedItems.Count];
clbReports.CheckedItems.CopyTo(strCheckedItems, 0);
List<string> checkBoxReportFilter = new List<string>();
foreach (ReportRecord obj in this._lstReportRecords)
{
foreach (string str in strCheckedItems)
{
if (str.Contains(obj.Description))
{
checkBoxReportFilter.Add(obj.PartID.ToString());
}
}
}
try
{
if (checkBoxReportFilter.Count == 0 && clbReports.CheckedItems.Count > 0)
{
throw new NullReferenceException();
}
_strReportFilter = String.Join(",", checkBoxReportFilter.ToArray());
}
catch (NullReferenceException)
{
_strReportFilter = "-1";
}
generateReport();
}
And here is the code where I am clearing the items, getting the report counts and creating the new items.
_lstReportRecords = _dataController.ReportList;
bool[] bChecked = new bool[clbReports.Items.Count];
int nCounter = 0;
foreach (string str in _strReports)
{
foreach (string str2 in clbReports.SelectedItems)
{
bChecked[nCounter] = str2.Contains(str);
}
nCounter++;
}
clbReports.Items.Clear();
nCounter = 0;
foreach (string str in _strReports)
{
int nCount = _lstReportRecords.Where<ReportRecord>(delegate(ReportRecord rr) {
return rr.Description == str;
}).Count();
string newReport = str + " (" + nCount + ")";
clbReports.Items.Add(newReport);
clbReports.SetItemChecked(nCounter, bChecked[nCounter]);
nCounter++;
}
Please tell me there is an easier way to do this. I tried doing foreach loops through the clbReports.Items but it wants me to cast it to a string (errored on me when trying to cast to a CheckBox) so I couldn't change the value. And even if I could cast it to a CheckBox, I have a feeling it will give me the error that Enumeration has failed because the list has been changed (or however they word it). Any and all help is welcome. Thanks.
Edit: Please know that the Report X are just so that the actual report names aren't displayed to keep it generic. However, in the code, I just copied and pasted so the Show All Error Reports and Show All Tagged Records are reports I need to check.
The right ( == most simple and most direct) answer and solution is:
this.clbReports.Items[nIndex] = "new text of the item"
yes, those items are of type "object". No, nobody minds that, string is an object too ;)
If I were you, I'd try to give the INotifyPropertyChanged Interface a go.
You Shouldn't mess with events unless necessary. this will mean you can't use the designer to create the items, but as far as I've understood, it's a runtime-modified list anyway...
In detail:
• Create A Class (e.g.'Foo') that Implements INotifyPropertyChanged (Basically this will tell any listener that the text property has changed). This class will hold the names of all entries.
• create an ObservableCollection and bind your CheckedListBox to that Collection. In WinForms you will have to create a DataBindingSource and plug your Collection to one end and the ComboBox to the other end.
• Any change made to the collection will be visible in the control.
HTH
Sebi
In order to change the items in a ListBox (or a CheckedListBox), you should change these items' ToString() result.
The easiest solution would be to create a "Holder" class, which has a reference to the report it represents. Then the Holder class' ToString() method should be something like this:
public override string ToString()
{
return String.Format("{0} ({1})", BaseStr, MyReport.RecordCount);
}
If you change MyReport.RecordCount somehow (because a report's record count changes), you can just call clbReports.Refresh(), and it'll automatically show the new value.
I think this way you don't even need the temporary array solution in the second code block; however, I'd like to suggest an alternative way of getting the item's checked state.
You can iterate through the clbReports.CheckedIndices, and fill your bChecked array with true values only for indices in that array.
Well, due to time constraints I tried something else. I went with a ListView where CheckBoxes = true and View = List. I also removed Show All Error Reports and Show Tagged Records to checkboxes outside of the list. This made it a lot easier to do the functions I wanted. Here is the new code.
MainForm.Designer.cs
//
// cbTaggedRecords
//
this.cbTaggedRecords.AutoSize = true;
this.cbTaggedRecords.Location = new System.Drawing.Point(151, 9);
this.cbTaggedRecords.Name = "cbTaggedRecords";
this.cbTaggedRecords.Size = new System.Drawing.Size(106, 17);
this.cbTaggedRecords.TabIndex = 3;
this.cbTaggedRecords.Text = "Tagged Records";
this.cbTaggedRecords.UseVisualStyleBackColor = true;
this.cbTaggedRecords.CheckedChanged += new System.EventHandler(this.ShowTaggedRecords_CheckChanged);
//
// cbAllErrorReports
//
this.cbAllErrorReports.AutoSize = true;
this.cbAllErrorReports.Location = new System.Drawing.Point(6, 9);
this.cbAllErrorReports.Name = "cbAllErrorReports";
this.cbAllErrorReports.Size = new System.Drawing.Size(102, 17);
this.cbAllErrorReports.TabIndex = 2;
this.cbAllErrorReports.Text = "All Error Reports";
this.cbAllErrorReports.UseVisualStyleBackColor = true;
this.cbAllErrorReports.CheckedChanged += new System.EventHandler(this.ShowAllErrorReports_CheckChanged);
//
// listView1
//
this.listView1.CheckBoxes = true;
listViewItem1.StateImageIndex = 0;
listViewItem2.StateImageIndex = 0;
listViewItem3.StateImageIndex = 0;
listViewItem4.StateImageIndex = 0;
listViewItem5.StateImageIndex = 0;
listViewItem6.StateImageIndex = 0;
listViewItem7.StateImageIndex = 0;
listViewItem8.StateImageIndex = 0;
listViewItem9.StateImageIndex = 0;
this.listView1.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
listViewItem1,
listViewItem2,
listViewItem3,
listViewItem4,
listViewItem5,
listViewItem6,
listViewItem7,
listViewItem8,
listViewItem9});
this.listView1.Location = new System.Drawing.Point(6, 29);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(281, 295);
this.listView1.TabIndex = 1;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.List;
this.listView1.ItemChecked += new System.Windows.Forms.ItemCheckedEventHandler(this.listView_ItemChecked);
MainForm.cs
private void listView_ItemChecked(object sender, ItemCheckedEventArgs e)
{
if (e != null)
{
int nLength = e.Item.Text.IndexOf("(") - 1;
string strReport = nLength <= 0 ? e.Item.Text : e.Item.Text.Substring(0, nLength);
if (e.Item.Checked)
{
_lstReportFilter.Add(strReport);
}
else
{
_lstReportFilter.Remove(strReport);
}
}
List<string> checkBoxReportFilter = new List<string>();
foreach (ReportRecord obj in this._lstReportRecords)
{
foreach (string str in _lstReportFilter)
{
if (str.ToLower().Contains(obj.Description.ToLower()))
{
checkBoxReportFilter.Add(obj.PartID.ToString());
}
}
}
try
{
if (checkBoxReportFilter.Count == 0 && listView1.CheckedItems.Count > 0)
{
throw new NullReferenceException();
}
_strReportFilter = String.Join(",", checkBoxReportFilter.ToArray());
}
catch (NullReferenceException)
{
_strReportFilter = "-1";
}
if (!bShowAll)
{
generateReport();
}
}
private void ShowAllErrorReports_CheckChanged(object sender, EventArgs e)
{
bShowAll = true;
foreach (ListViewItem lvi in listView1.Items)
{
lvi.Checked = ((CheckBox)sender).Checked;
}
_lstReportFilter.Clear();
bShowAll = false;
generateReport();
}
private void ShowTaggedRecords_CheckChanged(object sender, EventArgs e)
{
bool bChecked = ((CheckBox)sender).Checked;
if (bChecked)
{
if (!_lstReportFilter.Contains("Show Tagged Records"))
{
_lstReportFilter.Add("Show Tagged Records");
}
}
else
{
_lstReportFilter.Remove("Show Tagged Records");
}
listView_ItemChecked(null, null);
}
Code to add counts to CheckBoxes
_lstReportRecords = _dataController.ReportList;
int nTotalCount = 0;
foreach (ListViewItem lvi in listView1.Items)
{
int nCount = _lstReportRecords.Where(rr => lvi.Text.Contains(rr.Description)).Count();
nTotalCount += nCount;
lvi.Text = (lvi.Text.Contains("(") ? lvi.Text.Substring(0, lvi.Text.IndexOf("(") + 1) : lvi.Text + " (") + nCount.ToString() + ")";
}
cbAllErrorReports.Text = (cbAllErrorReports.Text.Contains("(") ? cbAllErrorReports.Text.Substring(0, cbAllErrorReports.Text.IndexOf("(") + 1) : cbAllErrorReports.Text + " (") + nTotalCount.ToString() + ")";
int nTaggedCount = _lstReportRecords.Where(rr => rr.Description.Contains("Tagged")).Count();
cbTaggedRecords.Text = (cbTaggedRecords.Text.Contains("(") ? cbTaggedRecords.Text.Substring(0, cbTaggedRecords.Text.IndexOf("(") + 1) : cbTaggedRecords.Text + " (") + nTaggedCount.ToString() + ")";
Thank you all for your help and ideas.

Categories