|
Is this event handler actually called from anywhere? The only reference I can see is from the items that it creates, which would suggest it never runs.
If you want to add the files to the tree, then you probably want to do so from the folder_Expanded handler:
void folder_Expanded(object sender, RoutedEventArgs e)
{
TreeViewItem item = (TreeViewItem)sender;
if (item.Items.Count == 1 && item.Items[0] == dummyNode)
{
item.Items.Clear();
try
{
string path = item.Tag.ToString();
foreach (string dir in Directory.GetDirectories(path))
{
TreeViewItem subitem = new TreeViewItem();
subitem.Header = new DirectoryInfo(dir).Name;
subitem.Tag = dir;
subitem.FontWeight = FontWeights.Normal;
subitem.Items.Add(dummyNode);
subitem.Expanded += folder_Expanded;
item.Items.Add(subitem);
}
foreach (string file in Directory.GetFiles(path))
{
TreeViewItem fileitem = new TreeViewItem();
fileitem.Header = new FileInfo(file).Name;
fileitem.Tag = file;
fileitem.FontWeight = FontWeights.Normal;
item.Items.Add(fileitem);
}
}
catch (Exception) { }
}
}
If you want to add the files to a different control when a folder node is selected, then you'll need to do so in the SelectedItemChanged[^] event instead.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard, thanks for your reply. Interestingly about 1 am I rewrote my code similar to what you suggested and it worked. I added my files to the listview and they appear there. The only thing that baffles me is that now that the files are listed where I want them. I can't select any of them. I've made sure that the listview is enabled. Any ideas I's appreciate it. Here's the code I used, but your code seems much cleaner. Later today I will try your suggestion.
foreach (string dir in Directory.GetDirectories(item.Tag.ToString()))
{
TreeViewItem subitem = new TreeViewItem();
subitem.Header = new DirectoryInfo(dir).Name;
subitem.Tag = dir;
subitem.FontWeight = FontWeights.Normal;
subitem.Items.Add(dummyNode);
subitem.Expanded += folder_Expanded;
item.Items.Add(subitem);
}
foreach (string file in Directory.GetFiles(item.Tag.ToString()))
{
TreeViewItem fileitem = new TreeViewItem();
fileitem.Header = new DirectoryInfo(file).Name;
fileitem.Tag = file;
fileitem.FontWeight = FontWeights.Normal;
if (file.Contains(".csv"))
{
listView1.Items.Add(fileitem);
}
|
|
|
|
|
Maybe it's because you're adding a TreeViewItem to a ListView . You should be adding a ListViewItem[^] instead.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard,
Thank you for your feedback. The code you gave works fine. However, I'm not clear on your recent message regarding ListViewItem.
My Drives, Folders and subfolders appear in the Treeview (Left Panel) and I want the Files to appear in the ListView (Right Panel). So I'm not clear what you are suggesting. In the code below I try what I think you are saying but Header is not contained in ListViewItem. Can you explain further?
void folder_Expanded(object sender, RoutedEventArgs e)
{
TreeViewItem item = (TreeViewItem)sender;
if (item.Items.Count == 1 && item.Items[0] == dummyNode)
{
item.Items.Clear();
try
{
string path = item.Tag.ToString();
txtPath.Text = path;
listView1.Items.Clear();
foreach (string dir in Directory.GetDirectories(path))
{
TreeViewItem subitem = new TreeViewItem();
subitem.Header = new DirectoryInfo(dir).Name;
subitem.Tag = dir;
subitem.FontWeight = FontWeights.Normal;
subitem.Items.Add(dummyNode);
subitem.Expanded += folder_Expanded;
item.Items.Add(subitem);
}
foreach (string file in Directory.GetFiles(path))
{
ListViewItem fileitem = new ListViewItem();
fileitem.Header = new DirectoryInfo(file).Name;
fileitem.Tag = file;
fileitem.FontWeight = FontWeights.Normal;
listView1.Items.Add(fileitem);
}
|
|
|
|
|
The ListViewItem[^] class doesn't contain a Header property, because it wouldn't make sense in a ListView .
You specify the content of the item using the Content property. The ListView[^] documentation has an example of how you set up a template to display the items.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard, thank you very much. Using ListView resolved my issue. You've been a tremendous help!
|
|
|
|
|
i have created a crystal report with access db in c#. but when i run this report its want login information. how can i solve this problem?
|
|
|
|
|
What login information? Database credentials? Or other licensing needs?
Member 13417072 wrote: i have created a crystal report with access db in c# Then why you don't know how to solve where you have requested the credentials?
The thing is, you would require to provide the database credentials, so make sure you know what credentials you used while creating the report so that the same can be used during deployment and rest. If you do not know what credentials you used, you would need to recreate the report with the credentials that can be used on the server for access.
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
Hey guys i would like to know if it's better to start developing using java or c++?
I m still a beginner by the way.
|
|
|
|
|
A.Mr.Nice.Guy wrote: I m still a beginner by the way. That is glaringly obvious - you posted this in the C# forum.
Your choice of language should depend on the job/work you expect to do and you will no longer be able to choose just one language/tool set.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Depends on the application: an android app wouldn't be in C++, it would be in Java, or C# via Xamarin.
An iOS app would be Objective C or C# via Xamarin.
A PC desktop app could be C++, C, VB, C#, possibly Java but I wouldn't recommend it, F#, or any of countless others.
Me? I'd suggest starting to learn with C# rather than either of C++ or Java - it's a cleaner, simpler solution than C++ and has a much better IDE than Java. But it's your choice, not ours!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I agree with OriginalGriff. Think of language choice as being tools in your toolbox. As a beginner, you have limited tools, so learn the tool that will do what you are wanting to do the most of. A screwdriver handle can still drive a nail, but think of how much more efficient a hammer is.
|
|
|
|
|
|
So, we wouldn't actually be "helping" you, we'd be writing the whole thing for you. That's not what this site is about. If you need someone to write code for you, pay someone. There are plenty of sites that will do this for you.
This space for rent
|
|
|
|
|
So,updating is = to rewrite completely this program ?
Lol
|
|
|
|
|
Yeah, it is!
I've seen plenty of code in my lifetime that should never have been written. I'm looking a piece of code right now that needs to be "updated" but is written so badly that it would be faster and cheaper to scrap the damn thing and rewrite it from scratch so it's understandable.
|
|
|
|
|
Thanks for the humiliation,im really proud to get at least 1 from this forum.
1)I post my question on C# section and i made some mistakes,just because of my "perfect" english,so pls sorry.
2)However,i didn't speak good in english;
3)I had specified that im 0 on C# so for this reason i come on this forum just to ask some help....
If you can't or u won't to help me ,just stop here.
Thanks you.
|
|
|
|
|
I can't see the question, so...
I take it the question was closed because it was incomprehensible?
|
|
|
|
|
This was the text
I have 0 knowledge on C# and i need your help to update this program here :
http://urlremoved.
win6x_registry_tweak is an awesome tool for everyone,that allows you to :
unhide packages and similar stuff from your image(online/offline)of Windows.
I'm deploying Windows 10 Pro and i really wanna to get rid from a lot of spyware and bloatware
that in default mode are not visible via Dism .
Pls,if you can help me,you will help the entire comunity of Windows 10 users.
This space for rent
|
|
|
|
|
Another newb who has no clue how anything works.
|
|
|
|
|
No one humiliated you for your lack of English. You came on this site and said there was an application that didn't do what you wanted it to. You asked for someone to fix it for you. What did you think was going to happen?
This space for rent
|
|
|
|
|
Hi. I'm new to C#. I really want to learn to write code in a proper way. In the following example i was trying to do this:
1. On Form there is black Panel
2. On this Panel there are three green PictureBoxes
3. Whenever one of these PicureBoxes is clicked, Panel changes its color to red.
Now, the code I've came up with is:
using System;
using System.Drawing;
using System.Windows.Forms;
public class MainForm : Form
{
public class SomeClass : PictureBox
{
public SomeClass()
{
this.BackColor = Color.Green;
this.MouseDown += new System.Windows.Forms.MouseEventHandler(mouseDown);
}
public void mouseDown(object sender, MouseEventArgs e)
{
panel1.BackColor = Color.Red;
}
}
Panel panel1 = new Panel();
SomeClass someObject = new SomeClass();
SomeClass someObject2 = new SomeClass();
SomeClass someObject3 = new SomeClass();
public MainForm()
{
this.Controls.Add(this.panel1);
this.panel1.Size = new System.Drawing.Size(200, 600);
this.panel1.BackColor = Color.Black;
this.panel1.Controls.Add(this.someObject);
this.panel1.Controls.Add(this.someObject2);
this.panel1.Controls.Add(this.someObject3);
this.someObject.Location = new System.Drawing.Point(0, 0);
this.someObject2.Location = new System.Drawing.Point(0, 100);
this.someObject3.Location = new System.Drawing.Point(0, 200);
}
public static void Main()
{
Application.Run(new MainForm());
}
}
And the big problem is that I cannot access panel1. What's the most elegant solution to this?
|
|
|
|
|
This is already some quite 'special' code, not using the standard Visual Studio WinForms template or its designer.
But without going into that, here are 2 possible solutions to your problem:
- SomeClass is a Control (inherited from PictureBox) that exposes public events:
you can just subscribe to the mousedown event in the MainForm class (just as you set its location):
public class MainForm : Form
{
public MainForm()
{
someObject.MouseDown += mouseDown;
}
private void mouseDown(object sender, MouseEventArgs e)
{
panel1.BackColor = Color.Red;
}
}
2. if you don't want to do it that way, you can 'supply' the panel reference to the constructor of SomeClass:
public class SomeClass : PictureBox
{
Panel panel1;
public SomeClass(Panel mainPanel)
{
panel1 = mainPanel;
}
}
and in the MainForm class you do:
SomeClass someObject = new SomeClass(panel1);
|
|
|
|
|
Since you derive your SomeClass from PictureBox, the best way is is to do it properly: create an event in your SomeClass which indicates to the outside world "I need attention".
Your panel handles that event, and changes itself.
That way, the control doesn't need to know about any containing controls, and the containing control becomes responsible for indicating the change itself:
public class SomeClass : PictureBox
{
public SomeClass()
{
this.BackColor = Color.Green;
this.MouseDown += new System.Windows.Forms.MouseEventHandler(mouseDown);
}
public void mouseDown(object sender, MouseEventArgs e)
{
OnFocussed(e);
}
public event EventHandler Focussed;
protected virtual void OnFocussed(EventArgs e)
{
EventHandler eh = Focussed;
if (eh != null)
{
eh(this, e);
}
}
}
And your Panel handles it:
{
MyPictureBox1.Focussed += MyPictureBox_Focussed;
MyPictureBox2.Focussed += MyPictureBox_Focussed;
MyPictureBox3.Focussed += MyPictureBox_Focussed;
}
void MyPictureBox_Focussed(object sender, EventArgs e)
{
SomeClass sc = sender as SomeClass;
if (sc != null)
{
BackColor = Color.Red;
}
}
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
If I interpret your description literally, there;s a trivial solution that requires no exposure of your subclassed PictureBox to its Form context:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace CPWorking
{
public partial class PBX : PictureBox
{
public PBX()
{
InitializeComponent();
this.BackColor = Color.Green;
this.MouseDown += OnMouseDown;
}
private void OnMouseDown(object sender, MouseEventArgs mouseEventArgs)
{
this.Parent.BackColor = Color.Red;
}
}
} This exploits the fact that the Parent of a Control is a Control.
But, this is definitely not elegant, and probably not what you are looking for, so: let's make it more interesting, more like what you might do in a "real" app.
Like the approach OriginalGriff showed you, I will use a "broadcast" model, but I will implement it in a way that, imho, is more modern (uses a newer .NET facility), and, imho, more clearly expresses intent when viewed by newcomers to WinForms:
public partial class PBX : PictureBox
{
public PBX()
{
InitializeComponent();
this.MouseDown += OnMouseDown;
}
public Action<string, Color> PBXOnColorChanged { set; get; }
private void OnMouseDown(object sender, MouseEventArgs mouseEventArgs)
{
if (PBXOnColorChanged != null) PBXOnColorChanged((sender as PBX).Name, this.BackColor);
}
} Instead of using the "classic" syntax for a Delegate/EventHandler, this uses the newer syntax of the Action structure; do keep in mind that an Action is a Delegate. In this example we handle/respond-to the Action being invoked in the Form host context:
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.PBXOnColorChanged += PbxOnColorChanged;
pictureBox2.PBXOnColorChanged += PbxOnColorChanged;
pictureBox3.PBXOnColorChanged += PbxOnColorChanged;
}
private void PbxOnColorChanged(string pbxName, Color pbxBackColor)
{
} Yes, you could set the Panel BackColor to Red, if that's your thing
But, let's make this more like a real-world app by making the Form context not just get a one-way message, but, also send back a result to the sub-classed PictureBox that will determine what happens in the MouseDown event of the PictureBox:
public partial class PBX : PictureBox
{
public PBX()
{
InitializeComponent();
this.BackColor = Color.Green;
this.MouseDown += OnMouseDown;
}
public Func<string, Color, Color> PBXOnColorChanged { set; get; }
private void OnMouseDown(object sender, MouseEventArgs mouseEventArgs)
{
if (PBXOnColorChanged != null)
{
Color result = PBXOnColorChanged((sender as PBX).Name, this.BackColor);
if (result != this.BackColor && result != Color.Empty)
{
this.BackColor = result;
}
else
{
this.BackColor = Color.Green;
}
}
}
} Here we use the 'Func syntax that specifies the Type of its return value/object as well as its parameter Types.
In the Form context:
private Color PbxOnColorChanged(string pbxName, Color pbxBackColor)
{
Color NextColor = Color.Empty;
switch (pbxName)
{
case "pictureBox1":
if (pbxBackColor == Color.Green) NextColor = Color.Yellow;
break;
case "pictureBox2":
if (pbxBackColor == Color.Green) NextColor = Color.Red;
break;
case "pictureBox3":
break;
}
return NextColor;
} Note the event handler returns a Color value. Note that PictureBox3 wiii not change its Color when clicked because the event handler returns Color.Empty when it is clicked.
Elegant ? I don't think so ... trivial, really, but you can see some hopefully useful ideas in this example:
1. encapsulation, loose coupling: the sub-classed PictureBox has no access to any of its context's (Form) objects, properties, fields, etc.; it publicly exposes only one access point, a kind of "socket" (Action, or Func) that consumers of the object can "inject" a reference to executable code into at run-time.
2. the Form context, by design, limits its access to the instances of thr PictureBox it uses to a very structured message passing facility. It defines the PictureBox instances it creates as private.
3. the newer Action and Func language features make the task of implementing Delegates and EventHandlers much easier, an d, imho, easier to maintain. About the only limitation I am aware of compared to Delegates is that Delegate can use the 'params feature for passing a variable length Array of arguments; Action and Func cannot.
4. structured message passing is a useful model for dynamic exchange of data and implementation of object interaction.
«While I complain of being able to see only a shadow of the past, I may be insensitive to reality as it is now, since I'm not at a stage of development where I'm capable of seeing it.» Claude Levi-Strauss (Tristes Tropiques, 1955)
|
|
|
|
|