Panel Does not update TreeView - c#

I have a TreeView that is displayed inside a panel. The data in the TreeView is based on data returned from the database. The first time, the data is correct. The second time, the TreeView is not refreshed, and the previous data is still showing in the tree. I checked the list that contain the data. The list returned the correct data. I've Google the issue, and could not resolved it with some of the answers that were posted. Here is a sample code of how the TreeView is being created and added to the Panel.
ReportGroups gr = new ReportGroups();
var Name = gr.GetReportName(groupID);
TreeView tr = new TreeView();
tr.BeginUpdate();
tr.Size = new Size(570, 600);
tr.Name = "Home";
tr.Nodes.Add("Reports Name");
tr.CheckBoxes = true;
if (Name.Count() > 0)
{
foreach (var item in Name)
{
if (item != null)
{
tr.Nodes[0].Nodes.Add(item.reportName);
}
}
}
tr.Nodes[0].ExpandAll();
tr.EndUpdate();
this.pDisplayReportName.Width = tr.Width * 2;
this.pDisplayReportName.Height = 300;
this.pDisplayReportName.Controls.Add(tr);
this.pDisplayReportName.Refresh();
What am I doing wrong?

try to add this.pDisplayReportName.Clear(); so data will not double up. :)

The easy option would be to use this.pDisplayReportName.Controls.Clear(); just after tr.EndUpdate();. But, this would cause an issue if you have other controls within the same Panel.
The best option would be to use this.pDisplayReportName.Controls.RemoveByKey("MyTree"); instead of this.pDisplayReportName.Controls.Clear();
And, another option would be to add a TreeView in design time (with name tr) rather than dynamically to the panel. Then, use tr.Nodes.Clear(); before tr.BeginUpdate(); and remove following two lines from your code.
TreeView tr = new TreeView();
.
.
.
this.pDisplayReportName.Controls.Add(tr);
Cheers

Related

Getting list of checked radiobuttons in .net

TLDR; Looking for a method to retrieve all radiobuttons by means of something like this... (psudo)
List<RadioButton> btn = new List<RadioButton>;
btn = stackPanel.getAllRadioButtons()
I am currently building a little quiz application in C#. I have functions that add the required GUI elements to a groupbox. I would like to know if there is any way that I can loop through the created elements (for instance radio buttons) to see which are checked.
Here is one of the functions along with how they are added to the window.
private void tfQ(string questionBody)
{
StackPanel questionPanel = new StackPanel{Orientation = Orientation.Vertical};
questionPanel.Children.Add(new Label { Content = questionBody });
GroupBox group = new GroupBox();
RadioButton trueRadio = new RadioButton();
trueRadio.Content = "True";
RadioButton falseRadio = new RadioButton();
falseRadio.Content = "False";
questionPanel.Children.Add(trueRadio);
questionPanel.Children.Add(falseRadio);
group.Content = questionPanel;
mainStack.Children.Add(group);
}
Constructor:
public quiz()
{
tfQ("This is a true/false question");
Window w = new Window();
w.Content = mainStack;
w.Show();
}
I have found many ways to do it in the C# scripting format
(using the Control function ...)
var checkedButton = container.Controls.OfType<RadioButton>()
.FirstOrDefault(r => r.Checked);
but I have not yet found a "programmatic" way of doing it. I have considered changing the type of void tfQ to a StackPanel, but that only helps me to easier loop through the stack panels, althought that only partly solves me problem - allows me to easier loop through the StackPanels but I still don't know how to get the RadioButtons on a panel.
P.S I am very new to C# - with experience in Java/C/C++/Python
I made use of the following code to cast the content into a new groupbox and stackpanel, respectively.
if (child is GroupBox)
{
if ((child as GroupBox).Content is StackPanel)
{
StackPanel d = (StackPanel)((GroupBox)child).Content;
}
}
This allowed me to cast the content into a local copy of a control. It may not be the best way - I'm sure it is very inefficient to be honest. Solved the problem.

asp.net Keep selected value on dynamically populated / created radiobuttonlist

I hope this question hasn't been asked before in this manner, if it has, I sadly can't find it!
I'm currently trying to create a User Control in which I want a client to select a questionnaire from a dropdownmenu which is dynamically populated on page_load like this:
DiscoverConnectData(); //gets the items from a database
var formItems = 0;
using (var osContext = new OrganizationServiceContext(_crmAccessor.OrganisationService))
{
var forms = (from f in osContext.CreateQuery("form") select f);
foreach (var form in forms)
{
var name = formulier.GetAttributeValue<string>("name");
var formId = formulier.GetAttributeValue<Guid>("formid");
DropDownList_RP.Items.Insert(formItems, new ListItem(naam, formId.ToString()));
formItems++;
}
}
I've also tried putting the above code inside an if (!Page.IsPostBack), however this didn't fix my problem... Okay, moving on. When the client has selected a questionnaire and hit a button, I have a piece of code that runs through all the questions and answers related to that specific questionnaire in a for each loop and creates Labels and RadioButtonLists for each question. First it loads the questions and answers related to the selected formId:
var curQuestionName = string.Empty;
var lblId = 0;
_formGuid = new Guid(DropDownList_RP.SelectedValue);
DiscoverConnectData();
using (var osContext = new OrganizationServiceContext(_crmAccessor.OrganisationService))
{
_answerList =
(from a in osContext.CreateQuery("answer")
join v in osContext.CreateQuery("question")
on a["question"] equals v["questionid"]
where ((EntityReference)v["form"]).Id == _formGuid
select new AnswerClass
{
AnswerQuestionId = v.GetAttributeValue<Guid>("questionid"),
QuestionName = v.GetAttributeValue<string>("name"),
Name = a.GetAttributeValue<string>("ansname"),
Score = a.GetAttributeValue<int>("score")
}).ToList();
}
After picking up this data and adding it to a list, I create the Questionnaire items:
foreach (var ant in _answerList)
{
if (ant.QuestionName != curQuestionName)
{
var vLabel = new Label();
var qid = "Q" + lblId;
vLabel.ID = qid;
vLabel.Text = ant.QuestionName;
Questionnaire.Controls.Add(vLabel);
_aRadioList = new RadioButtonList();
var rblId = "list" + lblId;
_aRadioList.ID = rblId;
Questionnaire.Controls.Add(_aRadioList);
Questionnaire.Controls.Add(new LiteralControl("<br />"));
curQuestionName = ant.QuestionName;
lblId++;
}
_aRadioList.Items.Add(new ListItem((" " + ant.Name), ant.Score.ToString()));
}
This all works fine and I'm getting a list with questions and answers neatly put underneath each other. So far so good!
However, the big problem I'm having is the losing of data on page postback. Whenever the user selects certain answers and I then want to show them a score, or do anything else - the page reloads and all my dynamically added controls are lost. Now I have fixed that issue by putting the relevant functionality above into a function and calling that again when I press the button to show a score, and this recreates the questions and answers, but I lose all the selectedvalues. I don't specifically even need to keep the controls on the page or anything, I just need to keep the selectedvalues in order to do stuff with them!
I can't create the controls on page_load or page_init because I need user input to know exactly which questionnaire to pull from the database. I can't use "hard-coded" values for the dropdownlist items either, because that's not the functionality we're aiming for. We need to pull it all from the database - which I can't access directly, I need to use the DiscoverConnectData() function.
Please, if anyone has any ideas in order to get the functionality I want (also if it means exploring a whole new 'avenue'), please let me know!

ListBox Items Disappear

I currently have a ListBox (called wafersListBox) bounded to an ArrayList of a certain object type (called wafers). When I want to add to the ListBox dynamically, I use the following code:
wafersListBox.DataSource = null;
wafersListBox.DataSource = wafers;
wafersListBox.Refresh();
This successfully changes the items in the ListBox, but all of the items disappear (they're still there and can be selected, but the user just can't see them).
Any ideas on how to fix this?
UPDATE:
This is my Wafer class:
public class Wafer
{
public string maID;
public string MID
{
get
{
return maID;
}
set
{
maID = value;
}
}
public Wafer(string m)
{
maID = m;
}
}
This is the code that I call, it adds a copy of the currently selected item to the listbox:
Wafer w = wafersListBox.SelectedItem as Wafer;
wafers.Add(w);
wafersListBox.DataSource = null;
wafersListBox.DisplayMember = "MID";
wafersListBox.DataSource = wafers;
wafersListBox.Refresh();
You should probably tell the wafersListBox what property to use as it's caption.
Do it like this;
wafersListBox.DisplayMember = "PropertyNameThatYouWantToShow";
Sorry - I'm not able to add an additional comment, which would have been preferable over writing a new "answer" but do you see any difference if you switch the position of the lines as below?
Wafer w = wafersListBox.SelectedItem as Wafer;
wafers.Add(w);
wafersListBox.DataSource = null;
wafersListBox.DataSource = wafers;
wafersListBox.DisplayMember = "MID";
wafersListBox.Refresh();
One other thing I just came across on a different SO posting (ListBox doesn't show changes to DataSource):
"There is also a bug in the list box which can cause this problem. If you set the SelectionMode to None this problem appears.
As a work around I set the selection mode to One and then back to None when updating the datasource."

TreeView Not Displaying Images from ImageList

I have a TreeView that displays CheckBoxes:
I want to check if a given directory contains an ".mdf" database and if it does, check whether it is attached on the selected server instance. If the database is attached I display an image against that node, and a different image if it is not attached. Note: The images are .png format, size 32x32...
I populate an ImageList from Properties.Resources
mainImageList = new ImageList();
mainImageList.Images.Add(Properties.Resources.Database);
mainImageList.Images.Add(Properties.Resources.DatabaseGrey);
I then loop through the tree and add the relevant image
public static void RecursiveAddImage(TreeNode treeNode, List<string> attachedList)
{
if (String.Compare(Path.GetExtension(treeNode.Text), ".mdf", true) == 0)
{
string databaseName = treeNode.Text.Replace(".mdf", String.Empty);
if (attachedList.Contains(databaseName))
{
treeNode.ImageIndex = 0;
treeNode.SelectedImageIndex = 0;
}
else
{
treeNode.ImageIndex = 1;
treeNode.SelectedImageIndex = 1;
}
}
foreach (TreeNode node in treeNode.Nodes)
RecursiveAddImage(node, attachedList);
}
The above code goes through the loop with no complaints, finds ".mdf"s and seems to add the relevant ImageIndexes but these do not show up in the TreeView. What am I doing wrong here and can I add the ImageList at design time (something I also can't seem to do)?
I have read several posts and ofcourse the MSDN documantation but I still can't seem to get it working. Any help as always, is much appreciated.
Make sure the TreeView control has the ImageList property set to the correct ImageList reference:
mainImageList = new ImageList();
mainImageList.Images.Add(Properties.Resources.Database);
mainImageList.Images.Add(Properties.Resources.DatabaseGrey);
treeView1.ImageList = mainImageList;
TreeNode.StateImageIndex= 0; would set the imagelist images. Make sure the imagelist is binded to Treeview control as mentioned above.

Tree View not showing update

I have a TreeView in my Windows Form user interface.
I want to fill it up from a database, but it does not refresh ever, even though if I WriteLine() every node, it is in memory as I expect.
In order to make it more easy to understand, I wrote a little example program that only has one button that creates a TreeView and a TreeView called treeView1 to display its content.
If anyone can tell me where I have misunderstood the use of the TreeView, it would be a tremendous help.
private void button1_Click(object sender, EventArgs e)
{
// create a tree
TreeView t = new TreeView();
TreeNode[] child = new TreeNode [1];
child[0]=new TreeNode("myCat");
child[0].Name = "IndependantOne";
TreeNode categoryNode = new TreeNode("catIdTag", child);
categoryNode.Name = "Citizen Cat 5239002147";
t.Nodes.Add(categoryNode);
// some stuff under the first node
TreeNode[] mouseNode = new TreeNode[1];
mouseNode[0] = new TreeNode("myMouse");
mouseNode[0].Name = "SqueakyOne";
TreeNode[] childItem = new TreeNode[1];
childItem[0] = new TreeNode("mouseIdTag", mouseNode);
childItem[0].Name = "Citizen Mouse 54655654649";
TreeNode eltNode = new TreeNode("Cheese", childItem);
eltNode.Name = "Emmental";
t.Nodes["Citizen Cat 5239002147"].Nodes.Add(eltNode);
// fill in the winform treeview
if (t != null)
{
//treeView1.Visible = false;
treeView1.BeginUpdate();
treeView1.Nodes.Clear();
treeView1.TopNode = t.TopNode;
foreach (TreeNode n in t.Nodes)
{
treeView1.Nodes.Add(n);
System.Diagnostics.Debug.WriteLine("Category Node contains: " + treeView1.Nodes[n.Name].Name + " at " + treeView1.Nodes[n.Name].Level);
foreach (TreeNode no in treeView1.Nodes[n.Name].Nodes)
{
System.Diagnostics.Debug.WriteLine("Category Node Nodes contains: " + no.Name);
}
}
/*
This part I tried and it doesn't work either, still add it in the question if anyone knows if it's wiser?
this.treeView1 = t;
this.treeView1.Location = new System.Drawing.Point(233, 12);
this.treeView1.Name = "treeView1";
this.treeView1.Size = new System.Drawing.Size(351, 277);
this.treeView1.TabIndex = 11;
this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
*/
treeView1.EndUpdate();
this.treeView1.Update();
this.treeView1.Refresh();
treeView1.Show();
//this.Refresh();
}
}
I also tried setting the treeView1 with
treeView1 = t;
It was not a success...
change
treeView1.Nodes.Add(n);
with
treeView1.Nodes.Add((TreeNode)n.Clone());
You cannot host the same node in more than one TreeView control.
Alternatively, you can remove nodes from source TreeView before adding them to other TreeView:
while(t.Nodes.Count != 0)
{
var node = t.Nodes[0];
t.Nodes.RemoveAt(0);
treeView1.Nodes.Add(node);
}
And I hope that there is a real reason why you create and fill tree view in your method instead of directly filling an already existing one. If it is not an intended behavior, remove the if block completely and change TreeView t = new TreeView(); to var t = treeView1.
This behavior is expected because with this code you`re confusing node-to-treeview relationship nature of the native .NET treeview control. Instead when moving nodes between treeviews (t -> treeView1) you need to clone them as suggested. Without that moved node still linked with the old treeview (see node.Treeview property) and because original tree (t) is not visible/added to any parent (form), I guess node will be invisible as well.
Also, the way you`re working with data loading (through creating new treeview) is pretty bad pattern. Instead you need to download your data (async I guess) into a temp buffer and recreate the treeView1 at once when data will be available with BeginUpdate/EndUpdate calls.
PS. Replacing treeView1 variable with 't' won't work as well because you don't replace the treeview control instance in the parent form/panel Controls property with this code.

Categories