To Enable right click in disabled control - c#

I Have a textbox which is disabled and has value .And i want to enable right click option to copy the disabled value from textbox (Windows application).Pls help me to do this.

Try this, keeping in mind that you have to have your contextmenustrip added:
private void YourFormName_Load(object sender, EventArgs e)
{
ContextMenu mnu = new ContextMenu();
MenuItem mnuCopy = new MenuItem("Copy");
mnuCopy.Click += (sen, ev) =>
{
System.Windows.Forms.Clipboard.SetText(YourTextBoxName.Text);
};
mnu.MenuItems.AddRange(new MenuItem[] { mnuCopy });
YourTextBoxName.ContextMenu = mnu;
}
private void YourFormName_MouseUp(object sender, MouseEventArgs e)
{
Control ctl = this.GetChildAtPoint(e.Location);
if (ctl != null && !ctl.Enabled && ctl.ContextMenu != null)
ctl.ContextMenu.Show(this, e.Location);
}

When you click on a disabled element in a page, the event is handled by the parent element of the disabled element.
for example if your textbox is in a page then the page handles it. if the text box is in a different container like div, then that container will handle the mouse click event.
for your situation, you could write a handler on the parent element.
a javascript function that will catch the event and it can read the value for you. for instance, the JS function can change the disabled property to false, read the value, and then disable the textbox again.

Coming back to Vijaya's answer, I handled the problem by just placing the control with Dock=Fill into a panel control with zero padding and margin. So you would do your things in the MouseUp event of the panel instead.

Related

Show property editor by click on property text box in PropertyGrid

I am creating a touch application on a no-keyboard pc, where I use a PropertyGrid to manage classes to store / save the app configuration.
I need to edit the propertyline's rows with a custom keyboard that I created (not the system's) setting the class as UITypeEditor
Now the custom keyboard is showed when right button is clicked.
Is it possible to show when on the row start edit (like textbox Enter event),
or when the row is selected ?
The editor control which you see in PropertyGrid is a GridViewEdit control which is a child of PropertyGridView which is a child of the PropertyGrid.
You can find the edit control and assign an event handler to its Enter event. In this event, you can find the SelectedGridItem and then call its EditPropertyValue method which is responsible to show the UITypeEditor.
private void propertyGrid1_SelectedObjectsChanged(object sender, EventArgs e)
{
var grid = propertyGrid1.Controls.Cast<Control>()
.Where(x => x.GetType().Name == "PropertyGridView").FirstOrDefault();
var edit = grid.Controls.Cast<Control>()
.Where(x => x.GetType().Name == "GridViewEdit").FirstOrDefault();
edit.Enter -= edit_Enter;
edit.Enter += edit_Enter;
}
private void edit_Enter(object sender, EventArgs e)
{
var item = this.propertyGrid1.SelectedGridItem;
if (item.GetType().Name == "PropertyDescriptorGridEntry")
{
var method = item.GetType().GetMethod("EditPropertyValue",
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance);
var grid = propertyGrid1.Controls.Cast<Control>()
.Where(x => x.GetType().Name == "PropertyGridView").FirstOrDefault();
method.Invoke(item, new object[] { grid });
}
}
Note: For modal editors the Enter event is annoying and repeats over and over again. To avoid this you can use Click event of the control.
Also as another option you can rely on SelectedGridItemChanged event of PropertyGrid and check if e.NewSelection.GetType().Name == "PropertyDescriptorGridEntry" then call EditPropertyValue of the selected grid item using reflection.

Why didn't trigger the CellEndEdit event of DataGridView

All,I knew we can set a column editable for a DataGridView.
And when finish editing the cell. the CellEndEdit event would be triggered.
But I just want to know why didn't end the edit of cell when I click the blank area of DataGridView. And click the area out of DataGridView doesn't trigger it too. only clicking the other cells could make it happen. It really doesn't make sense. Could anyone know why ? and How to make it ? It try to use the Click event of the DataGridView, But When I click the cell, It also trigger the DataGridView_click event.
private void dgvList_Click(object sender, EventArgs e)
{
dgvFileList.EndEdit();
}
Try using the HitTest function in the MouseDown event of the grid:
void dgvFileList_MouseDown(object sender, MouseEventArgs e) {
DataGridView.HitTestInfo hit = dgvFileList.HitTest(e.X, e.Y);
if (hit.RowIndex < 0 | hit.ColumnIndex < 0) {
dgvFileList.EndEdit();
}
}
Clicking outside the DataGridView control would require hitting a focusable control.
Before BeginEdit. Set a variable to identify if current state is edit mode.
bBeginEdit = true;
dgvFileList.BeginEdit(false);
In the Form_Click event
if (bBeginEdit)
{
dgvFileList.EndEdit();
bBeginEdit = false;
}
Thanks,
Joe
CellEndEdit() causes the event to be fired only if the cell was in edit mode (see Joe.wang's response). You can simply preceed CellEndEdit() with CellBeginEdit() to enter Edit mode (code from CellContentClick-handler, PickNewFont() is a wrapper for the FontDialog):
[...]
else if (String.Compare(rowName, "Font name") == 0) // user clicks on Font-row
{
dgvConfigSettings.BeginEdit(true);
Font newFont = PickNewFont(fontName, fontSize, fontStyle);
dgvConfigSettings.CurrentCell.Tag = newFont; // a bit dirty.... but that way we can pick-up the font in the panel-handler more easily
dgvConfigSettings.CurrentCell.Value = newFont.Name.ToString();
dgvConfigSettings.EndEdit();
}

Changing a tab and clearing content

I have a program which gives two options through which to test a student's knowledge of complex numbers. However, I want the content in the tab (labels, textboxes) to be cleared when the tab is changed. Is there a method I can use to do this?
just handle the SelectedIndexChanged event of TabControl and retrieve all the controls within the tab. Now you can loop through controls and do whatever you want with them, like this-
// SelectedIndexChange Event
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
// Get Selected Tab
var selectedTab = tabControl1.SelectedTab;
foreach (Control ctrl in selectedTab.Controls)
{
if (ctrl is TextBox)
{
(ctrl as TextBox).Text = string.Empty;
}
if (ctrl is Label)
{
(ctrl as Label).Text = string.Empty;
}
// Other Controls....
}
}
hope it helps...!!
You could reset the value of label and textbox in Tabchange event. Take a look at
Tab Change MSDN
Hope it helps

How to deselect textbox if user clicks elsewhere on the form?

Currently in my application it is impossible to deselect a textbox. The only way is to select another textbox. My users and I agree that clicking anywhere else on the form should deselect the current textbox. I tried overriding the MouseDown on many controls and having the focus set to a random label but it doesn't work for some controls like the MenuStrip or scrollbars. Any ideas?
Assuming you have no other controls on your forum, try adding a Panel control that can receive focus.
Set the TabIndex on the Panel control to something less than your TextBox or NumericUpDown control has.
Now, when your main form receives focus, the Panel should receive the focus instead of the TextBox area.
I had a similar issue recently. My interface is very complex with lots of panels and tab pages, so none of the simpler answers I found had worked.
My solution was to programatically add a mouse click handler to every non-focusable control in my form, which would try to focus any labels on the form. Focusing a specific label wouldn't work when on a different tab page, so I ended up looping through and focusing all labels.
Code to accomplish is as follows:
private void HookControl(Control controlToHook)
{
// Add any extra "unfocusable" control types as needed
if (controlToHook.GetType() == typeof(Panel)
|| controlToHook.GetType() == typeof(GroupBox)
|| controlToHook.GetType() == typeof(Label)
|| controlToHook.GetType() == typeof(TableLayoutPanel)
|| controlToHook.GetType() == typeof(FlowLayoutPanel)
|| controlToHook.GetType() == typeof(TabControl)
|| controlToHook.GetType() == typeof(TabPage)
|| controlToHook.GetType() == typeof(PictureBox))
{
controlToHook.MouseClick += AllControlsMouseClick;
}
foreach (Control ctl in controlToHook.Controls)
{
HookControl(ctl);
}
}
void AllControlsMouseClick(object sender, MouseEventArgs e)
{
FocusLabels(this);
}
private void FocusLabels(Control control)
{
if (control.GetType() == typeof(Label))
{
control.Focus();
}
foreach (Control ctl in control.Controls)
{
FocusLabels(ctl);
}
}
And then add this to your Form_Load event:
HookControl(this);
Since you probably have a label, or any other control on your winform, I would go with the solution recommended here and just give the focus to a label when the Form gets clicked.
Worst case, you can even add a label situated at the -100, -100 position, set him as the first in the tab order and Focus() it on form click.
I have some kind of "workaround" for you. Just but another control (that can get the focus) in the background. I tested this for a GridView (which will paint your control grey) - but you should be able to do it with a custom control in the color you want or just set the backgroundcolor of the gridview (doh).
This way everytime the user clicks the background this backgroundcontrol will get the focus.
This is generic answer: To deselect TextBoxes when user clicks anywhere else on the form, first make controls to lose focus. For this subscribe to Click Event of the form:
private void Form1_Click(object sender, EventArgs e)
{
this.ActiveControl = null;
}
Next subscribe your TextBoxes to Focus Leave event and set SelectionLength to 0 (to deselect text, somehow it is not deselected although textbox does not show selection when loses focus):
private void textBoxes_Leave(object sender, EventArgs e)
{
TextBox txbox = sender as TextBox;
txbox.SelectionLength = 0;
}
If you have your TexBoxes nested in custom user control, you have to add events within that user control in a similar manner. Hope that helps to anyone else.

C# - Passing focus to a tabcontrol/page and not being able to mousewheel scroll

I have a combobox with four items that correspond to tabs in a tabcontrol. When the user selects an item from the combobox (by left clicking and left clicking again to select an item) the corresponding tabpage in the tabcontrol is selected. The tabpage is set to autoscroll but when the tabpage is selected in this way mousewheel scrolling does not work. (If I click a control inside that tabpage manually I can then mousewheel scroll..)
If the user mousewheels to select an item from the same combobox (and successfully passes control to the corresponding tabpage) mousewheel scrolling works fine on that tabpage and I cant figure out why.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBox1.SelectedIndex)
{
case 0:
tabControl1.SelectedTab = tabPage3;
tabPage3.Focus();
break;
}
...
}
I can't get a repro of this problem. Something that might help is to set the focus to the first control of the page instead, just like what happens when you fix the problem by clicking a control. And to do so later, after the combobox event is completed. Use this:
private void setFocusToPage(TabPage page) {
var ctl = page.Controls.Count > 0 ? page.Controls[0] : page;
this.BeginInvoke((MethodInvoker)delegate { ctl.Focus(); });
}
Call setFocusToPage instead of the Focus() method in your SelectedIndexChanged event handler.

Categories