I am beginner in c#. In my project, I populated a xml file inside a TreeView control. If the xml file is large, the TreeView control is showing the data with scroll bars. Beside this, whenever the user double clicks a node I am showing a panel beside the selected node something like this..
When I scroll the TreeView Control :
My question is how to make the panel attached to treeView control so that eventhough the user scrolls the TreeView control the panel should also move along with the selected node.
Well, hard to do since TreeView doesn't have a Scroll event. It isn't reliable anyway since nodes can be expanded and collapsed, changing the position and visibility of the node. The backup plan is to use a Timer. This worked well:
private void timer1_Tick(object sender, EventArgs e) {
var node = treeView1.SelectedNode;
if (node == null || !node.IsVisible) panel1.Visible = false;
else {
panel1.Visible = true;
var nodepos = treeView1.PointToScreen(node.Bounds.Location);
var panelpos = panel1.Parent.PointToClient(nodepos);
panel1.Top = panelpos.Y;
}
}
Related
I'm working on a form application in c# that includes TreeView in it. What I want to do is to attach a panel to each node so whenever user clicks on a node the panel will be updated according to the node selected.
The problem I'm facing is that when I select a node, the application does nothing but when I select another node then the application shows the content related to previously selected node. Means that App is always getting content related to last selected node not the current one. For Example If I'll select "Text" node, the label will show nothing and after that If I'll select some other node like "Appearance" the label will show "Text" which was the last selected node.
Following here is the image of my Form that contains TreeView.
For testing purpose I'm just storing the selected node's value in my label's text
Here's the code.
public partial class TextEditor_Preferences : Form
{
public TextEditor_Preferences()
{
InitializeComponent();
}
List<Panel> myPanels = new List<Panel>(); //Ignore this line of code !
private void SideBar_MouseClick(object sender, MouseEventArgs e)
{
label1.Text = SideBar.SelectedNode.ToString();
}
}
Can anobody suggest me a method?
If I'm missing something or question is not valid, Please let me know explicitly. Thanks
TreeView control has the AfterSelect event you should write your code in that handler.
public YourForm()
{
InitializeComponent();
treeView.AfterSelect += TreeViewAfterSelect;
}
private void TreeViewAfterSelect(object sender, TreeViewEventArgs e)
{
string nodeText = treeView.SelectedNode.Text;
// Update the panel here accordingly
}
Maybe try using the AfterSelect event rather than MouseClick. e.g.
private void Sidebar_AfterSelect(object sender, TreeViewEventArgs e)
{
label1.Text = e.Node.Text;
}
So, I have a GridView on my MasterPage with an onclick Event which returns the ID (there are device components in this grid and I need their ID from the database) from that row.
The Onclick event which is bound in the RowDataBound event
e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(this.selectionGridView, "Select$" + e.Row.RowIndex);
When I click a row the SelectedIndexChanged Event gets fired
protected void selectionGridView_SelectedIndexChanged(object sender, EventArgs e)
{
GridView selectedGridView = sender as GridView;
//Saves the Database ID of the selected data
Session["selectedDevID"] = selectedGridView.SelectedRow.Cells[0].Text;
_Auswahl.FillTree();
}
And this piece of code calls a method from the page I want to show a Treeview with more stuff etc.
_Auswahl.FillTree();
Everthing is fine, all methods are called correctly, but on the _Auswahl Page, I can't add things to the TreeView (or to the Page at all). All my controls return a NullReferenceExeption, and if I initialize them, then this stuff won't show up
I tried everything, I guess I miss some little thing or forgot something, but I can't get this thing right ._.
Edit: I don't want to Redirect to that Page again, I want to "refresh" the treeview (which is located in an UpdatePanel)
Edit2:
This is my ___FillTree()_ Methode (Excluded the whole Linq stuff etc etc und left just some basic stuff)
public void FillTree()
{
//Add child node value
TreeNode child = new TreeNode("Manager");
//Add parent value
TreeNode parent = new TreeNode("Development");
//Add child to parent
parent .ChildNodes.Add(child);
//Add parent to tree
TreeView1 = new TreeView();
TreeView1.Nodes.Add(parent);
TreeView1.DataBind();
TreeView1.ExpandAll();
}
So I've been able to populate a TreeView with the tabnames in WPF/XAML binding but haven't done this before with C# Windows Forms.
I want to have the treeview display the project name based on what file is open and then tabcontrol names below it (these are static -- one is called editor and the other fields).
I'll add a context menu later, but the sole purpose would be to make the tabs visible based on their state with click events from the treeview.
My problem is I can't figure out how to associate them in the treeview. I found this code, can anyone tell me if I'm on the right track here?
private void treeView1_AfterSelect(Object sender, TreeViewEventArgs e)
{
// Set the visibility of the tabpages from the treeview
if ((e.Action == TreeViewAction.ByMouse))
{
if (e.Node.Name == "Editor")
{
this.editForm.tabControl1.SelectedTab = editForm.Editor;
}
if (e.Node.Name == "Fields")
{
this.editForm.tabControl1.SelectedTab = editForm.Fields;
}
}
}
You could use the TreeNodes's Tag property to hold the associated Tab Name.
if (e.Action == TreeViewAction.ByMouse)
{
TabPage p = tabControl1.TabPages[e.Node.Tag]
tabControl1.SelectedTab = p;
}
I have a treeView with many nodes. I want that some nodes change their image when node collapsed/expanded. How can I do it ?
Unfortunately, TreeNode don't have properties like ExpandNodeImage, CollapseNodeImage \
TreeView can change very often, so nodes can be deleted/added.. i can delete child nodes and so on...
Maybe, there is a class like ExpandAndCollapseNode ?
1). Add an ImageList Control to your WinForm.
2). Populate the ImageList with the pictures/icons you wish to change/display in response to what the user does at run-time with the TreeView, such as expanding, or collapsing nodes.
3). Assign the 'ImageList Control to the 'ImageList property of the 'TreeView
At this point you may want to make an initial pass over the TreeView, assuming it is populated, assigning the Node.ImageIndex property to point to the Image ... in the ImageList ... you want to use for the Node depending on whether it has children, or whatever.
4). If a user expands a Node, for example, you can use the BeforeExpand Event of the TreeView to change the Node's picture : like so : in this case we use the index of the Picture in the ImageList :
private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
e.Node.ImageIndex = 3;
}
5) You can also set the Node's image by using the ImageKey property which is the string name of the Image
6) There lots of other possible Node Picture variations to use : check out : SelectedImageIndex and SelectedImageKey : You can change Node pictures in the BeforeSelect, AfterSelect and BeforeExpand, events also, depending on the effect you are after.
BeforeCollapse
BeforeExpand
AfterCollapse
AfterExpand
Use both ImageIndex & SelectedImageIndex:
private void treeView_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
e.Node.ImageIndex = 1;
e.Node.SelectedImageIndex = 1;
}
TreeViews have the following events that will be be fired when nodes are collapsed/expaned.
BeforeCollapse
BeforeExpand
AfterCollapse
AfterExpand
It's better to use :
treeNode.SelectedImageIndex = 1;
you can use the events AfterCollapse & AfterExpand (that are avilable on the TreeView itself) to modify the image of a node.
you can get the node using the TreeViewEventArgs input parameter:
private void treeView1_AfterCollapse(object sender, TreeViewEventArgs e)
{
e.Node.ImageIndex = 1;
}
I have changed the Treeview.HideSelection = false;
But how do I insure that when focus is lost that the selected item remains the original selected color?
EDIT:
I have a listview on a form that holds a list of process events. Alongside the Treeview on the same form is a series of selections that the user completes to classify the event in the listview. However, when the user makes a selection on one of the classification controls the blue highlighted selected Treeview item turns to a grey color. I was hoping to find the property that defines this color to make it the same color blue.
Any suggestions.
Update:
public partial class myTreeView : TreeView
{
TreeNode tn = null;
public myTreeView()
{
InitializeComponent();
}
protected override void OnAfterSelect(TreeViewEventArgs e)
{
if (tn != null)
{
tn.BackColor = this.BackColor;
tn.ForeColor = this.ForeColor;
}
tn = e.Node;
base.OnAfterSelect(e);
}
protected override void OnBeforeSelect(TreeViewCancelEventArgs e)
{
e.Node.BackColor = Color.Green;
e.Node.ForeColor = Color.White;
base.OnBeforeSelect(e);
}
protected override void OnGotFocus(System.EventArgs e)
{
base.OnGotFocus(e);
}
protected override void OnLostFocus(System.EventArgs e)
{
if (tn != null)
{
tn.BackColor = Color.Green;
tn.ForeColor = Color.White;
}
// tn.BackColor = Color.Red;
base.OnLostFocus(e);
}
}
Setting ListView.HideSelection to true means that when focus is lost, it will hide the selection. By setting HideSelection to false, the selected item will still have the color indicator showing which item is selected.
Generally, you don't. The change in color is one of the visual cues that indicate which control has the focus. Don't confuse your customers by getting rid of that.
If you want to buck the convention, then you can make your control owner-drawn, and then you can paint the items whatever color you want.
Another option, in your case, is to use a drop-down combo box instead of a list box. Then the current selection is always clear, no matter whether the control has the focus. Or, you could consider using a grid, where each event has all its settings given separately, and then "selection" doesn't matter at all.
If I were doing it, I would simply have an extra Label alongside the ListView, above the classification controls being selected, that would indicate which process event has been selected. You can also use said Label to add extra details about the event (if any).
This way, you are sticking to standard UI conventions and making it that much clearer to the user what their current selection is.
I use this code; it works for me.
design: Mytreeview.HideSelection = True you will manual highlight the lose focus selected node.
Private Sub MyTreeview_Leave(sender As Object, e As EventArgs) Handles MyTreeview.Leave
MyTreeview.SelectedNode.BackColor = Color.LemonChiffon
End Sub
Private Sub MyTreeview_BeforeSelect(sender As Object, e As TreeViewCancelEventArgs) Handles MyTreeview.BeforeSelect
If MyTreeview.SelectedNode IsNot Nothing Then
MyTreeview.SelectedNode.BackColor = Color.White
End Sub
I like the HideSelection = false; answer, because:
It's easy
I have a search function that cycles through the nodes and marks the relevant ones by changing it's background to yellow, when the user clicks on the node a textbox fills with the relevant info attached to that node, before I used this method, if the user clicked on the textbox to scroll through it, it would unhighlight the node and make it hard to keep track of which node was selected, this way it is still highlighted in a light gray colour showing it is not in focus, opposed to the blue highlight which is used when it is in focus. I could have 'painted' the node but with the yellow background for search results would have made life more complicated than it needed to be.
Did I mention it was easy?