ive got a problem with getting my data from a xml file to an listbox.
this is the data i want to get in my listbox:
<gjester>
<gjest>
<id>test</id>
<fornanv>test</fornanv>
<etternavn>test</etternavn>
<adresse>test</adresse>
<telefonnr>test</telefonnr>
</gjest>
</gjester>
and i created a listbox in my gui. But i don't know what to write in my code.
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
I don't know what to write here
}
There are approximately a bazillion ways to add items from an XML file to your listbox but a good place to start would be the MSDN documentation for the XMLTextReader class and the ListBox.Items.Add() method.
Also - you will probably want to do this somewhere other than the SelectedIndexChanged event on the listbox. For learning purposes, try it on a button click.
Good luck - and after looking into the above I'm sure someone will help you if you haven't figured it out.
This is .NET 4.0 (VS2010 C#) and is completely untested but may give you a start....
private void FillListBoxWithThingsIWantToSelect()
{
XDocument ListBoxOptions = XDocument.Load(Filename);
foreach (XElement element in ListBoxOptions.Root.Elements())
{
if (element.Name.LocalName.Contains("gjester"))
{
foreach (XElement subelement in element.Elements())
{
if (subelement.Name.LocalName.Contains("gjest"))
{
// What do you want to add? The Attribute? Element value
listbox1.Items.Add(element.Value.ToString());
}
}
}
}
}
Would help if you listed your platform and what you want in the listbox.
You want to call this from your constructor.
Can use dictionary object to bind the data from XML to Listbox.
var dic = (from order in ds.Tables[0].AsEnumerable()
select new
{
UserView = order.Field<String>("Value"),
DevView = order.Field<String>("id")
}).AsEnumerable().ToDictionary(k => k.DevView, v => v.UserView);
Click here for reference
Related
I'm new in using mshtml package library from .NET and, as such, I'm struggling to read the input element rendered in WPF web browser control.
I have researched through the internet and stumbled on this link. How to get the value of an input box of the webbrowser control in WPF?
But in my case, I want to read/get the input element by class, not by name attribute. Unfortunately, I know that GetElementByClass is not an option in WebBrowser.Document
This is my code - When I tried to run the debugger, the code doesn't continue inside the foreach loop ):
private void browser_LoadCompleted(object sender, NavigationEventArgs e)
{
IHTMLElementCollection elementCollection;
IHTMLDocument3 dom = (IHTMLDocument3)browser.Document;
elementCollection = dom.getElementsByTagName("input");
foreach (HtmlElement item in elementCollection)
{
if (item.OuterHtml.Contains("input_search"))
{
//Do something else here...
}
}
}
A screen capture of the DOM of the page
Any help is greatly appreciated.
I have a Treeview created in virtual mode which have 4 levels of nodes and at the page load the root element and first level elements are loaded and rest will be loaded based on dynamically using treeFolderList_VirtualModeCreateChildren event.
This is what I have so far
protected void treeFolderList_VirtualModeCreateChildren(object source, TreeViewVirtualModeCreateChildrenEventArgs e)
{
List<TreeViewVirtualNode> children = new List<TreeViewVirtualNode>();
if (e.NodeName == null)
{
AppendChildNode(children, "root", "All Domains", false);
}
else
{
if (e.NodeName.Contains("root"))
{
PopulateChildNodes(false, children);
}
else
{
if (!(e.NodeName.StartsWith("u_")))
{
PopulateUserChildNodes(false, children, GetDomainBase(e.NodeName), e.NodeName);
}
else
{
TreeViewVirtualNode tvNode = (TreeViewVirtualNode)treeFolderList.Nodes.FindByName(e.NodeName);
TreeViewVirtualNode tvNodeParent = (TreeViewVirtualNode)tvNode.Parent;
string tvParentNodeName = tvNodeParent.Name;
PopulateUserChildNodes(true, children, GetDomainBase(tvParentNodeName), e.NodeName);
}
}
}
e.Children = children;
}
This work as expected and it creates the children elements when expanding nodes respectively. My problem is I have check boxes for each node and I need to be able to save the Treeview in a way when I reload/redirect to the page it would reflect the nodes I have selected.
Is there a way to achieve this?
There are several ways to try to achieve this:
Configure ASPxTreeList.SettingsCookies element (probably the easiest solution), in particular the StoreSelection attribute:
<dvx:ASPxTreeList ... >
...
<SettingsCookies Enabled="true" StoreSelection="true" />
...
</dvx:ASPxTreeList>
If SettingsCookies doesn't work try saving and restoring the TreeList layout manually using ASPxTreeList.ClientLayout event. Define the event handler first:
<dvx:ASPxTreeList OnClientLayout="dvxTreeList_ClientLayout"... >
...
</dvx:ASPxTreeList>
and follow the example in the doc to handle this event. This way ASPxTreeView as well as the ASPxGridView provide the node/column format data in the e.LayoutData string which can be saved to DB and then restored back.
Store the TreeView selected node keys in Session and restore them using callbacks:
Example: ASPxTreeList - How to store the selection between requests
I personally would not rely on cookies and would try methods #2 and #3 first. #2 has been working nicely for me with ASPxGridView and #3 we use in a complex ASPxTreeView setup which also tracks the selection of hidden nodes.
I hope the examples in the linked docs should be easy enough for you to copy and modify. If not, comment what is not working for you.
HTH
I have searched around but could not find any references.
How do I delete an item in a generic list that relates to items in a listbox?
I currently have a public static List<Employees> and a listbox named lstRecords, I can remove the item in the listbox just fine, but either everything is removed from the list or nothing at all.
This was my first set of code I was working with:
private void DeleteRecord()
{
if (lstRecords.Items.Count > 0)
{
for (int i = 0; i < lstRecords.Items.Count; i++)
{
if (lstRecords.GetSelected(i) == true)
{
Employees employeeRecord = lstRecords.SelectedItem as Employees;
employee.Remove(employeeRecord);
}
}
lstRecords.Items.Remove(lstRecords.SelectedItem);
}
}
}
This is my 2nd set of code I was working with, I have my List right under partial class, but this is all contained in a method.
private void DeleteRecord()
{
ListBox lstRecords = new ListBox();
List<object> employee = new List<object>();
employee.RemoveAt(lstRecords.SelectedIndex);
lstRecords.Items.RemoveAt(lstRecords.SelectedIndex);
}
So far I haven't gotten either set of code to work the way I would like it to, I'm obviously doing something wrong.
I have a few other blocks of code I played around with but these seemed to be headed in the right direction.
Eventually I'll need to be able to double click an item in the list to pull up the properties menu.
Your code runs fine you just have to make some small changes.
The first code block is Ok however I dont know where your lstRecords are.
But have a look at this just copy the code and run it after you have some records in your employee object.
It's createing a listbox in code then adds it to the form(Winforms) and having the lstRecords globaly.
ListBox lstRecords;
private void IntializeDemoListbox()
{
lstRecords = new ListBox();
this.Controls.Add(lstRecords);
foreach (var item in employee)
{
lstRecords.Items.Add(item);
}
}
And then you will be able to use your first set of code the other set will be like this.
private void DeleteRecord()
{
employee.RemoveAt(lstRecords.SelectedIndex);
lstRecords.Items.RemoveAt(lstRecords.SelectedIndex);
}
What you want to do is bind your ListBox to you List of employees. This post shows the binding and the comments shows the removing code as well. The idea is that when you remove an item from the DataSource, then you won't see it in the ListBox.
Binding Listbox to List<object>
The problem with the DeleteRecord() method is that the lstRecords object you just created isn't the ListBox that is on the form.
What i'm trying to do is select an item in my listview, and it works! That is it works once, the first time a select an item it go's well, the second time a get an argument out of range exception on features[0].SubItems[1].Text; on the zero.
this is what i have:
private void listViewFeatures_SelectedIndexChanged(object sender, EventArgs e)
{
ListView.SelectedListViewItemCollection features = listViewFeatures.SelectedItems;
string feature = features[0].SubItems[1].Text;
BL_AddReport addReport = new BL_AddReport(this.databaseConnectionString);
Dictionary<string, bool> pictures = addReport.GetpicturesFromFeature(feature);
foreach (KeyValuePair<string, bool> pic in pictures)
{
if (pic.Value) {
pictureBoxCar.Image = Image.FromFile(pic.Key);
}
else
{
pictureBoxEquip.Image = Image.FromFile(pic.Key);
}
}
}
Does anyone know what the problem is?
I'm betting you'd get this exception if you clicked off of the listview as well.
Remember that this event is for selection changes.. which may mean that something was selected and now nothing is. In fact, according to this an event is fired once for every thing that is selected. Take a look at that link for more information and designs around this problem if that is the case for you.
Otherwise just check to make sure that your "features" variable has anything inside of it before indexing into it
I'm busy with a simple application. It reads xml and puts the information in a treeview.
I do this by creating TreeNodes and nest them, and finaly, return the root treenode. Because I want to show some extra information when a treenode is selected, I put the information in the tag property of the TreeNode. In this way, I should be able to retrieve the information when the node is selected.
But when I try to retrieve the information in the Tag property, it says the value = null.
Here is the code where I fill the tag. This is in a function which is recursively used to read the XML dom. treeNode is a paramater given to this function.
if (treeNode.Tag == null)
{
treeNode.Tag = new List<AttributePair>();
}
(treeNode.Tag as List<AttributePair>).Add(new AttributePair(currentNode.Name, currentNode.Value));
This is the event where a treenode is selected
private void tvXML_AfterSelect(object sender, TreeViewEventArgs e)
{
if (tvXML.SelectedNode.Tag != null)
{
}
if (e.Node.Tag != null)
{
}
}
Both values evaluate to null. How can I solve this problem?
The code you posted should work as-is. Something else in your code, code that you didn't post here, is causing this to break. It could be clearing the Tag, it could be a data binding set on the tag, etc.
Without seeing all your code, the best I can do is guess and help you isolate the problem.
Here's what I'd do: setup Visual Studio to allow stepping into the .NET framework source code with the debugger. Then, set a breakpoint on the setter for the TreeNode.Tag property. After you set the tag in your code to your AttributePair List, see when it gets set again. The breakpoint will hit, you'll look at the stack trace and see what exactly is clearing your Tag property.
If using Tag property isn't in principle, I'm recommend inherit TreeItem:
public class MyTreeNode : TreeNode
{
public List<AttributePair> list;
public MyTreeNode (string text,List<AttributePair> list) : base(text)
{
this.list = list;
}
//or
public MyTreeNode (string text) : base(text)
{
this.list = new List<AttributePair>();
}
}
And use it:
private void tvXML_AfterSelect(object sender, TreeViewEventArgs e)
{
if (tvXML.SelectedNode is MyTreeNode)
{
MyTreeNode selectedNode = tvXML.SelectedNode as MyTreeNode;
selectedNode.list.Add(.., ..);
}
if (e.Node is MyTreeNode)
{
MyTreeNode node = e.Node as MyTreeNode;
node.list.Add(.., ..);
}
}
Maybe you are assigning the values after Select event. Otherwise you can maintain a dictionary of TreeNode and tag values as workaround.
Try declaring/initialising your List object somewhere above (outside of the inner scope you are in) and when you assign to the .tag property - don't create a new list but rather assign previously created List object.
private TreeViewItem _subsender;
private object _senderTag;
public TreeViewItem _sender
{
get {
return _subsender;
}
set
{
_senderTag = value.Tag;
_subsender = value;
}
}
Got the same problem this the solution that i found
Just don't use the .tag but _senderTag
(don't change the lines in the set for some reason :D )
(You cant just reset the tag (maybe new TreeViewItem ))