Non-autogenerated ContextMenu will not display - c#

I'm trying to create a context menu which will be able to add extra menu items, with an attached child menu, as required.
As it stands my code generates no errors and it will flow through the program as intended, but it doesn't display anything on screen.
I thought I might be missing the link between the ContextMenuStrip and the component it attaches to so I had a look at how the code is auto generated in the design view... however I have no access to the "Controls" object to do Control.Add(this.whatever) and it appears not to be part of the using System... collection.
I have left my testing for this commented out below.
The SuspendLayout and ResumeLayout methods are another thing I looked into after reading that you should call them before and after making changes to UI components. I am not sure if I am correctly using them as they appeared to do nothing, I'm unsure when they are actually required to be used.
Would appreciate being pointed towards what ever I have missed out on, thanks :)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
namespace Context
{
class PopulateMenu
{
private void PopulateSubMenu(string fileName, ContextMenuStrip parent, EventHandler onClickDelete, EventHandler onClickView)
{
try
{
Image delete = Properties.Resources.RO_Mx2_24_delete;
Image view = Properties.Resources.OpenFolder1;
parent.SuspendLayout();
//Parent
ToolStripMenuItem attachedFiles = new ToolStripMenuItem(fileName, Properties.Resources.NewDocumentHS);
//Kids
attachedFiles.DropDownItems.Add(new ToolStripMenuItem("View File", view, onClickView));
//if (!hotfolder)
//{
attachedFiles.DropDownItems.Add(new ToolStripMenuItem("Delete File", delete, onClickDelete));
//}
//else
//{
// attachedFiles.DropDownItems.Add(new ToolStripMenuItem("Remove File", delete, onClickDelete));
//}
// Attach kid to parent
parent.Items.Add(attachedFiles);
parent.ResumeLayout();
}
catch { Exception e; }
}
private void BuildHotMenu(List<FileInfo> files, ContextMenuStrip parent)
{
try
{
parent.SuspendLayout();
parent.Items.Add(new ToolStripMenuItem("Hot Folder Files"));
parent.Items.Add(new ToolStripSeparator());
foreach (FileInfo fileInfo in files)
{
PopulateSubMenu(fileInfo.Name, parent, null, null);
}
parent.ResumeLayout();
}
catch { Exception e; }
}
private void BuildDropMenu(List<FileInfo> files, ContextMenuStrip parent)
{
try
{
parent.SuspendLayout();
parent.Items.Add(new ToolStripMenuItem("Dropped Files"));
parent.Items.Add(new ToolStripSeparator());
foreach (FileInfo fileInfo in files)
{
PopulateSubMenu(fileInfo.Name, parent, null, null);
}
parent.ResumeLayout();
}
catch { Exception e; }
}
public void SummonContextMenu()
{
try
{
ContextMenuStrip attachedFilesWithMenu = new ContextMenuStrip();
////ContextMenu
//attachedFilesWithMenu.Name = "attachFilesWithMenu";
//attachedFilesWithMenu.Size = new Size(61, 4);
//ToolStrip ToolStrip1 = new ToolStrip();
////ToolStrip
//toolStrip1.ContextMenuStrip = attachedFilesWithMenu;
//toolStrip1.Location = new Point(0, 0);
//toolStrip1.Size = new Size(284, 25);
//toolStrip1.Name = "toolStrip1";
//toolStrip1.Text = "toolStrip1";
//// Form2
////
//AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
//AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
//ClientSize = new System.Drawing.Size(284, 262);
//Controls.Add(this.toolStrip1);
//ResumeLayout(false);
//PerformLayout();
//static directory for testing only
ConfigurationInformation configurationInformation = new ConfigurationInformation();
configurationInformation.HotFolderPath = "C:\\Users\\alexh\\HotFolder\\";
//Store DirectoryInfo
DirectoryInfo dirInfo = new DirectoryInfo(Environment.ExpandEnvironmentVariables(configurationInformation.HotFolderPath));
//Create list to store file details
List<FileInfo> hotFiles = new List<FileInfo>();
foreach (FileInfo fileInfo in dirInfo.GetFiles())
{
hotFiles.Add(fileInfo);
}
BuildHotMenu(hotFiles, attachedFilesWithMenu);
BuildDropMenu(hotFiles, attachedFilesWithMenu);
}
catch { Exception e; }
}
public PopulateMenu()
{
}
//private System.Windows.Forms.ToolStrip toolStrip1;
//private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
}
}

Well that was actually quite simple. You just have to do it from a Form.
//this.Literally = theAnswer;
this.ContextMenuStrip = myContextMenu;

Related

How do I get the value of NotifyIcon.Icon property programmatically?

I know this is such a noob question, but I am trying to learn and test. One of my self-imposed challenges is creating a NotifyIcon in the systray (easy) and then clicking a button and making the icon change on interval from green to red based on the current icon value. However, when I try to read the value of NotifyIcon.Icon, it is just (Icon). I expect it to be an ico file I have in Properties.Resources (i.e. Properties.Resources.icon.ico).
using System;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Threading;
namespace TEST_NotifyIconChange
{
public partial class MainWindow : Window
{
private NotifyIcon notifyIcon = new NotifyIcon();
private ContextMenuStrip ContextMenuStrip_System_Tray = new ContextMenuStrip();
private DispatcherTimer iconAnimationTimer = new DispatcherTimer();
public MainWindow()
{
InitializeComponent();
iconAnimationTimer.Interval = TimeSpan.FromSeconds(1);
iconAnimationTimer.Tick += IconAnimationTimer_Tick;
iconAnimationTimer.IsEnabled = false;
ResetAll();
}
private void ResetAll()
{
notifyIcon.ContextMenuStrip = ContextMenuStrip_System_Tray;
notifyIcon.Icon = Properties.Resources.icon_green;
notifyIcon.Text = "I am just a standard icon";
notifyIcon.Visible = true;
}
private void ChangeColorButton_Click(object sender, RoutedEventArgs e)
{
if (iconAnimationTimer.IsEnabled == false)
{
iconAnimationTimer.IsEnabled = true;
iconAnimationTimer.Start();
}
else
{
iconAnimationTimer.Stop();
iconAnimationTimer.IsEnabled = false;
}
}
private void IconAnimationTimer_Tick(object sender, EventArgs e)
{
if (notifyIcon.Icon == Properties.Resources.icon_green)
{
Console.WriteLine("Changing to red");
notifyIcon.Icon = Properties.Resources.icon_red;
}
else
{
Console.WriteLine("Changing to green");
notifyIcon.Icon = Properties.Resources.icon_green;
}
}
}
}
notifyIcon.Icon == Properties.Resources.icon_green will never return true, because every time that you call it, it returns a new instance of the Icon.
You don't need to track the assigned icon; instead, you need to track of the status and based on the status, assign an icon. To track the status you can use Tag property of NotifyIcon or a member field of the Form. But do not look at the Icon property as a status indicator.
Just in case you are interested to compare two Icon objects to see whether they are same icon, you can use the following method:
bool AreIconsEqual(Icon ico1, Icon ico2)
{
byte[] bytes1, bytes2;
using (var fs = new MemoryStream())
{
ico1.Save(fs);
bytes1 = fs.ToArray();
}
using (var fs = new MemoryStream())
{
ico2.Save(fs);
bytes2 = fs.ToArray();
}
return bytes1.SequenceEqual(bytes2);
}

C# Open listView Item from treeView item location

I'm needing a way to allow the files that are displayed in the ListView to be opened. The Items In the ListView are displayed from the TreeView. Take a look at my code below to see in more detail.
Code for this form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace OSTP
{
public partial class User1FileExplorer : Form
{
public User1FileExplorer()
{
InitializeComponent();
PopulateTreeView();
this.treeView1.NodeMouseClick +=
new TreeNodeMouseClickEventHandler(this.treeView1_NodeMouseClick);
}
private void User1FileExplorer_Load(object sender, EventArgs e)
{
}
private void PopulateTreeView()
{
TreeNode rootNode;
DirectoryInfo info = new DirectoryInfo(#"C:\Users\Oliver\Documents\.OSTP\User1\Files\Documents");
if (info.Exists)
{
rootNode = new TreeNode(info.Name);
rootNode.Tag = info;
GetDirectories(info.GetDirectories(), rootNode);
treeView1.Nodes.Add(rootNode);
}
}
private void GetDirectories(DirectoryInfo[] subDirs,
TreeNode nodeToAddTo)
{
TreeNode aNode;
DirectoryInfo[] subSubDirs;
foreach (DirectoryInfo subDir in subDirs)
{
aNode = new TreeNode(subDir.Name, 0, 0);
aNode.Tag = subDir;
aNode.ImageKey = "folder";
subSubDirs = subDir.GetDirectories();
if (subSubDirs.Length != 0)
{
GetDirectories(subSubDirs, aNode);
}
nodeToAddTo.Nodes.Add(aNode);
}
}
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
TreeNode newSelected = e.Node;
listView1.Items.Clear();
DirectoryInfo nodeDirInfo = (DirectoryInfo)newSelected.Tag;
ListViewItem.ListViewSubItem[] subItems;
ListViewItem item = null;
foreach (DirectoryInfo dir in nodeDirInfo.GetDirectories())
{
item = new ListViewItem(dir.Name, 0);
subItems = new ListViewItem.ListViewSubItem[]
{new ListViewItem.ListViewSubItem(item, "Directory"),
new ListViewItem.ListViewSubItem(item,
dir.LastAccessTime.ToShortDateString())};
item.SubItems.AddRange(subItems);
listView1.Items.Add(item);
}
foreach (FileInfo file in nodeDirInfo.GetFiles())
{
item = new ListViewItem(file.Name, 1);
subItems = new ListViewItem.ListViewSubItem[]
{ new ListViewItem.ListViewSubItem(item, "File"),
new ListViewItem.ListViewSubItem(item,
file.LastAccessTime.ToShortDateString())};
item.SubItems.AddRange(subItems);
listView1.Items.Add(item);
}
listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
}
}
}
As you can see from my code I use a TreeView to select the directory for the files then the ListView to display the files inside of the directory.
I would like it so when the user double clicks the file in the ListView it opens. And when I say open I'm not meaning open a text file as such. So lets say the user double clicks text file 1 in the ListView, I would like it to show User1TextFile1.cs. This is because the text files are loaded into a text box.
I know this is a little complicated, so If I have missed anything please drop a comment.
Thanks.
UPDATE
http://pastebin.com/BgVdLavL
UPDATE2
When I add MessageBox.Show("" + lvHti);
To find out which ListViewItem was clicked or doubleclicked use the MouseClick or the MouseDoubleClick event.
Here you can either code simply:
ListViewItem lvItem = null;
if (listView1.SelectedItems.Count > 0)
lvItem = listView1.SelectedItems[0];
provided the ListView has MultiSelect = false.
If you allow multiple selections you need to do a HitTest to find out where the user has clicked:
ListViewItem lvItem = null;
ListViewHitTestInfo lvHti = listView1.HitTest(e.Location);
if (lvHti.Item != null) lvItem = lvHti.Item;
Now you can access the Item and/or its SubItems to process the choice.

How can i add to the listView the searching results in real time?

My program search for text inside files.
But what i want to do is to see the searching progress in real time.
I want to add the current file name search in to listView and also to display to a progressBar the percentages from 0 to 100%.
I added a backgroundworker but the way i'm using the ReportProgress is not working good. I need to wait for it to finish the foreach in the FindLines method and then only in the end i see the items in the listView and even then the items are a big mess.
This is the line that report:
backgroundWorker1.ReportProgress(0, fi.Name);
This is a screenshot of the items in the listView when the foreach is over:
What i want to do is to display the items adding to the listView in real time without waiting first to the operation over and also to add each item to a line and if a line is too long then later i will add a tip baloon or something. But now the listView look like a big mess.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;
namespace Search_Text_In_Files
{
public partial class Form1 : Form
{
StreamWriter w = new StreamWriter(#"e:\textresults.txt");
public Form1()
{
InitializeComponent();
backgroundWorker1.RunWorkerAsync();
}
public List<string> FindLines(string DirName, string TextToSearch)
{
int counter = 0;
List<string> findLines = new List<string>();
DirectoryInfo di = new DirectoryInfo(DirName);
if (di != null && di.Exists)
{
if (CheckFileForAccess(DirName) == true)
{
foreach (FileInfo fi in di.EnumerateFiles("*", SearchOption.AllDirectories))
{
if (string.Compare(fi.Extension, ".cs", true) == 0)
{
backgroundWorker1.ReportProgress(0, fi.Name);
using (StreamReader sr = fi.OpenText())
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
if (s.Contains(TextToSearch))
{
counter++;
findLines.Add(s);
}
}
}
}
}
}
w.Close();
}
return findLines;
}
private bool CheckForAccess(string PathName)
{
// Determine if the path is a file or a directory
if (File.Exists(PathName) == true)
return CheckFileForAccess(PathName);
if (Directory.Exists(PathName) == true)
return CheckFolderForAccess(PathName);
return false;
}
private bool CheckFileForAccess(string FileName)
{
FileSecurity fs = new FileSecurity(FileName, AccessControlSections.Access);
if (fs == null)
return false;
AuthorizationRuleCollection TheseRules = fs.GetAccessRules(true, true, typeof(NTAccount));
if (TheseRules == null)
return false;
return CheckACL(TheseRules);
}
private bool CheckFolderForAccess(string FolderName)
{
DirectoryInfo di = new DirectoryInfo(FolderName);
if (di == null)
return false;
DirectorySecurity acl = di.GetAccessControl(AccessControlSections.Access);
if (acl == null)
return false;
AuthorizationRuleCollection TheseRules = acl.GetAccessRules(true, true, typeof(NTAccount));
if (TheseRules == null)
return false;
return CheckACL(TheseRules);
}
private bool CheckACL(AuthorizationRuleCollection TheseRules)
{
foreach (FileSystemAccessRule ThisRule in TheseRules)
{
if ((ThisRule.FileSystemRights & FileSystemRights.Read) == FileSystemRights.Read)
{
if (ThisRule.AccessControlType == AccessControlType.Deny)
return false;
}
// Run as many other checks as you like
}
return true;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
FindLines(#"d:\c-sharp", "string s1 = treeView1.SelectedNode.Tag as string;");
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
listView1.Items.Add(e.UserState.ToString());
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
}
}
}
This is a screenshot of the items in the listView when the foreach is
over: What i want to do is to display the items adding to the listView
in real time without waiting first to the operation over and also to
add each item to a line and if a line is too long then later i will
add a tip baloon or something. But now the listView look like a big
mess.
Did you try setting your Listview's view property to "List" ? That should display the file names in individual lines.
I think you should look into ObservableCollection(T). The collection fires events when items are added to it, and the UI element should automatically detect these changes and update the view.
At least that is how it works in WPF, but i think it should work as well in WinForms.

Overwrite file only works once

I have a simple program that copies files and directories from one place to another. I have it set-up that if there are any exceptions (such as if access to the path is denied) it will create a log file with the error.
I have a button that when pressed, performs the copy action. Everything works fine the first time I press the button and the log file is either created or overwritten with the appropriate error messages.
However, if I press the button a second time, the text file is not overwritten and instead the error messages append. If I close out of my program and run it again, the file is overwritten on the first button press. Any thoughts would be greatly appreciated.
target is a string filepath which I'm getting from a FolderBrowserDialog and taking the selected path and setting it to a textbox. loglist is just a simple List<string> I'm using to store the error messages from any exceptions that occur during the copy process.
public partial class Form1 : Form
{
static List<string> logList = new List<string>();
public Form1()
{
InitializeComponent();
}
private static void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
if (source.FullName.ToLower() == target.FullName.ToLower())
return;
if (Directory.Exists(target.FullName) == false)
{
Directory.CreateDirectory(target.FullName);
}
foreach (FileInfo fi in source.GetFiles())
{
try
{
fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
}
catch (Exception ex)
{
logList.Add(ex.Message);
}
}
foreach (DirectoryInfo diSourceSub in source.GetDirectories())
{
DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSub.Name);
CopyAll(diSourceSub, nextTargetSubDir);
}
}
private void directoryPickerBtn1_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderDialog = new FolderBrowserDialog();
DialogResult folderResult = folderDialog.ShowDialog();
if (folderResult == DialogResult.OK)
{
directoryTextbox1.Text = folderDialog.SelectedPath;
}
}
private void directoryPickerBtn2_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderDialog = new FolderBrowserDialog();
DialogResult folderResult = folderDialog.ShowDialog();
if (folderResult == DialogResult.OK)
{
directoryTextbox2.Text = folderDialog.SelectedPath;
}
}
private void copyBtn_Click(object sender, EventArgs e)
{
string source = (directoryTextbox1.Text);
string target = (directoryTextbox2.Text);
DirectoryInfo dirSource = new DirectoryInfo(source);
DirectoryInfo dirTarget = new DirectoryInfo(target);
try
{
CopyAll(dirSource, dirTarget);
if (logList.Count > 0)
{
using (StreamWriter sw = new StreamWriter(target + #"\log.txt", false))
{
foreach (string error in logList)
{
sw.WriteLine(error);
}
}
}
DialogResult result = MessageBox.Show("Copy Succeeded", "Success");
if (result == DialogResult.OK)
{
string myPath = dirTarget.ToString();
System.Diagnostics.Process prc = new System.Diagnostics.Process();
prc.StartInfo.FileName = myPath;
prc.Start();
}
}
catch (Exception)
{
MessageBox.Show("Copy Failed", "Failed");
}
}
}
}
As #Reza Aghaei pointed out in comments, the problem is that you do not clear the logList.
The file gets created anew every time, but each time you click the Copy button, the loglist still contains the results of the previous copy action.
So you need to clear the list when starting a new copy:
private static void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
logList.Clear();
// ...
From your code it seems that you never clear the logList, this means that it appears the file is being appending because the logList still contains all of the old entries.
You'll need to clear the list between copies if you only want relevant entries to that copy, either before you start copying or after you finish writing the file.
This would be better as a separate method
try
{
CopyAll(dirSource, dirTarget);
SaveLog(target + #"\log.txt");
ClearLog();
//...
}
private void SaveLog(string filename)
{
if (logList.Count > 0)
{
FileStream fs = File.Open(target + #"\log.txt", FileMode.Create);
using (StreamWriter sw = new StreamWriter(fs))
{
foreach (string error in logList)
{
sw.WriteLine(error);
}
}
}
}

I'm trying to change the color of each of a set of buttons using C#/Visual studio but nothing happens

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Cameron_PickerillTrinitapoli_Assn1
{
public partial class seasonalBttnChanger : Form
{
public seasonalBttnChanger()
{
InitializeComponent();
}
//Triggers when program first loads
//Sets up
private void bttnChanger_Load(object sender, EventArgs e)
{
List<Button> lstTxt = new List<Button>
{
bttnSpring, bttnSummer, bttnAutumn, bttnWinter
};
//Wiring up event handlers in run time
foreach (Button txt in lstTxt)
{
txt.MouseEnter += button_MouseEnter;
txt.MouseLeave += button_MouseLeave;
}
}
// Sets up different background colors for TextBoxes
//* Static values for the color of each button need
//to be added.
//**Not what I was trying to accomplish
//**This needs to go somewhere else
void button_MouseEnter(object sender, EventArgs e)
{
try
{
// Event handlers always pass the parameter "sender" in as an object.
// You have to downcast it back to the actual type.
String bttnName = null;
String bttnNameSpring = "Spring";
String bttnNameSummer = "Summer";
String bttnNameAutumn = "Autumn";
String bttnNameWinter = "Winter";
Button txt = (Button)sender;
// stkColor.Push(txt.BackColor);
//txt.BackColor = Color.Red;
bttnName = txt.Name;
if (bttnName == bttnNameSpring)
{
txt.BackColor = Color.LightGreen;
}
else if (bttnName == bttnNameSummer)
{
//txt.BackColor = Color.Red;
txt.BackColor = Color.Red;
}
else if (bttnName == bttnNameAutumn)
{
txt.BackColor = Color.Yellow;
}
else if (bttnName == bttnNameWinter)
{
txt.BackColor = Color.Cyan;
}
}
catch (Exception ex)
{
MessageBox.Show("Error:\n " + ex.Message);
}
}
//Handler for mouse leaving the button
void button_MouseLeave(object sender, EventArgs e)
{
try
{
Button txt = (Button)sender;
//txt.BackColor = stkColor.Pop();
}
catch (Exception ex)
{
MessageBox.Show("Error:\n " + ex.Message);
}
}
}
}
}
Alright, so I'm just trying to create a simple program that changes the background colors of these buttons to some plain colors when you mouse over them. I had the same thing working using TextBox, but I need to get it working with button. I changed everything over to use the Button class, and it seems to have all the same functionality, but now the program runs, but nothing happens when you mouse over the buttons.
I'm pretty new to c#, stack exchange, and only slightly less so to programming in general, so sorry if I'm dinking up on etiquette/formatting my questions, etc.
I think you are trying to reference your buttons title when you are actually calling the button name
Changing:
String bttnName = null;
String bttnNameSpring = "Spring";
String bttnNameSummer = "Summer";
String bttnNameAutumn = "Autumn";
String bttnNameWinter = "Winter";
Button txt = (Button)sender;
To:
String bttnName = null;
String bttnNameSpring = "bttnSpring";
String bttnNameSummer = "bttnSummer";
String bttnNameAutumn = "bttnAutumn";
String bttnNameWinter = "bttnWinter";
Causes the code to work.
If you do infact want to use the "Spring", "Summer" etc reference than i would suggest changing
bttnName = txt.Name;
to
bttnName = txt.Text;
And ensure that the Text of the labels is set correctly

Categories