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();
}
Related
I've a user control with 2 labels and two textboxes. When the label is clicked the textbox's visible prop is set to true. Here's the code I've used:
private void label_Heading_Click(object sender, EventArgs e)
{
label_Heading.Visible = false;
textBox_Heading.Text = label_Heading.Text;
textBox_Heading.Visible = true;
textBox_Heading.Focus();
}
After the textbox lose focus, it's visible prop is set to false and the label is updated with the text. Code:
private void textBox_Heading_Leave(object sender, EventArgs e)
{
textBox_Heading.Visible = false;
if(textBox_Heading.Text != "")
label_Heading.Text = textBox_Heading.Text;
label_Heading.Visible = true;
}
The code to create user controls on click:
private void label1_Click(object sender, EventArgs e)
{
TaskCard _taskCard = new TaskCard(++TOTAL_ITEM_COUNT, PanelName);
panel_DeletedItem.Controls.Add(_taskCard);
panel_DeletedItem.Refresh();
}
These code work fine when a single user control of this type is added to a panel. But if I add more than one, the code works only for the first user control, but it wont work for the new ones, although the event is fired for every user control. What am I missing here? Please suggest.
If I add a mbox to this code, the mbox is displayed for any control, but the rest of the code won't work, except for the first one.
private void label_Heading_Click(object sender, EventArgs e)
{
MessageBox.Show("Test"); // this will display, but the rest of the code is not executed or changes are not visible, i.e., the teboxes are not displayed even if I click the labels
label_Heading.Visible = false;
textBox_Heading.Text = label_Heading.Text;
textBox_Heading.Visible = true;
textBox_Heading.Focus();
}
I have a treenode which displays a checklist from a SQL database. I have a method to get the selected workflows.
I want to enable the run button if a checkbox is checked and disable the button if nothing is checked and on load.
I'm not sure where to put this if statement. I have tried putting it under the run button on the click action but it is not working correctly.
Any help is appreciated.
List<WorkflowViewModel> workflowViewList = new List<WorkflowViewModel();
var workflowList = GetSelectedWrokflows();
if (workflowList.Count == 0)
{
button.enabled = false;
}
else
{
button.enabled = true;
}
One way to do this is to create a method that will do the work of determining the selected workflow items and enabling or disabling the button. By putting the code in a single method, it allows you to call it from multiple places, and if you need to change the behavior, you only have one place to make the modifications.
Then you can just call this method from the Form_Load event, and from the checked list box's ItemCheck event:
public partial class Form1 : Form
{
List<WorkflowViewModel> workflowViewList = new List<WorkflowViewModel>();
private void SetRunButtonState()
{
workflowViewList = GetSelectedWorkflows();
button.Enabled = workflowViewList.Count > 0;
}
private void Form1_Load(object sender, EventArgs e)
{
SetRunButtonState();
}
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
SetRunButtonState();
}
// Rest of class code omitted...
}
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;
}
}
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;
}
With C# I am trying to only show a ContextMenuStrip (CMS) when I right click a specific column in my DataGridView. I am confused as to whether I should be using a DataGridView_CellContentClick and/or dataGridView1.HitTest(). Then to finish off my problem I want to send the data from that right clicked cell to a new form window.
My current code has strange behavior. It will not show the CMS unless I have first left click or right clicked the 4th column. However once I have it will always show the CMS on a right click.
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 4)
{
//Create the ContextStripMenu for Creating the PO Sub Form
ContextMenuStrip Menu = new ContextMenuStrip();
ToolStripMenuItem MenuOpenPO = new ToolStripMenuItem("Open PO");
//Assign event handlers
MenuOpenPO.MouseUp += new MouseEventHandler(MenuOpenPO_Click);
Menu.Items.AddRange(new ToolStripItem[] { MenuOpenPO });
//Assign created context menu strip to the Datagrid
dataGridView1.ContextMenuStrip = Menu;
}
}
void MenuOpenPO_Click(object sender, MouseEventArgs e)
{
var ht = dataGridView1.HitTest(e.X, e.Y);
MessageBox.Show("Hello2");
PO_Form POWindow = new PO_Form();
POWindow.Show();
}
I was going to use the var ht = dataGridView1.HitTest(e.X, e.Y); to grab the cell value.
Any help would be appreciated, thanks!
Edit 1
So I updated dataGridView1_CellContentClick to this and it gets me very close to the behaviour I am looking for. If I first left click in column 4 then right click I get my CMS. If I left click any other cell in another column then right click the CMS is no longer there. However I want to be able to just right click a cell in column 4 without having to left click first to create the CMS.
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 4)
{
//MessageBox.Show("Hello1");
//Create the ContextStripMenu for Creating the PO Sub Form
ContextMenuStrip Menu = new ContextMenuStrip();
ToolStripMenuItem MenuOpenPO = new ToolStripMenuItem("Open PO");
//Assign event handlers
MenuOpenPO.MouseUp += new MouseEventHandler(MenuOpenPO_Click);
Menu.Items.AddRange(new ToolStripItem[] { MenuOpenPO });
//Assign created context menu strip to the Datagrid
dataGridView1.ContextMenuStrip = Menu;
}
else
dataGridView1.ContextMenuStrip = null;
}
I did this in VB ..
Private Sub DGV_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DGV.CellMouseClick
If e.Button = Windows.Forms.MouseButtons.Right Then
DGV.CurrentCell = DGV.Rows(e.RowIndex).Cells(e.ColumnIndex)
CMS.Show(DGV, DGV.PointToClient(Windows.Forms.Cursor.Position))
End If
End Sub
I figured out my own problem and so will post the solution. Unfortunately I am very new at C# programming and really extremely rusty at it in general. I struggled with the concept of Event handlers (not sure if correct name?), how they were being called and how they all vary depending on if you are using MouseEventArgs, EventArgs, KeyEventArgs, etc. Anyway I digress.
My solution is below. I found the using dataGridView1.MouseUp gave me poor user interaction results requireing 2 of each action for a state change. IE create my ContextMenuStrip when the correct coloumn is right Clicked. Or make it disappear if any other column is right clicked. So use dataGridView1.MouseDown for better results.
dataGridView1.MouseDown += new MouseEventHandler(this.dataGridView_MouseDown);
private void dataGridView_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var ht = dataGridView1.HitTest(e.X, e.Y);
if (ht.ColumnIndex == 4)
{
//Create the ContextStripMenu for Creating the PO Sub Form
ContextMenuStrip Menu = new ContextMenuStrip();
ToolStripMenuItem MenuOpenPO = new ToolStripMenuItem("Open PO");
MenuOpenPO.MouseDown += new MouseEventHandler(MenuOpenPO_Click);
Menu.Items.AddRange(new ToolStripItem[] { MenuOpenPO });
//Assign created context menu strip to the DataGridView
dataGridView1.ContextMenuStrip = Menu;
}
else
dataGridView1.ContextMenuStrip = null;
}
}
If you want to create a new form or do something else I used the following code when clicking your ContextMenuStrip I used the following after creating this event handler? MenuOpenPO.MouseDown += new MouseEventHandler(MenuOpenPO_Click);
void MenuOpenPO_Click(object sender, MouseEventArgs e)
{
PO_Form POWindow = new PO_Form();
POWindow.Show();
}