I have a combo box. It must display its content, when focused and its value changed as well.
I wrote this code in its Value Change event:
if(combo1.Focused)
combo1.DroppedDown=true;
But it doesn't work!
what's your solution?
What Event handler are you putting that code in? Assuming that you want to show the drop down when the user types in the edit box part of the combo just handle the TextChanged event and put that code inside there and it should work.
If I understand your requirement correctly, when the combobox gets focus you want the drop down list to show. That can be achieved as follows
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.GotFocus += new EventHandler(comboBox1_GotFocus);
}
void comboBox1_GotFocus(object sender, EventArgs e)
{
comboBox1.DroppedDown = true;
}
Related
I am relatively new to c# but I am currently creating a Windows Form that has an editor window. I'm struggling with the Paste button though as I have 2 textbox fields, one for the title of the note and one for the note itself. I'm wanting to be able to paste from the clipboard into either textbox.
I have tried using if statements based on noteText.Focused and titleText.Focused but obviously this doesn't work as the Paste button becomes focused as soon as you click it.
Any suggestions would be of great help.
Create a local variable and save last focused textBox in it.
//subscribe both textBoxes with same GotFocus event handler
textBox1.GotFocus += textBox_GotFocus;
textBox2.GotFocus += textBox_GotFocus;
//local variable
TextBox lastSelected;
//GotFocus
private void textBox_GotFocus(object sender, EventArgs e)
{
//save last Selected textBox
lastSelected = sender as TextBox;
}
private void button1_Click_1(object sender, EventArgs e)
{
//on click get value from clipboard
if(lastSelected != null)
lastSelected.Text = Clipboard.GetText();
}
I'm not sure what's going wrong but I can't select the Text of my TextBox in a dialog.
I added a FirstTimeLoadedHandler to Loaded in my view where I select the text:
public ParticipantView()
{
InitializeComponent();
Loaded += FirstTimeLoadedHandler;
}
private void FirstTimeLoadedHandler(object sender, RoutedEventArgs e)
{
SurnameBox.Focus();
Keyboard.Focus(SurnameBox);
SurnameBox.SelectAll();
}
However my textbox is in Keyboard-Focus, but not selected at all. I'm not sure why it's not working.
I thought it has something to do with my databinding but the data should be received before Loaded fires or am I wrong?
Your code would work fine with two small changes. The first is that obviously, selecting all on a TextBox that has no text in it will have no effect. Secondly, if you focus it after calling SelectAll, you'll have more luck. Try this:
private void FirstTimeLoadedHandler(object sender, RoutedEventArgs e)
{
SurnameBox.Text = "This text is selected";
SurnameBox.SelectAll();
Keyboard.Focus(SurnameBox);
}
I have a DevExpress' XtraGrid which is bound to a collection of objects. I want changes to get into the underlying datasource immediately on change. But the default DevExpress behavior is to put new values into the datasource only when the user has left the cell. So by default when the user types "Hello world" into a cell, the datasource will receive the whole sentence in one go. But I want it to receive "H", "He", "Hel" and so on.
I tried to call PostEditor() in CellValueChanging event handler but it didn't help. Any other ideas?
Grid's in-place editors provide the EditValueChanged event that occur when an end-user types within the editor or changes its value somehow. You can handle this event to post the currently edited value to the data source.
So, I recommend you use the following approach:
//...
gridView.ShownEditor += gridView_ShownEditor;
gridView.HiddenEditor += gridView_HiddenEditor;
}
DevExpress.XtraEditors.BaseEdit gridViewActiveEditor;
void gridView_ShownEditor(object sender, EventArgs e) {
gridViewActiveEditor = gridView.ActiveEditor;
gridViewActiveEditor.EditValueChanged += ActiveEditor_EditValueChanged;
}
void gridView_HiddenEditor(object sender, EventArgs e) {
gridViewActiveEditor.EditValueChanged -= ActiveEditor_EditValueChanged;
}
void ActiveEditor_EditValueChanged(object sender, EventArgs e) {
gridView.PostEditor();
}
I think CellValueChanging is the event to trap but instead of PostEditor() try UpdateCurrentRow().
This code in the view's CellValueChanging event handler solved the problem:
private void OnCellValueChanging(object sender, CellValueChangedEventArgs e)
{
_gridView.SetFocusedRowCellValue(_gridView.FocusedColumn, e.Value);
}
I have a tree view on the left side. Selecting a node displays relevant information in a form on the right side.
Would I be able to keep the tree and any one control (textbox, combobox, checkbox) on the right in focus at the same time? This will enable a user to select a field, make a change, select another node, and without having to go back and select the same field again, just type and change the value of the same field.
Thanx.
EDIT
I suppose one could implement such behaviour manually:
private Control __cFocus;
private void {anyControl}_Focus(object sender, EventArgs e)
{
__cFocus = (Control)sender;
}
private void treeView1_AfterSelect(object sender, EventArgs e)
{
__cFocus.Focus();
}
I was just wondering if there exists an automatic / more elegant solution
EDIT 2
Ok, so it seems I'll have to implement it manually. Manual implementation it is then. However, now there seem to be another problem; not sure if I should ask this as a separate question.
When selecting a node the textbox gains focus as intended, but only when using the keyboard. It doesn't work when selecting a node with the mouse. First I thought that it might be a mouse event that's interfering, but stepping revealed that the MouseUp event fired first and then the AfterSelect event which sets the focus, so I don't think it's interfering. The textbox's Enter event is also fired, but for some reason it loses focus again to the tree.
Thanx
no, you cannot keep two controls in focus at the same time. But what you can do is set the focus to the target control in the treeview AfterSelect event
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
textBox1.Focus();
textBox1.SelectAll();
}
then in your textbox leave, save the changes, like so:
private void textBox1_Leave(object sender, EventArgs e)
{
//save changes here
}
this way, everytime you select an item in the treeview, check your textbox for change and save as needed, then you will refocus on the textbox for your next edit
There only can be one element having the focus!
But I have an idea for you that might solve your problem. Assuming you have a window with a TreeView and a TextBox. Set the HideSelection property of the TreeView to false and subscribe the AfterSelect event (like edeperson already answered) like this:
private void OnTreeViewAfterSelect(object sender, TreeViewEventArgs e)
{
textBox1.Text = e.Node.Text;
textBox1.Focus();
}
Then subscribe the KeyDown event of the TextBox and do following in the event method:
private void OnTextBoxKeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode == Keys.Up) || (e.KeyCode == Keys.Down))
{
treeView1.Focus();
SendKeys.Send(e.KeyCode == Keys.Up ? "{UP}" : "{DOWN}");
}
}
At last subscribe the Leave event of the TextBox and do following in the event method:
private void OnTextBoxLeave(object sender, EventArgs e)
{
if (treeView1.SelectedNode != null)
{
treeView1.SelectedNode.Text = textBox1.Text;
}
}
And, voilá it should work like you expected it...
If you want to focus on it , you can use usercontrol. you can put your textbox on usercontrol and set focus of this textbox on usercontrol using set properties on treeview select.
No you may not, only one control may be in focus at any given time.
See Moonlight's comment for one way to achieve the behavior that you seek.
I am new to using Event Handling in C# .NET, and I am trying to understand the behavior behind some simple code that I am experimenting with. I am working with a more complicated example, but I am hoping I will get a more focused answer if I simplify the example.
I have the following code which defines a main window with a ListBox that is initialized with values, and a panel in the window. I am working with dragging the ListBox Items and dropping them in the panel. To signify that the panel is reading the DragDrop event, I am simply just changing the background color.
My problem is, it is not changing the background color when I drop the values, hence, the DragDrop is not working. I know this is a bit exaggerated, but I am trying to understand why its not working.
Here is the following code that I am using.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Allow Panel to accept dropped values
this.panel1.AllowDrop = true;
//Initialize ListBox with sample values
listBox1.Items.Add("First Name");
listBox1.Items.Add("Last Name");
listBox1.Items.Add("Phone");
//Setup DragDrop Event Handler - is this correct, or even needed?
this.panel1.DragDrop += new DragEventHandler(panel1_DragDrop);
}
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
ListBox box = (ListBox)sender;
String selectedValue = box.Text;
DoDragDrop(selectedValue.ToString(), DragDropEffects.Copy);
}
private void panel1_DragDrop(object sender, DragEventArgs e)
{
//Change Background color to signify value has been dropped
((Panel)sender).BackColor = Color.Black;
}
}
I realize this is an oversimplified example. If you see what I am doing wrong, then please let me know.
EDIT To give an example of why I am confused, I change this example around to put the dragged ListBox Item text into a Textbox when the DragOver event was fired, and it worked fine, but when I tried to do the same thing when they dropped the values over the textbox, I could not get it to work.
Handle the panel's DragEnter event and set e.Effects to something other than None.