I have two TextBoxes and a button control in the form. When the button is clicked the name of the last entered TextBox should be displayed in a MessageBox. At the same time I need to reset the focus to last entered TextBox.
string str=string.Empty;
bool foc;
In button click I wrote the following code
if (MessageBox.Show("You want to reset or continue", "control",
MessageBoxButtons.OKCancel) == DialogResult.Cancel)
{
if (foc== true)
{
textBox1.Focus();
}
else
{
textBox2.Focus();
}
}
When I clicks on cancel button the focus should be into textbox which is entered at last
private void textBox1_Enter(object sender, EventArgs e)
{
str = textBox1.Name;
foc= textBox1.Focus();
}
private void textBox2_Enter(object sender, EventArgs e)
{
str= textBox2.Name;
foc= false;
}
Other than the above lines of code is there any other possibility to focus into the textbox, but when number of textboxes increases how i need to write the conditions.
If I am having textbox,combobox,listbox,checkbox or any other controls in the form then how to find in which control the user enterd at last and set focus to that control by using any function instead of writing in every control Enter event
You can handle Leave event of the TextBoxes to store the Last TextBox Control.
Try this:
this.btnSubmit.Click += new System.EventHandler(this.Submit_Click);
this.btnCancel.Click += new System.EventHandler(this.Cancel_Click);
this.textBox1.Leave += new System.EventHandler(this.textBox1_Leave);
this.textBox2.Leave += new System.EventHandler(this.textBox2_Leave);
TextBox txtLast = new TextBox();
private void textBox1_Leave(object sender, EventArgs e)
{
txtLast = (TextBox)sender;
}
private void textBox2_Leave(object sender, EventArgs e)
{
txtLast = (TextBox)sender;
}
private void Submit_Click(object sender, EventArgs e)
{
MessageBox.Show(txtLast.Text);
}
private void Cancel_Click(object sender, EventArgs e)
{
txtLast.Focus();
}
public bool textBox1WasLastFocused = false, textBox2WasLastFocused = false; // Global Declaration
void textBox2_GotFocus(object sender, EventArgs e)
{
textBox2WasLastFocused = true;
textBox1WasLastFocused = false;
}
void textBox1_GotFocus(object sender, EventArgs e)
{
textBox1WasLastFocused = true;
textBox2WasLastFocused = false;
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1WasLastFocused)
MessageBox.Show("textbox1 was ladst focused !");
else if(textBox2WasLastFocused)
MessageBox.Show("textbox2 was ladst focused !");
}
Related
Is it possible to get any value from the currently displayed tabcontrol that is open (displayed)? Trying to highlight the adjacent / corresponding tab / link label.
I am using link labels as navigation for the tabs. The real (ugly top) tabs will hidden when the project is finished.
//LINK LABELS CLICK EVENTS TO DISPLAY / OPEN TABS
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
tabControl1.SelectedIndex = 0;
//COLOURS TO BE APPLIED WHEN THE CORRESPONDING TAB IS OPEN
linkLabel1.BackColor = Color.Black;
linkLabel1.ForeColor = Color.White;
linkLabel1.ActiveLinkColor = System.Drawing.Color.White;
}
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
tabControl1.SelectedIndex = 1;
txtFirstName.Focus();
}
private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
tabControl1.SelectedIndex = 2;
}
After #Idle_Mind answer I still was not sure how to bind / wireup the event. This is for anyone else with same question:
//LINK LABELS CLICK EVENTS TO DISPLAY / OPEN TABS
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
tabControl1.SelectedIndex = 0;
labels_LinkClicked(sender, e);
}
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
tabControl1.SelectedIndex = 1;
txtFirstName.Focus();
labels_LinkClicked(sender, e);
}
private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
tabControl1.SelectedIndex = 2;
labels_LinkClicked(sender, e);
}
//METHOD TO CALL ON EACH CLICK OF LINK LABELS
private void labels_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
LinkLabel lbl = (LinkLabel)sender;
tabControl1.SelectedIndex = labels.IndexOf(lbl);
foreach (LinkLabel curLbl in labels)
{
curLbl.BackColor = (lbl == curLbl) ? Color.Black : Color.Transparent;
}
}
Wire up the LinkClinked() events to the same event handler like below:
private List<LinkLabel> labels;
private void Form2_Load(object sender, EventArgs e)
{
labels = new List<LinkLabel>() { linkLabel1, linkLabel2, linkLabel3 };
}
private void labels_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
LinkLabel lbl = (LinkLabel)sender;
tabControl1.SelectedIndex = labels.IndexOf(lbl);
foreach(LinkLabel curLbl in labels)
{
curLbl.BackColor = (lbl == curLbl) ? Color.Black : Color.Gray;
}
}
Is this possible to display button on Windows Form only when focus is on specific textbox?
Tried that with this approach:
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("OK");
}
private void textBox2_Enter(object sender, EventArgs e)
{
button3.Visible = true;
}
private void textBox2_Leave(object sender, EventArgs e)
{
button3.Visible = false;
}
No luck, because button click does not work then, because button is hidden immediately after textbox lost focus, preventing it from firing button3_Click(/*...*/) { /*...*/ } event.
Now I'm doing it like that:
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("OK");
}
private void textBox2_Enter(object sender, EventArgs e)
{
button3.Visible = true;
}
private void textBox2_Leave(object sender, EventArgs e)
{
//button3.Visible = false;
DoAfter(() => button3.Visible = false);
}
private async void DoAfter(Action action, int seconds = 1)
{
await Task.Delay(seconds*1000);
action();
}
Form now waits for a second and only then hides button3.
Is there any better approach?
I think you want to display the button only when focus is on specific textbox or the focus is on the button.
To do this you can check the Focused property of button3 in the Leave event of textBox2 and only hide the button if the button doesn't have focus. Note that the button will get focus before the Leave event of textBox2 fires.
You will then need to hide the button in the scenario where button3 loses focus and the focus moves to somewhere other than textBox2. You can use exactly the same technique here by handling the Leave event of button3 and only hiding button3 if textBox2 does not have focus.
The following code should fit your requirements:
private void textBox2_Leave(object sender, EventArgs e)
{
if (!button3.Focused)
{
button3.Visible = false;
}
}
private void button3_Leave(object sender, EventArgs e)
{
if (!textBox2.Focused)
{
button3.Visible = false;
}
}
private void textBox2_Enter(object sender, EventArgs e)
{
button3.Visible = true;
}
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("Button clicked");
}
Why not work with the GotFocus and LostFocus event of the TextBox?
private void textBox2_GotFocus(object sender, EventArgs e)
{
button3.Visible = true;
}
Then hide the button on the click event.
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("OK");
button3.Visible = false;
}
How about you add a Panel and place the button and text boxes in that panel and when user MouseHovers that Panel then display the button...
This way user would be able to click on the button...
This is the event you are looking for, I think...
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousehover(v=vs.110).aspx
UPDATE:
var textboxFocussed = false;
private void textBox2_Enter(object sender, EventArgs e)
{
textboxFocussed = true;
}
private void textBox2_Leave(object sender, EventArgs e)
{
textboxFocussed = false;
}
UPDATE 2
private void Panel_GotFocus(object sender, EventArgs e)
{
button3.Visible = textboxFocussed;
}
private void Panel_LostFocus(object sender, EventArgs e)
{
button3.Visible = false;
}
Here are the details of the Panel Events
you can add Enter event handler for all controls on form at Load. Just make sure to skip the controls on which you want to show the button.
List<string> strControlException = new List<string>();
public Form1()
{
InitializeComponent();
strControlException.Add("btnMain");
strControlException.Add("txtMain");
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < this.Controls.Count;i++ )
{
if (!strControlException.Contains(Controls[i].Name))
{
Controls[i].Enter += new EventHandler(hideButton);
}
}
}
private void txtMain_Enter(object sender, EventArgs e)
{
btnMain.Visible = true;
}
private void hideButton(object sender, EventArgs e)
{
btnMain.Visible = false;
}
btnMain (Button you want to Manipulate) and txtMain (Which controls the vibility of the button) are the controls in contention here
Add more controls on the form to test.
Explanation for the above code :
First initialize a list with the names of controls that should show the Button
On Form Load add an Event handler to all controls (except the one in our list)
In the handler function hide the button. (You might want to perform more logic here based on the control that called this function)
Button is hidden by default and only on textbox Enter event we show the button.
i have 3 radio buttons:
rbtPercentualmedioanual
rbtPercentualmensal
rbtValorfixo
I would like to change options events for textbox1 according choosed option
If chose rbtValorfixo,
It will uncomment:
private void textbox1_TextChanged(object sender, EventArgs e)
{
//substituipontovirgula_textBox(sender as TextBox, e);
}
private void textbox1_Leave(object sender, EventArgs e)
{
//formatamoeda_textBox(sender as TextBox, e);
}
private void textbox1_KeyPress(object sender, KeyPressEventArgs e)
{
//numeropontoouvirgula_textBox(sender as TextBox, e);
formatarporcentagem_textBox(sender as TextBox, e);
}
and will comment
private void textbox1_KeyPress(object sender, KeyPressEventArgs e)
{
numeropontoouvirgula_textBox(sender as TextBox, e);
//formatarporcentagem_textBox(sender as TextBox, e);
}
If choose option rbtPercentualmedioanual or rbtPercentualmensal, it will comment:
private void textbox1_TextChanged(object sender, EventArgs e)
{
substituipontovirgula_textBox(sender as TextBox, e);
}
private void textbox1_Leave(object sender, EventArgs e)
{
formatamoeda_textBox(sender as TextBox, e);
}
private void textbox1_KeyPress(object sender, KeyPressEventArgs e)
{
numeropontoouvirgula_textBox(sender as TextBox, e);
//formatarporcentagem_textBox(sender as TextBox, e);
}
and will uncomment: formatarporcentagem_textBox
private void textbox1_KeyPress(object sender, KeyPressEventArgs e)
{
//numeropontoouvirgula_textBox(sender as TextBox, e);
//formatarporcentagem_textBox(sender as TextBox, e);
}
i dont know how comment/uncomment keypress, textchanged or focusleave event using radiobutton check, only say how, not need make all, i can do it, but i have to know if its possible and if is, how ?
Thanks
You can determine if a radiobutton or checkbox is checked by the radioButton.Checked property.
In you case it will be something like this:
private void textbox1_TextChanged(object sender, EventArgs e)
{
if(!rbtValorfixo.Checked)
substituipontovirgula_textBox(sender as TextBox, e);
}
or:
private void textbox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(rbtValorfixo.Checked)
numeropontoouvirgula_textBox(sender as TextBox, e);
else
formatarporcentagem_textBox(sender as TextBox, e);
}
and so on.
You don't need to comment or uncommment code. Being within a programming language, you need to look out for right structures.
In this rather simple case, you just need an "if" block.
This might help
You may want to use something like this? Not sure as it's quite unclear what you are trying to say.
private void Operation_Changed(object sender, EventArgs e)
{
RadioButton rbt = sender as RadioButton;
if (btn != null)
{
case "rbtPercentualmedioanual":
_operation = new example2();
example1.Visible = true;
example2.Visible = true;
example3.Visible = false;
return;
case "rbtPercentualmensal":
_operation = new example3();
example1.Visible = true;
example2.Visible = true;
example3.Visible = false;
return;
case "rbtValorfixo":
_operation = new example1();
example1.Visible = true;
example2.Visible = true;
example3.Visible = false;
default:
break;
And change it to what you want to show
When I click on a textbox, I want the default text to disappear. Is there any other property that would work for this purpose?
The following Will set Default text in Gray color. Also When you leave the textbox as blank, again set default text to the textbox.
private void Form1_Load(object sender, EventArgs e)
{
this.textBox2.Enter += new EventHandler(textBox2_Enter);
this.textBox2.Leave += new EventHandler(textBox2_Leave);
textBox2_SetText();
}
protected void textBox2_SetText()
{
this.textBox2.Text = "Default Text...";
textBox2.ForeColor = Color.Gray;
}
private void textBox2_Enter(object sender, EventArgs e)
{
if (textBox2.ForeColor == Color.Black)
return;
textBox2.Text = "";
textBox2.ForeColor = Color.Black;
}
private void textBox2_Leave(object sender, EventArgs e)
{
if (textBox2.Text.Trim() == "")
textBox2_SetText();
}
Add a method to the GotFocus event of the TextBox that will change the Text property to ""
private void Form1_Load(object sender, EventArgs e) {
this.textBox1.GotFocus += new EventHandler(textBox1_Focus);
this.textBox1.Text = "some default text...";
}
protected void textBox1_Focus(Object sender, EventArgs e) {
textBox1.Text = "";
}
It sounds like you are wanting a Watermark in your Textbox.
See these articles.
http://www.codeproject.com/Articles/319910/Custom-TextBox-with-watermark
Watermark in System.Windows.Forms.TextBox
and if you are using wpf something like this http://msdn.microsoft.com/en-us/library/bb613590.aspx
How to create a Textbox which displays "search" in grey color when it is empty and standard behavior when user starts typing text into it?
Do it via TextBox Events Enter and Leave and Attributes:
private void textBox1_Leave(object sender, EventArgs e)
{
if(textBox1.Text.Trim().Length == 0)
{
textBox1.Text = "Search";
textBox1.ForeColor = Color.LightGray;
}
}
private void textBox1_Enter(object sender, EventArgs e)
{
textBox1.Text = string.Empty;
}
See this thread at MSDN for a possible solution: http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/93a67793-6426-4d4f-be9d-a9b79725efc8