I'm developing a windows forms application. And i have the following issue: in a form there's a panel, and in that panel i have a number of controls (just a label with a text box, the number is determined at runtime). This panel has a size that is smaller than the sum of all the controls added dynamically. So, i need a scroll. Well, the idea is: when the user open the form: the first of the controls must be focused, the the user enters a text and press enter, the the next control must be focused, and so until finished.
Well it's very probably that not all the controls fit in the panel, so i want that when a control inside the panel got focus the panel scrolls to let the user see the control and allow him to see what he is entering in the text box.
I hope to be clear.
here is some code, this code is used to generated the controls and added to the panel:
List<String> titles = this.BancaService.ValuesTitle();
int position = 0;
foreach (String title in titles)
{
BancaInputControl control = new BancaInputControl(title);
control.OnInputGotFocus = (c) => {
//pnBancaInputContainer.VerticalScroll.Value = 40;
//pnBancaInputContainer.AutoScrollOffset = new Point(0, c.Top);
// HERE, WHAT CAN I DO?
};
control.Top = position;
this.pnBancaInputContainer.Controls.Add(control);
position += 10 + control.Height;
}
If you set AutoScroll to true this will be taken care of automatically. As for the idea that Enter should move focus to next field, the best solution would be to execute enter as if it was TAB key in BancaInputControl:
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.KeyCode == Keys.Enter)
{
e.Handled = true;
// Move focus to next control in parent container
Parent.SelectNextControl(this, true, true, false, false);
}
}
If BancaInputControl is composite control (a UserControl containing other controls) each child control should hook up KeyDown event to this handler. It tries to move focus to next control in BancaInputControl; if it fails, moves focus to parent container's next control.
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.Handled = true;
if (!SelectNextControl((Control)sender, true, true, false, false))
{
Parent.SelectNextControl(this, true, true, false, false);
}
}
}
Related
So the scenario that I have is that there is a Form with three text boxes and a button. Clicking the button sets textBox1.Enabled = false, textBox2.Enabled = false, and textBox3.Focus().
The problem that I'm running into is that if either textBox1 or textBox2 has focus at the moment the user clicks the button, the text box becomes disabled but retains a greyed-out version of the focus rectangle. It's like the form isn't redrawing the disabled text box. Please observe the attached screenshot and notice the difference between the first and second text box.
How do I ensure that I move focus to textBox3 and get rid of the focus rectangle around textBox1?
I'm not sure if this behavior is a bug, but i found way to handle it. The trick is play around with BorderStyle property.
private void button1_Click(object sender, EventArgs e)
{
textBox3.Focus();
var borderStyle = textBox1.BorderStyle;
textBox1.BorderStyle = BorderStyle.None;
textBox2.BorderStyle = BorderStyle.None;
textBox1.Enabled = false;
textBox2.Enabled = false;
textBox1.BorderStyle = borderStyle;
textBox2.BorderStyle = borderStyle;
textBox1.Refresh();
}
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.
I have a telerik grid view and when I right click the header, I'm showing a with a ListBox inside containing the list of columns.
The item template is redefined to show a check box so I can set the column visible or not.
I can also drag/drop columns to reorder them.
So here is how I create my Popup:
var view = new ColumnsOrderer.ColumnsOrderer
{
DataContext = new ColumnsOrderer.ViewModelColumnsOrderer(Columns)
};
var codePopup = new Popup
{
Child = view,
MaxHeight = 400,
StaysOpen = false,
Placement = PlacementMode.Mouse
};
codePopup.IsOpen = true;
Now everything seems to work correctly, but it's not.
If I set columns visible or hidden and then click outside the popup, it closes correctly.
Though if I drag an item to reorder it, the popup seems to lose focus and then it won't close if I click outside the popup. I have to click back in the list box inside the popup and then it closes by clicking outside.
Here is my drag/drop events:
public ColumnsOrderer()
{
InitializeComponent();
InitialiazeListBoxDragDrop();
}
private void InitialiazeListBoxDragDrop()
{
var itemContainerStyle = new Style(typeof(ListBoxItem));
itemContainerStyle.Setters.Add(new Setter(AllowDropProperty, true));
itemContainerStyle.Setters.Add(new EventSetter(PreviewMouseMoveEvent, new MouseEventHandler(OnMouseMove)));
itemContainerStyle.Setters.Add(new EventSetter(DropEvent, new DragEventHandler(OnDrop)));
listColumns.ItemContainerStyle = itemContainerStyle;
}
void OnMouseMove(object sender, MouseEventArgs e)
{
if (e.OriginalSource is CheckBox || e.LeftButton == MouseButtonState.Released)
return;
if (sender is ListBoxItem)
{
var draggedItem = sender as ListBoxItem;
draggedItem.IsSelected = true;
DragDrop.DoDragDrop(draggedItem, draggedItem.DataContext, DragDropEffects.Move);
}
}
void OnDrop(object sender, DragEventArgs e)
{
if (!(sender is ListBoxItem))
return;
}
An interesting thing is that if I remove the OnDrop handler, the problem is not there.
I tried many ways to set back the focus to the popup, but it's not working.
Could anyone help me on that?
How about trying to re-focus your Popup control after the drag and drop operation?
void OnDrop(object sender, DragEventArgs e)
{
if (!(sender is ListBoxItem))
return;
codePopup.Focus();
}
The problem:
My application requires a user to be able to select multiple entries in a datagrid via a column of checkboxes. The desired behavior is that when you click on a checkbox in the column, it behaves like a normal checkbox, but if you drag over it while the left mouse button is down, its selection state changes to the opposite of what is was before.
What I have tried so far:
I have tried subclassing CheckBox and handling OnMouseEnter, but the first checkbox that is clicked seems to capture the mouse so no other checkboxes fire the OnMouseEnter event.
I have tried implementing a drag-and-drop hack, where the user clicks to select a checkbox, and then drags that checkbox over the others so the others recieve a DragOver event and can switch states. This solution causes the cursor to display as a circle with a slash when not over another checkbox during the drag and drop, which is not acceptable for this application.
What I would like:
I would like a method to implement a checkbox that has the functionality I describe, ideally in a xaml style or subclass that I can reuse, as this functionality is needed in multiple places in my application.
Is there an elegant way to achieve this effect?
I did this in my application, very handy when you have to select, say, 30 checkBoxes.
To do this, i handled the preview mouse event myself : PreviewMouseLeftButtonDown, PreviewMouseMove, PreviewMouseLeftButtonUp.
In PreviewMouseLeftButtonDown : i get the mouse position relative to the control.
In PreviewMouseMove : i draw a rectangle from start to current position if i am far enough from firstPoint. then i iterate in CheckBoxes, see if they intersect with rectangle, and highlight them if so (so the user know whiwh chexboxes will swap)
In PreviewMouseLeftButtonUp : i do the swap for intersecting CheckBoxes.
if it can help you, here's the code i use. it is not MVVM (:-)) but works fine, it might give you ideas. it is an automatic translation from vb.net code.
To make it work, you need a Canvas on top of your CheckBoxes (=within the same grid cell for instance), with property IsHitTestVisible="False" .
Within this Canvas, put a Rectangle nammed "SelectionRectangle" with proper fill and stroke, but with 0.0 Opacity.
// '' <summary>
// '' When Left Mouse button is pressed, remember where the mouse move start
// '' </summary>
private void EditedItems_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) {
StartPoint = Mouse.GetPosition(this);
}
// '' <summary>
// '' When mouse move, update the highlight of the selected items.
// '' </summary>
private void EditedItems_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e) {
if ((StartPoint == null)) {
return;
}
PointWhereMouseIs = Mouse.GetPosition(this);
Rect SelectedRect = new Rect(StartPoint, PointWhereMouseIs);
if (((SelectedRect.Width < 20)
&& (SelectedRect.Height < 20))) {
return;
}
// show the rectangle again
Canvas.SetLeft(SelectionRectangle, Math.Min(StartPoint.X, PointWhereMouseIs.X));
Canvas.SetTop(SelectionRectangle, Math.Min(StartPoint.Y, PointWhereMouseIs.Y));
SelectionRectangle.Width = Math.Abs((PointWhereMouseIs.X - StartPoint.X));
SelectionRectangle.Height = Math.Abs((PointWhereMouseIs.Y - StartPoint.Y));
foreach (CheckBox ThisChkBox in EditedItems.Children) {
object rectBounds = VisualTreeHelper.GetDescendantBounds(ThisChkBox);
Vector vector = VisualTreeHelper.GetOffset(ThisChkBox);
rectBounds.Offset(vector);
if (rectBounds.IntersectsWith(SelectedRect)) {
((TextBlock)(ThisChkBox.Content)).Background = Brushes.LightGreen;
}
else {
((TextBlock)(ThisChkBox.Content)).Background = Brushes.Transparent;
}
}
}
// '' <summary>
// '' When Left Mouse button is released, change all CheckBoxes values. (Or do nothing if it is a small move -->
// '' click will be handled in a standard way.)
// '' </summary>
private void EditedItems_PreviewMouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e) {
PointWhereMouseIs = Mouse.GetPosition(this);
Rect SelectedRect = new Rect(StartPoint, PointWhereMouseIs);
StartPoint = null;
SelectionRectangle.Opacity = 0;
// hide the rectangle again
if (((SelectedRect.Width < 20)
&& (SelectedRect.Height < 20))) {
return;
}
foreach (CheckBox ThisEditedItem in EditedItems.Children) {
object rectBounds = VisualTreeHelper.GetDescendantBounds(ThisEditedItem);
Vector vector = VisualTreeHelper.GetOffset(ThisEditedItem);
rectBounds.Offset(vector);
if (rectBounds.IntersectsWith(SelectedRect)) {
ThisEditedItem.IsChecked = !ThisEditedItem.IsChecked;
}
((TextBlock)(ThisEditedItem.Content)).Background = Brushes.Transparent;
}
}
Edit : i used that code within a user control. This control takes a list of booleans and a list of strings (caption) as argument, and builds (with a WrapPanel) an array of CheckBoxes having the right caption. And so you can select/unselect with the rectangle, and there are also two buttons to check all/uncheck all. I tried also to keep good column/rows ratio to handle selection of 7 to 200 booleans with a good column/row balance.
This topic is a few months old but I think I have the elegant answer you are looking for.
Since you describe dragging and checking as two separate behaviors, first set your datagrid MouseDown handler...
yourdatagrid.MouseDown += DragCheck_MouseDownHandler;
This will allow a drag start from the datagrid background (but not a control in the grid.)
private void DragCheck_MouseDownHandler(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
Control dgrid = sender as Control;
foreach (CheckBox box in dgrid.Controls.OfType<CheckBox>())
{
box.Tag = null;
}
dgrid.MouseMove += DragMove_MouseMoveHandler;
}
This uses the checkbox.Tag as a toggle for one pass only dragging while the mouse is down. If you use the CheckBox tags for something else I am sure you can find your own way to identify the boxes. The datagrid.MouseMove is set for the next handler....
private void DragMove_MouseMoveHandler(object sender, MouseEventArgs e)
{
Control dgrid = sender as Control;
Point now = dgrid.PointToClient(Cursor.Position);
if (e.Button == MouseButtons.Left)
{
Control under = dgrid.GetChildAtPoint(now);
if (under != null && under.GetType() == typeof(CheckBox))
{
//if the point has a valid CheckBox control under it
CheckBox box = under as CheckBox;
if (box.Tag == null)// not yet been swiped
{
box.Checked = !box.Checked;
box.Tag = true;
}
}
}
else
{
//if MouseButtons no longer registers as left
//remove the handler
dgrid.MouseMove -= DragMove_MouseMoveHandler;
}
}
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.