How To Make 'Reply' & 'Reply-All' Button Grey In Outlook C# - c#

In outlook…
If you select multiple email from top to bottom, Reply/Reply-all buttons are enabled
If you select multiple email from bottom to top, Reply/Reply-all buttons are disabled
CASE-1 : Selecting 'Test 2' & then select 'Test 1' => Reply/Reply-all buttons are enabled
CASE-2 : Selecting 'Test 1' & then select 'Test 2' => Reply/Reply-all buttons are disabled
I have Explorer.SelectionChange event which checks if there are multiple emails are selected. If there are multiple emails are selected, Then I have disabled 'Reply/Reply-All' events by MailItem.Actions["Reply"].Enabled = false;.
Explorer objExplorer = (Explorer)Api.Instance.GetActiveExplorer();
if (objExplorer != null)
{
objExplorer.SelectionChange += () =>
{
Selection objSelectionList = objExplorer.Selection;
if (objSelectionList != null && objSelectionList.Count > 1 && objSelectionList[1] != null)
{
MailItem objMailItem = objSelectionList[1] as MailItem;
if (objMailItem != null)
{
objMailItem.Actions["Reply"].Enabled = false;
objMailItem.Actions["Reply to All"].Enabled = false;
}
}
};
}
But it's not giving me output like CASE-2. So basically I want to make both buttons disabled in CASE-1 as shown in CASE-2.
It's only giving me a popup saying 'This action not available for this item'.
I have to do this to maintain consistency in both case. So need solution from one of below..
If there is any way to enable both buttons in CASE-2 (Like CASE-1)
If there is any way to disable both buttons in CASE-1 (Like CASE-2)
Thanks in advance.

In the code you disable the Reply and Reply to All actions on the first selected item which is a different item in both scenarios:
MailItem objMailItem = objSelectionList[1] as MailItem;
if (objMailItem != null)
{
objMailItem.Actions["Reply"].Enabled = false;
objMailItem.Actions["Reply to All"].Enabled = false;
}
But the displayed item is always the same, but it is not necessary the first in the collection. So, instead I'd suggest iterating over all selected items and disabling actions for all of them to make sure the view is updated correctly. A raw sketch:
for(int i=1; i <= objSelectionList.Count; i++)
{
MailItem objMailItem = objSelectionList[i] as MailItem;
if (objMailItem != null)
{
objMailItem.Actions["Reply"].Enabled = false;
objMailItem.Actions["Reply to All"].Enabled = false;
}
}

Related

MenuItem AutoClose = false on dynamically generated Toolstrip Menu

So, following this question, I have been attempting to deal with a way to stop a drop down menu from closing when I click on an item.
In the linked question, one such answer suggested that I set the AutoClose property to false. I did so, and this did achieve what I asked. However, the way I implemented it means that the Drop Down menu is forced open.
Form Code:
public void ToolStripMenuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem item = sender as ToolStripMenuItem;
if (item != null)
item.Checked = !item.Checked;
item.DropDown.AutoClose = false;
}
I know why this is - the implementation means that there is no way to allow the AutoClose to be set to true. However, since the menuItems are dynamically generated in a different class, I don't have any events or objects to refer to.
This code copies the menu structure from the Main Form, and copies it across to recreate it in the "Profile View" (to set what users can/cannot see).
Controller Code:
private void PopulateProfileView(User_AccessProfilesView view, Menu_View mainMenu)
{
// Disabled Items are not able to be set, becasue they are either always visible for every user,
// or only visible to specific users (Administrator)
List<string> disabledMenuItems = new List<string>();
List<string> disabledSubMenuItems = new List<string>();
bool error = false;
bool subError = false;
_groupDictionary = new Dictionary<string, List<string>>();
// Populate the disallowed Menu Items from the Main Menu,
// and then add the items specific to the Profile View
disabledMenuItems.Add("File");
disabledMenuItems.Add("Administrator");
disabledMenuItems.Add("Help");
disabledMenuItems.Add("Te&rminations");
disabledMenuItems.AddRange(mainMenu.disallowedMenuItems);
// Populate the disallowed Sub Menu Items from the Main Menu,
// and then add the items specific to the Profile View
disabledSubMenuItems.Add("View All");
disabledSubMenuItems.AddRange(mainMenu.disallowedSubItems);
foreach (ToolStripMenuItem item in mainMenu.mainMenuStrip.Items)
{
ToolStripMenuItem menuItem = new ToolStripMenuItem(item.Text);
if (error == false)
{
// Add to the menu bar
view.menuStrip.Items.Add(menuItem);
menuItem.Click += new EventHandler(view.ToolStripMenuItem_Click);
foreach (ToolStripItem dropItem in item.DropDownItems)
{
if (dropItem is ToolStripMenuItem)
{
ToolStripMenuItem menuDropItem = new ToolStripMenuItem(dropItem.Text);
// Same concerns as above with regards to doing a substring check
// to decide if menu items should be excluded or not.
foreach (string s1 in disabledSubMenuItems)
{
if (!menuDropItem.Text.Contains(s1))
{
subError = false;
}
else
{
subError = true;
break;
}
}
if (!subError)
{
menuItem.DropDownItems.Add(menuDropItem);
menuDropItem.Click += new EventHandler(view.ToolStripMenuItem_Click);
}
}
else if (dropItem is ToolStripSeparator)
{ menuItem.DropDownItems.Add(new ToolStripSeparator()); }
}
How do I implement the AutoClose property correctly so that if I click on a menu item, the menu won't close, but if I click on the menu header, or move the mouse away from the menu, or select another menu (either by click or mouse over), the menu does Close?
Apologies if this is a simple issue - I have been out of the game for roughly a year, and have to jump back into this and I am having a little bit of an issue following everything properly.
To solve the problem you can follow these steps:
You should determine which menu items should keep open even after clicking on them. I'll use "keepopen" as value of Tag property for those items that should be kept open after clicking.
For the menu item which contains those items, you need to get DropDown property and and handle its ItemClicked event and in the ItemClicked event, you should check if the item which is clicked is one of those "keepopen" items, then set DropDown.AutoClose of the container menu item to false. For other items, set it to true. It will prevent closing those "keepopen" item when clicking, while let other items close by click.
You should handle CheckedChanged event of those "keepopen" items and set DropDown.AutoClose to true. While using the Click event handler we prevented the items from closing, here we enable the closing again, so if the user click outside of the menu, it will close.
Then this would be the result, look at mouse clicks:
Example
As an example, create an empty form and handle its Load event and use following code. When you click on SubMenu1, SubMenu2 or SubMenu3, they will just get checked or unchecked without closing the menu. But of you click outside the menu or on SubMenu4, it will close the menu.
const string keepopen = "keepopen";
private void Form1_Load(object sender, EventArgs e)
{
var menuStrip = new MenuStrip() { Dock = DockStyle.Top };
this.Controls.Add(menuStrip);
var menu1 = (ToolStripMenuItem)menuStrip.Items.Add("Menu1");
menu1.DropDownItems.Add(new ToolStripMenuItem("Submenu1")
{ Tag = keepopen, CheckOnClick = true });
menu1.DropDownItems.Add(new ToolStripMenuItem("Submenu2")
{ Tag = keepopen, CheckOnClick = true });
menu1.DropDownItems.Add(new ToolStripMenuItem("Submenu3")
{ Tag = keepopen, CheckOnClick = true });
menu1.DropDownItems.Add("-");
menu1.DropDownItems.Add(new ToolStripMenuItem("Submenu4"));
menu1.DropDown.ItemClicked += (obj, args) =>
{
if (args.ClickedItem.Tag == keepopen)
menu1.DropDown.AutoClose = false;
else
menu1.DropDown.AutoClose = true;
};
menu1.DropDownItems.OfType<ToolStripMenuItem>()
.Where(x => x.Tag == keepopen)
.ToList().ForEach(x =>
{
x.CheckedChanged += (obj, args) =>
{
menu1.DropDown.AutoClose = true;
};
});
}

Selected TextBox deselecting when I change ListBox

I am making myself a project manager and I need to refresh Project List every so often. When I am refreshing the Project Manager, I want to select the item that was previously selected. But that selection causes my text box to unselect, therefore, what happens is that that text box unselects after typing one key.
So outline what happens:
I try to edit one text box
Edition causes update in project -> program calls RefreshProjectList()
RefreshProjectList() on marked position causes selected text box to unselect
Result: You must select text box after writing one symbol
Picture if useful
These selected text boxes are struggling to be edited
Code:
private void RefreshProjectList() {
if (BlockListReload)
return;
Project selected = (Project)ProjectList.SelectedItem;
ProjectList.Items.Clear();
CurrentlyShown.Clear();
foreach(Project p in Projects){
if (p.state == State.Planned && ShowPlanned.Checked) {
CurrentlyShown.Add(p);
ProjectList.Items.Add(p);
}
if (p.state == State.Active && ShowActive.Checked) {
CurrentlyShown.Add(p);
ProjectList.Items.Add(p);
}
if (p.state == State.Finished && ShowFinished.Checked) {
CurrentlyShown.Add(p);
ProjectList.Items.Add(p);
}
if (p.state == State.Delayed && ShowDelayed.Checked) {
CurrentlyShown.Add(p);
ProjectList.Items.Add(p);
}
}
if (selected == null)
return;
if (ProjectList.Items.Contains(selected)) {
ProjectList.SelectedItem = selected; // IF I REMOVE THIS
} else {
if (ProjectList.Items.Count > 0)
ProjectList.SelectedIndex = 0; // OR THIS LINE, EVERYTHING WORKS
}
}
If you need more code, I will be happy to provide, but I don't want to spam you with loads of unuseful code.
Q: Why does changing selected item in ListBox cause deselecting of TextBox and how to prevent it?
Several controls that have selectable text or items also come with a property HideSelection.
This includes:
TextBox
RichTextBox
ListView
but not
ListBox
CheckedListBox
DataGridView
Like it or not it always defaults to true so the selection is hidden whenever the focus is off the control..
Simply set it to false in the designer and you can see all selection no matter where the focus is..
OMG. I honestly don't know why I did not see it.
ProjectList.SelectedItem = selected;
//where ProjectList is ListBox<Project> and selected is Project
I am selecting an item in the ProjectList(ListBox). I didn't realize that it was calling a ProjectList_SelectedIndexChanged() event which was doing it.
EDIT: SOLVED by adding this:
if (focused != null) {
this.ActiveControl = focused;
focused.Select(focused.TextLength,0);
}
Where focused is a TextBox I set to last selected TextBox and this is the form.
Thanks TaW.

Clicking on List Item on Dropdown

I'm having a problem in interacting with a custom dropdown control. It works fine the 1st 6 times, but after that, since the screen is resized, it could no longer locate and click the option in the dropdown control, returning an exception - can't click on hidden control. I tried putting in a itemField.DrawHighlight(); on the control I'm looking for, and it finds it, however it can't click on it. I also tried a to scroll down, but it seems to be not working.
bool addItemCheck = false;
int scrollCheck = 0;
while (Check == false)
{
var addItem= new HtmlButton(window);
addItem.SearchProperties.Add(HtmlButton.PropertyNames.Id, "add-new-item");
Mouse.Click(addItem);
scrollCheck = scrollCheck + 1;
if (scrollCheck > 6)
{
Mouse.MoveScrollWheel(window, -100);
}
var itemDropDown = new HtmlSpan(window);
itemDropDown .SearchProperties.Add(HtmlSpan.PropertyNames.Class, "item-dropdown");
itemDropDown .SearchProperties.Add(HtmlSpan.PropertyNames.InnerText, "Select an Item");
Mouse.Click(itemDropDown );
addItemCheck = itemDropDown.Exists;
}
bool itemBoxCheck = false;
HtmlCustom itemBox = null;
while (itemBoxCheck == false)
{
itemBox = new HtmlCustom(window);
itemBox.SearchProperties.Add(HtmlCustom.PropertyNames.Id, "item-listbox");
var itemField = new HtmlCustom(itemBox);
itemField .SearchProperties.Add(HtmlCustom.PropertyNames.InnerText, item);
Mouse.Click(itemField);
itemBoxCheck = itemBox.Exists;
}
I would really appreciate any help. Thank you.
Try calling the method InsureClickable() on the control before attempting to click on it.
for example:
itemDropDown.EnsureClickable();
Mouse.Click(itemDropDown);
Edit:
if this doesn't work you'll have to scroll down to the item.
try using:
Mouse.MoveScrollWheel()
if that doesn't work also you'll have to map the scroll control and click on it.

ListView selection logic

I have a ListView with MultiSelect = false, View = Details and CheckBoxes = True. I'm stepping through it and controlling visibility in an application. I'm currently just using the Else portion of the code below. But it doesn't account for the first Item being selected, it just turns the second item on. And whether or not the item is checked (already visible), and it turns the visibility off. I'm comparing elements associated with the items against elements that are already visible. My app crashes at the currentItem.Checked loop. And doesn't account for combinations (first and checked). How could I code this?
int indexCount = listView1.Items.Count;
ListViewItem currentItem = listView1.SelectedItems[0];
int currentIndex = currentItem.Index;
if (currentItem.Index == 0)
{
//listView1.SelectedItems[0] on
}
if (currentItem.Index == indexCount)
{
//end
}
if (currentItem.Checked == true)
{
while (currentItem.Checked == true)
{
listView1.SelectedIndices.Clear();
listView1.SelectedIndices.Add(currentIndex + 1);
}
//listView1.SelectedItems[0] on
}
else
{
//listView1.SelectedItems[0] off
listView1.SelectedIndices.Clear();
listView1.SelectedIndices.Add(currentIndex + 1);
//listView1.SelectedItems[0] on
}
It is not clear what you are trying to do.
You are basically picking one selected item and placing it in 'currentItem'. If that item is checked as true you are looping through it until? and why?

set selected item of listview according to a value

C# winforms.
I have a listview, and for each item clicked I show its properties in labels and textboxes.
They are not binded in any way, I do manually.
So I change values in textboxes (via next and previous record buttons) and the listview.selected doesn't change.
I've done this my way, but I think maybe there's some optimization or even a single method to do this. I'm seeking something like selected=itemwithkey(idTextBox)
for (int i = 0; i < lstvClientes.Items.Count; i++) {
if (lstvClientes.Items[i].SubItems[0].Text == idTextBox.Text) {
lstvClientes.Items[i].Selected = true; break;
}
}
Suggestions?
thank you community.
You can use ListView.FindItemWithText method:
var item = lstvClientes.FindItemWithText(idTextBox.Text);
if (item != null)
item.Selected = true;

Categories

Resources