How do I specify the default button of a TextBox that is referred to by AcceptsReturn? - c#

I have a windows form with a tool strip on it containing a text box and some buttons (think browser url bar with a go button, back, forward)
I want pressing enter to activate the goButton just as clicking it would, which I believe is what TextBox.AcceptsReturn = false is for.
I don't see anything that seems to fit the bill for "tell me what button on the form is the one that we will activate".
What am I missing?

A Form has a default button, but a specific control does not (out of the box anyway).
In your scenario, I would probably handle invoking the goButton.Click event by monitoring the keys pressed waiting for the Enter key to be pressed.

The easiest way is to set the forms "Accept Button" to the button control you want. This can be done in the designer.

I know this is an Old Question, but for someone who might to to lazy or just a beginner ,
handler might look like too much work ( though it isn't really )
But there is an easier work around for this,
you can make a panel for each of them ( 1 Textbox and 1 Button for Example ) , and set the Defaultbutton for Each panel as you need.
I used this for my site, where I had several Ajax panel , and I Wanted Each to have their own search box on different subjects and work with Enter Button.

Looks like an old question, but I will provide my solution.
private void ChangeDefaultButton()
{
if (this.TextBox.Focused)
{
this.AcceptButton = button;
}
else
{
this.AcceptButton = button1;
}
}
And then add this method to the Focus Events of the text boxes. Like...
private void TextBox_Enter(object sender, EventArgs e)
{
ChangeDefaultButton();
}
And
private void TextBox_Leave(object sender, EventArgs e)
{
ChangeDefaultButton();
}

Related

How to hide control when click outside it?

I have a WindowForm and some controls on it.
My point is that when I click button "?" on top-right of the datagridview, it will show a picture box and when I click outside the pictureBox, it must invisible.
My MainForm
MyPictureBox
I have searched some topics on this site, but some dont work, some work partly. Like
this.
I also tried:
void pictureBox1_LostFocus(object sender, EventArgs e)
{
if (pictureBox1.Visible)
pictureBox1.Visible = false;
}
But when I click on button2, button3, ... The pictureBox wasn't invisible.
Any solution will be highly appreciated.
I think your pictureBox1 isn't losing focus, cause it never actually GOT focused. Set it to be focused after making it visible.
Oh, I have encountered this before...
I was making a Label that you could double click and it would allow you to edit the Label.Text, like a TextBox. However, I was having problems hooking into the events to know when the user had clicked off the Control and wished to stop editing. I tried Control.LostFocus, and Control.Leave, but nothing. I even got frustrated/desperate and tried some silly ones like Control.Invalidated.
What I ended up having to do was subscribe to the Click event of the Form/Container/Control behind it.
However, putting the responsibility of wiring up this event into the Form that wants to use it is poor design. What you can do, however is to make the constructor to Control class require a reference to the owner/parent/container as a parameter. That way, the requirements are not hidden, they must be satisfied before you can get a object instance, and the control can wired up to the Form.Click within itself, where that logic belongs.
private Form owner;
public EditLabel(Form Owner)
{
this.owner = Owner;
owner.Click += EndEditing;
}
Add this method in designer.cs:
pictureBoxEvent this.MouseLeave += new EventHandler(pictureBox_MouseLeave);
Add this code in cs file:
private void pictureBox_MouseLeave(object sender, EventArgs e)
{
pictureBox1.Visible = false;
}

How to check if button from user control was clicked?

It seems like a straight forward thing, but I can't figure it out.
How can I check if a certain button was clicked from user control?
my user control is uc_test and button name is btnTest .
I assume its some sort of event handler added to uc_test.btnTest ?
I'm working in WinForms.
It sounds like you've created a button but want to make it do something on click. I don't often use Winforms but from memory double-clicking on the control in the form should automatically create a btnTest_Click method. If not, just go to code and put in a method like:
protected void btnTest_Click(object sender, Eventargs e)
{
//do something
}
Then set in the properties of the button the OnClick event to btnTest_Click.

Fire event on textbox lose focus

I'm trying to call a method as soon as a TextBox on my screen gets 'un-focused' if that makes any sense? The user types in a username and as soon as that textbox loses focus I want to fire an event that checks if that username is indeed available.
Thanks!
There is a Control.Leave in C#, which I think is perfect for your purpose.
you can go to events of the textbox in visual studio, and find the Leave event.
The code generated will be like :
private void txtbox_Leave(object sender, EventArgs e)
{
//Check for available operation Code
}
Please explore the LostFocus event in a textbox. Hope it helps
select your object and to choose the panel of the properties and click on event.To unroll until LEAVE and double-click above.that will give you:
private void object_Leave( object sender, EventArgs e)
{
////put your code here
}
In WPF Try this:
TextBox.LostFocus
This will work:
Go to the "properties" of your textbox. You will see a yellow lightning bolt in the first line tab. There you will find all possible events that can be triggered. Search for "Leave" entry, double-click it. There you can put whatever you want.

ToolStripButton "Reset Appearance" / "Fire Leave Event"

I have a ToolStripButton that performs an action. The user clicks the button and the button is disabled to prevent the action being performed twice. After the action is complete the button is re-enabled. It all works perfectly...except:
Because the button is disabled it does not fire the "MouseLeave" event and as a result the appearance of the button is not updated. To be absolutely clear, when the mouse enters a ToolStripButton the button is highlighted in orange (by default) with a black box around it. This highlight is not being removed when I re-enable the button. The mouse cursor is, by this time, long gone from the control. Mousing over the button naturally fixes the button by redrawing it.
What I would like to do would be some method on the ToolStripButton that "resets" its appearance. Such a method may even exist on the ToolStrip, but despite searching I have been unable to find anything like this.
As an alternative I could fire the "Mouse Leave" event on the button directly. As far as I know there is no way to easily do this in C# .NET.
Any advice at this point in time would be most appreciated, naturally I don't want to tear up my application and replace the tool strip.
Update:
I reproduced your problem, trying figuring out!
I didn't get a better way other than reset the style in the click event
private void toolStripButton1_Click(object sender, EventArgs e)
{
toolStripButton1.BackColor = Color.FromKnownColor(KnownColor.Control);
toolStripButton1.Enabled = false;
}
private void toolStripButton1_MouseEnter(object sender, EventArgs e)
{
toolStripButton1.BackColor = Color.Red;
}
private void toolStripButton1_MouseLeave(object sender, EventArgs e)
{
toolStripButton1.BackColor = Color.FromKnownColor(KnownColor.Control);
}
Hope this helps!
Have you tried Control.Invalidate()?
from MSDN: Invalidates the entire surface of the control and causes the control to be redrawn.
I had the same problem. I "fixed" it by hiding and then showing back the ToolStripButton using the Visible property after the task was complete.
Before disabling ToolStrip or ToolStripItem:
private void RemoveHighlightFromToolStrip(ToolStrip toolStrip)
{
foreach (ToolStripItem item in toolStrip.Items)
{
if (item.Pressed || item.Selected)
{
item.Visible = false;
item.Visible = true;
}
}
}
also you can just hide and show entire ToolStrip, but this may affect other controls in your form (i.e. if you have some docked DataGridView it would be redrawn)

Focusing WebBrowser control in a C# application

I have a WebBrowser control hosted in a windows Form. The control is used to display hyperlinks which get created at runtime. These links point to some HTML pages and PDF documents.
The problem is that when the form hosting the browser control is loaded, the focus is on the form. When the TAB key is pressed, the focus does not shift to the first hyperlink. However, if I perform a mouse click on the control and then hit the TAB key, the tab focus is now on the first hyper link. I tried using Select() on the WebBrowser control and then I called Focus(), but it doesn't solve the problem.
Any ideas on how to set the tab focus on the first hyperlink at load? Thanks.
Cheers,
Harish
I guess it might be because the focus is set before the page is fully loaded. Try this:
private void Go(string url)
{
webBrowser1.Navigate(url);
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Document.Body.Focus();
}
You could also automatically select the focus on the first link directly by getting the HtmlElement of that first link.
If the above doesn't work, you might want to check other parts of your code to see if anything else is capturing the focus. Try searching for Select, Focus and ActiveControl in your code.
Use form.ShowDialog(form) instead on form.Show(), then it will work !
where form is the running instance of your windows Form
This is my solution
private void txtAdres_KeyPress(object sender, KeyPressEventArgs e)
{
int licznik = 1;
if (e.KeyChar == (char)13)
{
string adres = txtAdres.Text;
webBrowser1.Navigate(adres);
licznik = 0;
}
if (licznik == 0)
{
webBrowser1.Focus();
}
}
In a normal scenario it should be sufficient for you to set the TabIndex of the WebBrowser control to zero. This way, when the form loads the control will be focused and pressing TAB will iterate through the links.
Note that you should also change the TabIndex of the other controls on the form.
If this does not solve your problem, you need to add more detail on the complexity of the form hosting the control.

Categories

Resources