Using Button to show context menu [duplicate] - c#

I have used toolstripdropdown in my Windows form to show list of buttons on click of another button.
var td = new ToolStripDropDown
{
AutoSize = true,
DropShadowEnabled = false,
BackColor = Color.Transparent,
Margin = Padding.Empty,
Padding = Padding.Empty
};
var host = new ToolStripControlHost(panel)
{
BackColor = Color.Transparent,
Margin = Padding.Empty,
Padding = Padding.Empty
};
td.Items.Add(host);
The panel contains list of buttons to be displayed. To show the panel to user, on button(Show) click following line is called.
td.Show(pointonScreen);
By default, AutoClose is set to true. So whenever user clicks anywhere in the form, the toolstripdropdown is getting closed. This is ok.
My requirements:
Click Show button
Display the toolstripdropdown by calling td.show() and close the popup if td.Visible
Again click the Show button
toolstripdrown should be closed
Click anywhere in the form, toolstripdropdown should be closed if it is visible
What is happening now is, on step 3, before the button click event is raised, toolstripdropdown is getting closed. So again the dropdown is being displayed.
Is there any other way to achieve my requirements?

You should handle Closing event of the dropdown and set a flag if the dropdown is closing by click on the button which opened it. Then when you click on button, check the flag and if there wasn't a flag, show dropdown and set the flag, otherwise close the dropdown and clear the flag:
ToolStripDropDown td;
private void Form1_Load(object sender, EventArgs e)
{
td = new ToolStripDropDown { /*...*/};
var host = new ToolStripControlHost(this.panel1){ /*...*/};
td.Items.Add(host);
td.Closing += td_Closing;
}
void td_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
if (e.CloseReason == ToolStripDropDownCloseReason.AppClicked)
if (this.button1.Bounds.Contains(this.PointToClient(MousePosition)))
{
td.Tag = true;
return;
}
td.Tag = null;
}
private void button1_Click(object sender, EventArgs e)
{
if (td.Tag == null)
{
td.Show(Cursor.Position);
td.Tag = true;
}
else
{
td.Close();
td.Tag = null;
}
}

Related

Enable, Disable Panel in parent from child form

I am developing C# windows application. My DASHBOARD look like PICTURE-1.enter image description here
But, when I clicked sub menu sub menu shows like PICTURE-2. enter image description here
Sub menu must show like PICTURE-3. enter image description here
I have disabled panel at the time of loading of sub menu. I have tried to load DISABLED PANEL FROM SUBMENU, when submenu closed button is clicked. But the problem is other summery panel is not loaded in CONTAINER PANEL. It looks like PICTURE-4.enter image description here
I have done following things:
1. Set all panels modifiers to PUBLIC.
2. I have created a object in submenu form.
3. After that I tried to call all container and set the property of that panel as enable.
4. I have tried SHOW(), SHOWDIALOGUE(). But another form is loaded in taskbar.
My code is for calling SUBMENU from MAIN MENU (DASHBOARD) is
private void btnAdminDataBackup_Click(object sender, EventArgs e)
{
panelMainMenuRight.Visible = false;
panelTopSummery.Visible = false;
panelRightSummery.Visible = false;
submenuAdmin.Visible = false;
MenuDisplay(new Forms.frmDataBackup());
}
private void MenuDisplay(object MenuName)
{
Form fh = MenuName as Form;
fh.TopLevel = false;
fh.Dock = DockStyle.Fill;
this.panelContainer.Controls.Add(fh);
this.panelContainer.Tag = fh;
fh.Show();
}
Code is from SUBMENU:
private void btnClose_Click(object sender, EventArgs e)
{
Main_Menu mnu = new Main_Menu();
mnu.panelContainer.Visible = true;
mnu.panelMainMenuRight.Visible = true;
mnu.panelRightSummery.Visible = true;
mnu.panelTopSummery.Visible = true;
this.Close();
}
You are requested to help me to solve this issue.
Thank you.

C# WinForms contextmenu focus issue when textbox is added

In a C# WinForms application I need to create a ContextMenuStrip with dropdown and textbox:
private System.Windows.Forms.ContextMenuStrip ct1;
private void button_Click(object sender, EventArgs e)
{
var header = new ToolStripMenuItem("Header");
header.Enabled = false;
var options = new ToolStripMenuItem("Options");
for (int i = 0; i < 5; i++)
{
var checkoption = new ToolStripMenuItem("Check Me " + i + "!");
checkoption.CheckOnClick = true;
options.DropDownItems.Add(checkoption);
}
var txt = new ToolStripTextBox();
txt.Text = "changeme";
options.DropDownItems.Add(txt);
options.DropDown.Closing += DropDown_Closing;
ct1.Items.Clear();
ct1.Items.Add(header);
ct1.Items.Add(options);
ct1.Show(this, button.Left, button.Top);
}
private void DropDown_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
e.Cancel = (e.CloseReason == ToolStripDropDownCloseReason.ItemClicked);
}
Now, e.Cancel will prevent closing the dropdown if the reason is ItemClicked, so I can select more items without having to open the menu again:
Please note that "changeme" is a ToolStripTextBox!
Once I focus it (click on it), I can edit the text inside:
After finish editing the textbox, I still can change the checkbox items, but there is no focus indicator:
How can I get back the focus indicator just as shown on the first picure?
Note: if I move the mouse onto "Header", the dropdown will close, and then moving it back to "Options", will reopen the dropdown and then the focus indicator is good again:
How can I do this without closing and reopening the dropdown?
I have tried Select() for the options item, but it did not help, neither Invalidate() on ct1.
Just have found it:
First needs to add a click handler on the dropdown:
options.DropDown.Click += DropDown_Click;
Then in the click handler it needs to be focused:
private void DropDown_Click(object sender, EventArgs e)
{
var dropdown = (ToolStripDropDown)sender;
dropdown.Focus();
}

Xamarin dismiss popup window

I have an issue , when SavedAyasListView_ItemLongClick event fires, a popupwindow appears with some buttons, and my intention is whenever i click btnDeleteAya button , perform some actions and dismiss the popup.
When the event fires, the popup appears and when i click the btnDeleteAya the action gets performed and the popup disappears, but when i do the same thing again the popup window doesnt get closed.
I want it to get closed each time i click the btnDeleteAya button not by just clicking outside because that part works as expected.
Here's the code:
public string itemclicked;
PopupWindow mpopup;
private void SavedAyasListView_ItemLongClick(object sender, AdapterView.ItemLongClickEventArgs e)
{
View popUpView = LayoutInflater.Inflate(Resource.Layout.SavedAyasPopupMenu,
null); // inflating popup layout
mpopup = new PopupWindow(popUpView, 800, 500); // Creation of popup
mpopup.SetBackgroundDrawable(new BitmapDrawable());
mpopup.OutsideTouchable = true;
mpopup.Focusable = true;
mpopup.ShowAtLocation(popUpView, GravityFlags.Center, 10, 50);
itemclicked = savedAyasListView.GetItemAtPosition(e.Position).ToString();
Button btnDeleteAya = popUpView.FindViewById<Button>(Resource.Id.btnDeleteAya);
btnDeleteAya.Click += BtnDeleteAya_Click;
btnDeleteAya.Click += delegate
{
mpopup.Dismiss();
};
Button fbShareSavedAya = popUpView.FindViewById<Button>(Resource.Id.fbShareSavedAya);
fbShareSavedAya.Click += FbShareSavedAya_Click;
}

Datagridview shall disappear when clicking on the Form in the background

As described in the title, I have a Form with a Datagridview on the front. The datagridview is smaller than my form in the back and I want the Datagridview to disappear whenever I click anywhere else but the Datagridview.
My code looks like this:
this.dataGridView1.Leave += new System.EventHandler(this.focus);
and the Eventhandler is defined like this:
private void focus(object sender, EventArgs e)
{
if(dataGridView1.Focused == false)
{
dataGridView1.Visible = false;
}
}
My problem is that my Datagridview only disappears when a new event in my form is activated but not when I click for example in a textbox on my form.
Can anyone help me?
The Leave event will not raise if you click on Form, or a ToolStripButton, PictureBox or any other non-selectable control.
If you expect a behavior like a dropdown, you can host DataGridView in a ToolStripControlHost and show it using a ToolStripDropDown. This way when you click anywhere outside the `DataGridView, it will disappear. It will act like a dropdown menu. Also the grid can be larger than your form:
private void button1_Click(object sender, EventArgs e)
{
this.dataGridView1.Margin = new Padding(0);
var host = new ToolStripControlHost(this.dataGridView1);
this.dataGridView1.MinimumSize = new Size(200, 100);
host.Padding = new Padding(0);
var dropdown = new ToolStripDropDown();
dropdown.Padding = new Padding(0);
dropdown.Items.Add(host);
dropdown.Show(button1, 0,button1.Height);
}
Important Note: It's an example. It's better to pay attention to disposing of objects in a real world application. For example, use just a single ToolStripdropDown and dispose it when closing the form.
Change the event handler assigning to:
this.dataGridView1.Leave += new System.EventHandler(fokussiert);
Worked for me when focusing on a textbox
you want your dgv also to disapear when you click on your textbox? is that what you mean?
private void dataGridView1_Leave(object sender, EventArgs e)
{
dataGridView1.Visible = false;
}
private void Form1_Click(object sender, EventArgs e)
{
dataGridView1.Visible = false;
}

Expand/Collapse Win Form in C#

I want to create UI like this in Windows form application C#.
First my form should appear like this when the chechBox is not checked.
And if the checkBox checked my form changes to like this
How can I do this?
Change the height of the form dynamically on CheckedChanged event of check box. Don't forget to set anchor of below fields or set visible on expand and invisible on collapse.
EDIT: The most simple way to achieve the results is given below.
private readonly int _collapsedHeight;
public Form1()
{
//Set Anchor of Connect button to Right and Bottom and leave default for others
//Optionally you need to hide controls except Connect button on collapse and vice versa.
//Set Form Border Style to FixedSingle and MaximizeBox to false
InitializeComponent();
_collapsedHeight = Height;
}
private void chkAdvancedOption_CheckedChanged(object sender, EventArgs e)
{
//Set Y value to collapse eg. 140, adjust it as required...
Height = chkAdvancedOption.Checked ? _collapsedHeight + 140 : _collapsedHeight;
}
void Page_Load(Object sender, EventArgs e)
{
// Manually register the event-handling method for the
// CheckedChanged event of the CheckBox control.
checkbox1.CheckedChanged += new EventHandler(this.Check_Clicked);
}
void Check_Clicked(Object sender, EventArgs e)
{
**//This is only sample code**
// do your code
if (panel2.Visible)
{
panel2.Visible = false;
cmdAdvanced.Visible = true;
}
}

Categories