I am using mask edit TextBox.The textbox always shows 0 (zero). I cannot type any key from the keyboard. I need to delete the zero first then I can type digits. Therefore I am doing extra steps here. Is it possible to type as soon as I type from the keyboard? Any suggestion is welcome.
private void DateDay_GotFocus(object sender, RoutedEventArgs e)
{
if (((TextBox)sender).Text == "Day")
((TextBox)sender).Text = string.Empty;
}
private void DateDay_LostFocus(object sender, RoutedEventArgs e)
{
if (((TextBox)sender).Text == string.Empty)
((TextBox)sender).Text = "Day";
else
CheckForCorrectDateDay((TextBox)sender);
}
I have tried with Focus event but not successful:
You need to select all content in the textbox in GotFocus event. For MaskedTextBox control it handle the selection internally after the focus event fire. So we need to do BeginInvoke to call the SelectAll() afterward.
private void DateDay_GotFocus(object sender, RoutedEventArgs e)
{
this.BeginInvoke((MethodInvoker)delegate() {
((TextBox)sender).SelectAll();
});
}
This way you can start typing directly.
You can't make the text null if null is not allowed.
WPF version:
private void TextBox_GotFocus(object sender, RoutedEventArgs e) {
this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate() {
((TextBox)sender).SelectAll();
});
}
Alternate solution for MaskedTextBox using the Enter Event
private void maskedEdit_Enter(object sender, EventArgs e)
{
MaskedTextBox maskedTextBox = (MaskedTextBox)sender;
maskedTextBox.BeginInvoke
(new Action
(() =>
{
maskedTextBox.SelectAll();
}
)
);
}
Related
I am trying to get the text value from a button that was clicked. In my head, it looks something like this:
private void button2_Click(object sender, EventArgs e)
{
string s = thisbutton.text
}
The object which fired the event is sender, so:
private void button2_Click(object sender, EventArgs e)
{
string s = (sender as Button).Text;
}
Just cast the sender Object to a Button Object and access the text attribute :
protected void btn_Click (object sender, EventArgs e){
Button btn = sender as Button;
string s= btn.Text
}
Should be like this:
private void button2_Click(object sender, EventArgs e)
{
string s = this.button2.Text;
}
In every build in event handler there are 2 parameters sender and e.Sender takes reference to that object which fires the event.The second parameter e holds some information about the event(such as the location of pointer and other of this kind)
You need only bring it to Button type and get what information you want
try and apply this example in your button event
private void button_click(object sender, EventArgs e)
{
var getValue = ((Button)sender).Text; //this will get the value of the text using sender
}
This was asked some time ago and the platform in my case may be a little different to what the OP used but I arrived at the same question for GTK.
I am developing in Xaramin / Visual Studio in OSX using GTK2+, and for me the original accepted answer is close, but yields an error that .Text doesn't exist. In my case, it needs to be Label. This works for me:
protected void Button_Clicked(object sender, EventArgs e)
{
Button btn = sender as Button;
lblWhichButton.Text = btn.Label;
if (btn.Label == "<<<" )
i--;
else
i++;
lblCounter.Text = "" + i;
}
I haven't found something that matches my problem so I ask it here. I have some code which belongs to a Textbox:
if ((sender as TextBox).Text == form1.filterType())
{
//Do something
}
This comes from the TextBox TextChanged event. So when the TextChanged Event gets fired it calls a method which has the if-construct above and recognizes that the textchanged event came from the textbox.
Now I want exactly the same just when someone writes into a cell in a DataGridView (not when it's just clicked - when the content changes).
How to do this correctly and which event fires when the content changes in a cell without leaving the cell?
I have found a solution for this:
private void Form1_Load(object sender, EventArgs e)
{
this.dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
}
void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 0)
{
TextBox tb = (TextBox)e.Control;
//"unwire" the event before hooking it up ensures the event handler gets called only once
tb.TextChanged -= new EventHandler(tb_TextChanged);
tb.TextChanged += new EventHandler(tb_TextChanged);
}
}
void tb_TextChanged(object sender, EventArgs e)
{
MessageBox.Show("changed");
}
Now it fires everytime the value in the cell gets changed - it behaves like a textbox.
Now - I still need a solution for the "if-construct" above. How to do this exactly? I've casted the cell into a textbox but now? Does this still comes from the dataGridview?
Once I had a similar problem an solved it this way (think there are better ones but it worked)
private void DataGridView1_onFocus ( Object sender, EventArgs e)
{
DataGridView1.onKeyPress+=DataGridView1_onKeyStroke;
}
private void DataGridView1_onFocusLost( Object sender, EventArgs e)
{
DataGridView1.onKeyPress-=DataGridView1_onKeyStroke;
}
private void DataGridView1_onKeyStroke( Object sender , EventArgs e)
{
//Do your thing
}
This is probably an easy one for some of you.
I have a TextBox and a ListBox. ListBox provides options for the TextBox and copies selected item's text to TextBox on DoubleClick event. ListBox becomes visible only when TextBox fires Enter event. I do not want to discuss my reasons for selecting this control combination.
I want ListBox to disappear when any other control within the Form gets focus. So I capture Leave event of TextBox and call ListBox.Visible = fale The problem is that TextBox will also loose focus when I click on ListBox to select provided option thus preventing me from selecting that option.
What event combination should I use to preserve ListBox to select option but hide it whenever other controls get focus?
In the Leave method, you can check to see if the ListBox is the focused control or not before changing its Visibility:
private void myTextBox_Leave(object sender, EventArgs e)
{
if (!myListBox.Focused)
{
myListBox.Visible = false;
}
}
This example will provide you with the desired outcome:
public Form1()
{
InitializeComponent();
textBox1.LostFocus += new EventHandler(textBox1_LostFocus);
textBox1.GotFocus += new EventHandler(textBox1_GotFocus);
}
void textBox1_GotFocus(object sender, EventArgs e)
{
listBox1.Visible = true;
}
void textBox1_LostFocus(object sender, EventArgs e)
{
if(!listBox1.Focused)
listBox1.Visible = false;
}
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
textBox1.Text = listBox1.SelectedItem.ToString();
}
private void Form1_Shown(object sender, EventArgs e)
{
//if your textbox as focus when the form shows
//this is the place to switch focus to another control
listBox1.Visible = false;
}
I have a textBox and search button, I would ask about how can I make the user can click Enter to start searching without need to go and click the search button?
This would be best practice
private void txtSearch_Enter(object sender, EventArgs e)
{
AcceptButton = btnSearch;
}
private void txtSearch_Leave(object sender, EventArgs e)
{
AcceptButton = null;
}
The Form has a property called "AcceptButton" that identifies a button that should be associated to the "Enter" keypress. Its considered the "default action" for the form.
More info here:
Windows Form - AcceptButton property
If you want to use something other than Enter/Return, you could also try:
private void EnterKeyAction()
{
// Search...
}
private void btnEnter_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
EnterKeyAction();
}
private void btnEnter_Click(object sender, EventArgs e)
{
EnterKeyAction();
}
I have a dropdown list and radio button. If something is selected from the dropdown by the user, I want the radio button cleared. If the radio button is selected I want the selection of the dropdown cleared. Unfortunately, this creates events that cancel each other out. I tried using the sender as shown below to determine if the value was being changed by code or by the user, but that doesn't work. How do I make these events only work if the user is the source of the action?
private void rbBlank_Checked(object sender, RoutedEventArgs e)
{
// Verify source of event
if (sender is RadioButton)
{
// Display
comboBoxTitles.SelectedIndex = -1;
}
}
private void comboBoxTitles_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
// Verify source of event
if (sender is ComboBox)
{
// Display
rbBlank.IsChecked = false;
}
}
You won't be able to tell the difference between the two since the source will be the same instance for both occasions.
This doesn't answer the question directly but if you compare the SelectedIndex of comboBoxTitles in the SelectionChanged event handler, your problem should be solved
private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (comboBoxTitles.SelectedIndex != -1)
{
rbBlank.IsChecked = false;
}
}
Try to compare if sender == instance of a control instead of is type of.
private void rbBlank_Checked(object sender, RoutedEventArgs e)
{
// Verify source of event
if (sender == rbBlank)
{
// Display
comboBoxTitles.SelectedIndex = -1;
}
}
private void comboBoxTitles_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
// Verify source of event
if (sender == comboBoxTitles)
{
// Display
rbBlank.IsChecked = false;
}
}
If you know the IDs of those controls, you can try something like this:
System.Web.UI.WebControls.WebControl webControl = (System.Web.UI.WebControls.WebControl) sender;
if( webControl.ID == <comboboxId>)
{
//Do something
}
I havent tried this, but I guess it might work.