Tree View / File View Control for C# - c#

I have been looking for a C# tree control for displaying a file system that has the following capabilities:
Select a starting directory. I don't always want to start at a "default" top directory level.
The ability to grab an event when the user double clicks on a file in the tree. I want to handle opening the file within my application.
I have been looking at this C# File Browser. Unfortunately, I have not been able to figure out how to do meet my second need. (If anybody can clear that up for me, I would like that even better.) Thanks for any help.

Hi I've looked at the C# File Browser and Find a way to handle your 2nd requirement. You could try adding ItemActivate event on the fileView control (under Browser User Control of the FileBrowser project) and get the selected item(s) when handling it. ItemActivate event is triggered on every double click of an item. Here is the sample code:
private void fileView_ItemActivate(object sender, EventArgs e)
{
//Loop thru all selected items
foreach (ListViewItem item in ((BrowserListView)sender).SelectedItems)
{
//Do your stuuf here. MessageBox is only used for demo
MessageBox.Show(item.Text);
}
}
Edit by Original Question Writer: To see all of the source, look at the code posted by cipriansteclaru in the comments section of the FileBrowser. You have to actually edit the FileBrowser source to gain this functionality (which is what this answer was demonstrating).

Related

Raise PropertyChangeNotification on Property in different class (different ViewModel)

Motivation:
I'd like to have a 'File->Save As' MenuItem that behaves just like in Visual Studio. When there is nothing opened it says "Save Selected Items as..."
and when a particular file (e.g. SomeFile.cs) is opened in a tab, the MenuItem reads "Save SomeFile.cs as...".
My App architecture (MVVM, using MVVM Light):
MainWindow.xaml:
<MenuItem Header="{Binding SelectedProjectName}" HeaderStringFormat="Save {0} As..." />
MainWindowViewModel:
I hold a collection of opened tabs (opened files)
private ObservableCollection<BaseProjectViewModel> _projects;
I have a property returning a currently selected tab
public BaseProjectViewModel SelectedProject
{
get
{
return _selectedProject;
}
set
{
if (_selectedProject == value)
{
return;
}
_selectedProject = value;
RaisePropertyChanged("SelectedProject");
RaisePropertyChanged("SelectedProjectName");
}
}
I created a property returning the name of the file in the currently selected tab
public string SelectedProjectName
{
get
{
if (SelectedProject == null)
{
return "Selected Item";
}
return SelectedProject.SafeFileName;
}
}
BaseProjectViewModel serves as a base class for various file types. Each file type has its own class derived from BaseProjectViewModel. It has properties like for example
PaneHeader that returns a string to be displayed in pane header,
SafeFileName that returns just the file name of a path etc...
Question:
When I change the name of the file (thus changing properties of the BaseProjectViewModel) how do I trigger RaisePropertyChanged of the SelectedProjectName in MainWindowViewModel?
What is the cleanest way to do that?
My thoughts
I thought of two possible ways to do that, but i don't know if any of them is the correct way to do it:
(In short) Listening to CollectionChanged on _projects. When there is add/remove -> subscribe/ubsubscribe an event handler that would
look if the PropertyName is the one we are looking for and if yes subsequently call RaisePropertyChanged("SelectedProjectName")?
Use something like MVVM Light Messaging.
Question 2: If you don't suggest any other way and in fact you'd suggest one of these two - could you please elaborate on advantages and disadvantages?
EDIT
I created a very simple project to demonstrate the issue - LINK.
When you run the project:
'New' adds a new TabItem. When text is edited, the TabHeader is decorated with an asterisk.
'Save {0}' menu item "saves" the selected TabItem (simulated by removing the asterisk). I didn't want to complicate the example and introduce a SaveFileDialog and such.
'Save As {0}' menu item simulates Save as in such a way that it adds 'X' character to the end od Tab header string.
When no TabItem is selected, the {0} resolves to "Selected Item".
When you have one tab selected, you click SaveAs() and open the menu, you'll notice that change has not been raised on SelectedProjectName property. When you click another tab and then select the first one back, the change is propagated.
Edit for Erno: What I fail to understand is this: Let's suppose I have a special menu for each document type. Let's suppose I have one particular tab selected (with it's own menu enabled/visible, the other collapsed). How is it going to help me propagate the PropertyChanged of PaneHeader property in BaseProjectViewMode to SelectedProjectName in MainWindowViewModel? If you have time could you please demonstrate it on the example? I also would like to ask you what would be an alternate way if I wanted/neede to do the wiring? Thank you in advance.
From your options I dislike #1 because it might introduce a lot of wiring that is hard to track and maintain.
Option #2 might be OK but could end up in the same wiring mess as #1 but because of the messaging it will be less visible.
I'd consider a different approach:
put a menu in the MainWindow that is responsible for handling commands when no files are open or selected.
when a document is opened in a view and has the focus: replace the current menu with the document specific menu. (like MDI applications work in WinForms)
This way you can customize the Menu per document (type) and it does not require the event dependencies.

How to find the id of dragged control

I am implementing drag and drop functionality among lables. I want to find the ID of a dragged control like label, button, etc so that I can assign a text to it.
I am not sure how to get that data through the events. Any suggestion will help.
you can use Drag_Enter and DragOVer Events, to achieve the goal DragEnter Doucmentation
and DragOver Documentation
alternativily you can check following tutorials
Code project : Drag and Drop in Windows Forms
Code Project : Drag and Drop UI in WinForms
check out System.Windows.Forms.DragEventArgs e
void MyControl_DragDrop(object sender, DragEventArgs e)
{
var controlBeingDrag = (Label)sender; // cast from object
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop, false);
}
the control sending drag event is object sender
Assuming you've done your research to figure out how to do drag and drop of controls on your form (and your question is really only limited to your question title: How to find the id of dragged control) most standard WinForm events provide a parameter (object sender) which represents the control used to invoke the event. You should be able to get its ID as you would normally.
Apparently it is less obvious how to consistently get the ID from a WinForm control. Luckily, Brian McMaster has a (fairly old meaning only possibly relevant) MSDN blog post for doing just that. In .NET 3.5 I'd probably use this old post as the start to an extension method for control objects.
If your question is broader than that then you may benefit from #Ravi's links, but on SO we generally expect that you do your own research. Please be sure to do so before asking questions.

how to identity a click out of the form and 1 more question

i want to build a simple program which help you selecting pictures.
if you have lot of picture and you want to choose some of them then you see them 1 by 1 and when you see a picture you would lik to save on other folder on your pcyou just press a button ,lets say f5 and the program copy the phtot from the path you looking at to the destiny folder.
for that program i need to ask how to know if someone pressed f5 out of the form area and how to know in which path the user looking at.(i want to build it for myself atm so if its help i look with microsoft office picture manager)
about the clicking i search a little and get something named global clicking and hooks which i dont understand so much and about identify the path i have no idea .
tyvm for help:)
I'm not sure I follow the rest -- but if you want to capture the keypress event, simply add an event handler for KeyPress and determine if the pressed key is equal to the F5 button by using the Keys constants.
Here is a project on Code Project that does exactly what you need :)
http://www.codeproject.com/KB/cs/globalhook.aspx
The key press event wont work with the following (Link):
TAB
INSERT
DELETE
HOME
END
PAGE UP
PAGE DOWN
F1-F2
ALT
Arrow keys
Note: I think there is a typo on the page and the F1-F2 really should be F1-F12.
When you decide the key for your on key press event for the form area you are talking about it will look like this:
private void Form_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar == [keypressvalue])
{
//do your copy logic
}
}
[keypressvalue] will be the code for F5 if you choose to use this. I have found a mix of values for this (i could not get my test keypress event to pick up the F5 event, hence my note above) so i recommend running the event once with a brake point, inspecting the code, then brake and update your code, then test your logic.
Like the rest i'm not really sure what you want your custom logic to do.
Clairifcation: I'm trying to understand your question, so what you want is: When in Microsoft Picture Manager when you press F5 you want the image that is currently being viewed to be moved to a particular directory? Now if you are writing your own picture viewer and move software then i think we can help if it is the above i'm not really sure you can do that.

Responding to HTML hyperlink clicks in a C# application

Question:
How can I detect and handle clicks on hyperlinks in a Windows.Forms.WebBrowser control in C#?
Background:
My C# application does not carry a centralised help file. Instead, all the pieces that make up the app are allowed to display their own little help topic. This was done because the application is merely a framework, and it's hundreds of little plug-ins that actually make it useful.
Each class can implement an interface which registers it with the help UI. All my help topics are html strings (but I'm not particularly wedded to that), many of which are created programmatically at runtime.
The problem is that these topics are all isolated. I'd very much like to be able to include a "See also" section which will open other help topics. But how can I handle hyperlink-clicks in a Windows.Forms.WebBrowser?
Much obliged,
David
If I understand correctly, you would like to override your hyperlink-clicks with your own Form or UI. Well, if that's the case, you put your code on the OnNavigating event of Web Browser and make e.Cancel = true so that it will not navigate the URL specified by your hyperlink.
some snippet:
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
e.Cancel = true;
SeeAlsoFrm seeAlso = new SeeAlso();
seeAlso.showDialog();
}
that is based on my understanding. :)=)

What winforms control to display message with minor html formatting?

Is there a control I can use to display a short message that contains minor html formatting (eg one or more links). I'd prefer not to use the WebBrowser control (suggested here) as it's a bit heavy for what I want, so any other suggestions welcome.
If a user does click a link from my message I want it to be opened in their default browser, not within my application.
I do use the infragistics controls so one of those would be fine but I don't see any that will do this.
You can use the controls in this article on CodeProject or you can use them as an example for how to roll your own.
I have implemented an HTML control for .NET which may do what you want: see http://www.modeltext.com/html for details.
The control can display HTML including links, and doesn't use a browser.
What happens when the user clicks on a link is up to you: the control generates an event, which can be handled by the application in which the control is embedded; so, your application would instantiate the control, fill it with HTML, install an event handler, and launch the browser when its event handler is told that the user has clicked on a link.
If all you need is the link functionality you can use the LinkLabel control. Assuming that you have set the LinkLabel.Tag property to the url the following code will open the default browser and open the specified web page:
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start((string) ((sender as LinkLabel).Tag));
}
I think the best solution in my case might be to use RichTextBox, which has a property DetectLinks and a LinkClicked event that tells you which link is clicked. I'll look into this further...

Categories