Referencing the TreeView from other TreeViews - c#

I have a window form that has a control treeViewMain and in the code I have a list of treeViews that has variable number of treeViews. I want the treeViewMain to show anyone of the treeView from the list.
treeViewMain=treeViews[0];
but the form is not showing the tree. Also I have thoroughly checked the treeViews in the list, they are populating correctly.

Apparently simple assigning does not work. According to this post it seems that the parent of a TreeViewNode has to be unique and there by the "fruit" can only sit at one tree.
One workaround could be to set the parent property of the TreeView in your list to the treeViewMain:
treeViews[0].Parent = treeViewMain;
this will put the treeview from the list as a child inside treeViewMain. If you want to change that and load the next item from your list you need to make sure that the previously loaded item's parent property is set to null before assigning a new parent like in this example:
private int count = 0;
private void button_ChooseTreeView_Click(object sender, EventArgs e)
{
if (count > 0)
{
treeViews[count -1].Parent = null;
}
treeViews[count].Parent = treeViewMain;
count++;
}
The other possibility would be to clone each node and populate the treeViewMain each time you want to update the list items to the display

Related

SelectedNode is not working after refresh always showing null

Here i am trying to show the last selected node after reload of treeview but the selected node is always null. I am able to find the tree node from the treeview node collection but not able to assign it to tree view. Please suggeest:
public void LoadTreeViewData()
{
treeView.Nodes.Clear();// Clear any existing items
treeView.BeginUpdate(); // prevent overhead and flicker
LoadBaseNodes();//Populate all nodes
treeView.EndUpdate();
treeView.Refresh();
treeView.ExpandAll();
if (!string.IsNullOrEmpty(m_oSelectedNode_NAME))
{
TreeNode[] treeNodes = treeView.FlattenTree()
.Cast<TreeNode>()
.Where(r => Convert.ToDecimal(r.Tag) == Convert.ToDecimal(m_oSelectedNode_NAME))
.ToArray();
treeView.SelectedNode = treeNodes[0];
treeView.Focus();
}
}
This problem is happening because i am trying to Update the selected node inside the Tree view after label edit event. I put a timer inside After label edit to resolve the issue and set the Selected node it works for me.
var tnm = new Timer() { Enabled = true, Interval = 30 };
tnm.Tick += delegate {
SetSelectioNode();
tnm.Dispose();
};

How to remove item from generic list that relates to item in listbox?

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.

WPF How to remove a grid?

In my WPF application I have created and added a new Grid, I already know how to remove all Children from it:
private void ClrScr()
{
for (int i = GridName.Children.Count - 1; i >= 0; i--)
{
GridName.Children.RemoveAt(i);
}
}
But I have no idea how to kill the grid itself, my tries:
GridName.Exit/Disable/Something; /// <--- no Idea what am I doing...
this.Controls["GridName"].DIEEEE;
Sadly I have failed... I am very new to WPF, I've mostly played with WinForms... Help?
You can remove the grid using it's parent.
e.g: If you have three grids named grid1, grid2 and grid3 in a dock panel control named main, you can remove these grids like this:
main.Children.Remove(this.grid1);
main.Children.Remove(this.grid2);
main.Children.Remove(this.grid3);
All the controls in WPF has parent except form. If the Grid you are going to delete is the first then you can do this:
var parent = myGrid.Parent;
Window window = parent as Window;
if(window!=null)
window.Content = null;
Sometimes Grid can be child of a control which doesn't have Content property. If it doesn't have Content then it must have Children or Items.

Two comboBoxes with same members, when one selected, other must be unable to select same member

So I have two comboBoxes (comboBoxFromAccount and comboBoxToAccount). Each has the same datasource, which is AccountsList (a list of BankAccount objects that was passed from the parent form).
I would like to make it so that if an item is selected in one of the comboBoxes, it would no longer be selectable in the other. The way I'm trying to do this is by copying the list of BankAccounts from the comboBoxFromAccount to the comboBoxTo account, and removing the selected index of comboBoxFromAccount from the comboBoxToAccount.
I think I'm close, but what seems to happen is I have a blank comboBoxToAccount.
Here is my code:
private BankAccountCollection accountsListTransferTo = new BankAccountCollection();
// public property for passing collection data to the dialog
public BankAccountCollection AccountsList
{
get { return accountsListTransferTo; }
set { accountsListTransferTo = value; }
}
// Initial loading
private void TransferFundsDialog_Load(object sender, EventArgs e)
{
textBoxAmount.Text = String.Empty;
textBoxAmount.Select();
comboBoxFromAccount.DataSource = AccountsList;
accountsListTransferTo.AddRange(AccountsList); // Copy content
accountsListTransferTo.Remove(comboBoxFromAccount.SelectedItem as BankAccount); // Remove item
comboBoxToAccount.DataSource = accountsListTransferTo; // Data binding
}
private void comboBoxFromAccount_SelectedIndexChanged(object sender, EventArgs e)
{
accountsListTransferTo.Clear(); // Clear list, if you don't to it, AddRange will just add more items.
accountsListTransferTo.AddRange(AccountsList); // Copy ALL accounts
accountsListTransferTo.Remove(comboBoxFromAccount.SelectedItem as BankAccount); // Remove selected, so user cannot transfer to same account
// Refresh data binding
comboBoxToAccount.DataSource = null;
comboBoxToAccount.DataSource = accountsListTransferTo;
// Select very first item in "TO" combobox
comboBoxToAccount.SelectedIndex = 0;
}
Help would be appreciated.
Try removing the line
comboBoxToAccount.DataSource = null;
I have a vague recollection about comboboxes having problems with this.
Another possible problem that I can see is that you are using accountsListTransferTo both as your master collection and the one where you are removing the selected account from. Every time comboBoxFromAccount_SelectedIndexChanged is called one more account will disappear from the collection (and therefore from the available options in comboBoxToAccount).
I think I have seen comboboxes behave in a way where the SelectedIndexChanged (or a similar) event is triggered as new items are being added. If that is the case here it will explain the empty comboBoxToAccount, because comboBoxFromAccount_SelectedIndexChanged will run once for each bank account being added, essentially removing them from the master list and then rebinding the reduced list. You can easily verify this with a break point in your event handler.

silverlight, ListBox navigation to new page with object possible?

Hi all i have a listbox MainListBox where i add items to dynamically.
Now i want to navigate to DetialsPage.xaml.cs when i choose an item in the listbox.
where i can then display my info about the selected item.
private void SetListBox()
{
foreach (ToDoItem todo in itemList)
{
MainListBox.Items.Add(todo.ToDoName);
}
}
MainListBox_SelectionChanged ("Generated by visual studio 2010 silverlight for windows 7 phone)
// Handle selection changed on ListBox
private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// If selected index is -1 (no selection) do nothing
if (MainListBox.SelectedIndex == -1)
return;
// Navigate to the new page
NavigationService.Navigate(new Uri("/DetailsPage.xaml?selectedItem=" + MainListBox.SelectedIndex, UriKind.Relative));
// Reset selected index to -1 (no selection)
MainListBox.SelectedIndex = -1;
}
in DetailsPage.xaml.cs is the next method. ("Generated by visual studio 2010 silverlight for windows 7 phone)
I'm aware that the below method does not do what i try.
// When page is navigated to set data context to selected item in list
protected override void OnNavigatedTo(NavigationEventArgs e)
{
string selectedIndex = "";
if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
{
int index = int.Parse(selectedIndex);
DataContext = App.ViewModel.Items[index];
}
}
I would like to access the selectedIndex and call my methods of my object that is in the MainListbox
so Basicly:
Mainlistbox => select item => send that item to details page => details page access the item and call methods on the item (object)
I'm sure this is a basic question tough it seems hard to find any specifics on it. i would like to add that this is my first windows phone 7 app.
There are many ways you can pass an object from page to page:
serialize and deserialize like Dennis said, but this, although feasable, is not practical, unless you want to save the object in isolated storage and retrieve it later.
Place an object in the App.cs class, which is accessible to all pages. Set your object in the master page, retrieve it from the Details page.
Code to put in App.cs: MyObject selectedObject;
Code to put in MasterPage.cs: application.selectedObject = MainListBox.selectedItem;
Code to put in DetailsPage.cs: MyObject selectedObject = application.seletedObject;
You can set the Object in the DataContext of your LayoutRoot, but i don't have the code for that on top of my head.
The answer here is simple - you cannot directly pass an object to another page. You can serialize it to JSON or XML and then deserialize it on the target page, but the serialized item will still have to be passed as a parameter.
Instead of sending the selectedindex as a query string parameter you could send the ID for the object or similar, something that uniquely can identify the object.
Then in the details page you could fetch the correct object from the same datasource that the main list box get its data from (in your case "itemList" which could come from e.g. IsolatedStorage).
If itemList is instantiated and kept only within the main page then you won't be able to fetch the item by ID from the details page. So in that case you'd need to move the itemList to some static or app level storage.
HTH

Categories

Resources