I need TextBox that supports double click, but while trying to code it, i stuck with a problem that Silverlight textbox doesn't raise left-button mouse events if i click inside of text area. So what are possible solutions here? I'm using Silverlight 4.
You should create a custom textbox control that inherits from the default TextBox and override the OnMouseLeftButtonUp method, like this:
public class MyTextBox : TextBox
{
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonUp(e);
// Do your stuff here
}
}
If you need to handle the double click, it would be easier to switch to Silverlight 5 that introduced the concept of a click count. Here is a tutorial for handling the double click: http://www.silverlighthostingnews.com/index.php/archives/440
Basically you will just have to do this:
if(e.ClickCount == 2) {...}
EDIT: here is how to do in Silverlight 4:
Create the custom textbox that inherits the default TextBox class and override the mouse events like so:
-
public class DoubleClickTextBox : TextBox
{
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
// Do nothing
}
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
// Do nothing
}
}
Add the DoubleClickExtender class in your project class.
In your code-behind, attach to the double click event:
-
DoubleClickExtender dce = new DoubleClickExtender(textBox, 300)
dce.DoubleClick += new MyEventHandler(dce_DoubleClick);
You may be able to cheat with this one, although I haven't tried this myself
Change the template of the textbox to have a transparent overlay that
is IsHitTestVisible = true
Set the double click event on the overlay.
If you detect a single
click on the overlay, do nothing and let it bubble through
If you detect a double click, set the event handled of the
mousebuttoneventargs to true and perform your double click action
Please note that double click on Silverlight4 isn't natively supported, so you'll have to come up some way to simulate that.
Related
I have 50 UserControls that I add to a flowlayoutPanel dynamically.
I need to set focus to a user control but it doesn't work.
I have been searching a lot but can't find any example that I understand.
The only example that I find is this
Setting Focus to a .NET UserControl...?
I tried to use userCtrl.Focus(); but it didn't work.
As I have been reading the usercontrol doesn't like to have focus.
Addition: Now that I understand more of the Control class, I
understand that if you derive from Control you should not subscribe
to its events, but use the On.. functions, like OnEnter. I've
changed my answer accordingly
To Activate any Control, including a UserControl use Control.Select().
If you do this for a TextBox, you'll see that Select ensures that it gets the input focus.
I guess you want to do something with the selected UserControl (the Control that has the focus), for instance, you want to change its appearance, or select any of the controls on it. To do this, your UserControl class has to subscribe to the events Control.Enter and Control.Leave
I have created a UserControl with a CheckBox that is automatically checked whenever the UserControl is selected (has the input focus):
Addition: If you derive from a Control, don't subscribe to events Enter and Leave. Instead override the functions that raise these events: OnEnter / OnLeave.
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
protected override void OnEnter(EventArgs e)
{
this.checkBox1.Checked = true;
base.OnEnter(e); // this will raise the Enter event
}
protected override void OnLeave(EventArgs e)
{
this.checkBox1.Checked = false;
base.OnLeave(e); // this will raise the Leave event
}
}
I have a form with a button, and an event handler that is called when the button is clicked:
private void OnButton1Clicked(object sender, EventArgs e)
{
this.userControl1.Select();
}
Now whenever the button is clicked I see that the user control gets the focus because the check box is checked and whenever I click elsewhere the checkbox is unchecked.
You can set focus to a control by using the ActiveControl Property
this.ActiveControl = myUserControl;
Though you did not detail what did you mean it did not work, focusing has many aspects conventionally.
1. Explicit focusing
Calling Focus() method of a control is the same as setting ActiveControl of the container form. If CanFocus returns true (your control and all its parents are visible and enabled), it works; however, you will have no visual feedback, except some indirect hint, eg. the originally focused control (button or textbox) loses the focus.
To visualize the focused state you might want to use some custom paint:
protected override void OnPaintBackground(PaintEventArgs e)
{
e.Graphics.Clear(Focused ? SystemColors.Highlight : SystemColors.Control);
}
If you derive directly from Control instead of UserControl, override the following two methods to force a repaint on changing the focused state:
protected override void OnGotFocus(EventArgs e)
{
Invalidate();
base.OnGotFocus(e);
}
protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus(e);
Invalidate();
}
2. Focusing by the mouse
To receive focus by clicking the control add this line to the constructor:
SetStyle(ControlStyles.Selectable, true);
If you derive directly from Control instead of UserControl, override the OnMouseDown, too:
protected override void OnMouseDown(MouseEventArgs e)
{
if (!Focused)
Focus();
base.OnMouseDown(e);
}
3. Focusing by the keyboard
To receive focus by the TAB key just set the TabStop property to true and adjust the TabOrder property.
Example to focus on textBox1:
textBox1.Select();
you can try tab index of the user control. If you set its tab index to 1 it will be focused once the program start.
I have Windows Form TestForm, and in my Form I have several labels that are only used to display some text.
I need to display a MessageBox.Show anytime the Form is clicked. So I have an event handler for the click, which looks like this:
private void TestForm_Click(object sender, EventArgs e)
{
MessageBox.Show("The form has been clicked");
}
Unfortunately, the click event doesn't fire when I click over a label in the Form. Is there a way to fix this, besides consuming the click event for the labels?
Thanks.
To use the same click event for all labels:
In the properties for each label, go to the Events (lightning bolt tab).
You will see (probably near the top) a label for Click, click the dropdown for this event, and you will be shown a list of handlers that you could use for that label.
Here's the Properties > Events > Click handler (bottom right):
Because all of your labels are of the same type, and produce the same EventArgs, you are able to use the same handler for all of them.
Then, when you are adding more Labels, just choose the event handler from the Click event dropdown:
Hope this helps!
To flesh out LarsTech's comment, I have used something like this in the past when I was having problems with labels overlapping each other and lack of true transparency in WinForms. What I did was make the labels invisible on the Form, then iterate through them in the Form's paint event, pull the information out of them and then use Graphics.DrawString to draw the text. That way you you will still be able see them in design mode.
This is a quick example of what I mean.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
foreach (var temp in this.Controls)
{
if (temp is Label) //Verify that control is a label
{
Label lbl =(Label)temp;
e.Graphics.DrawString(lbl.Text, lbl.Font, new SolidBrush(lbl.ForeColor), new Rectangle(lbl.Location, lbl.Size));
}
}
}
private void Form1_Click(object sender, EventArgs e)
{
MessageBox.Show("The Form has been clicked");
}
}
I'm trying to write a Fahrenheit to Celsius converter but nothing happens when I click the button
and I have no idea why. Please help me.
It's written in C# and I have a form with a label, a button, a textbox for input, and a multiline textbox for output.
namespace _14._6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click( object sender, EventArgs e)
{
int farenhait;
int celsius;
farenhait = Convert.ToInt32(textbox2.Text);
celsius = Convert.ToInt32(5.0 / 9.0 * (farenhait - 32));
textBox1.Text = farenhait + "grader på farenhait skalan motsvarar \n " + celsius + "grader celsius";
}
}
}
Assuming the statement:
but nothing happends when i klick the button...
is literally true (i.e. when placing a breakpoint on the first line in the handler button1_Click it doesn't hit the breakpoint) then something happened to the handler. So, go to the Form Designer and double-click the button again to hookup the handler.
Is the event wired up?
Click on the button in the designer and look in the properties window (bottom-right underneath your file list), then click the events button (looks like a little lightning bolt) and scroll down until you find Click, then type button1_Click in there if it isn't already in there, that will wire the event up.
Alternatively, you can add it manually in the form's constructor:
public Form1()
{
InitializeComponent();
this.button1.Click += new EventHandler(button1_Click);
}
What exactly does the debugger tell you is wrong?
Not to deviate to far from your original question, I would suggest to use a technique called abstraction. An example of this would be to create a separate class, which will take parameters that will accomplish a task.
public class TemperatureConverter
{
public void Convertion(int fahrenheit, int celsius)
{
// Input your conversion syntax here.
}
}
Now with that particular class can handle the conversion, on your form within your button you can provide the necessary Error Handling then you can actually call it with those values.
private void button1_click(object sender, EventArgs e)
{
// Error Handling
TemperatureConverter tc = new TemperatureConverter();
tc.Conversion(textbox1.Text, textbox2.Text);
}
In this example which is quite poor, you'll have to cast the string to a integer otherwise it will error. But this is an example to help better adhere to Object-Orientation.
Your other issue: "nothing happens when I click the button". This leads me to believe the EventHandler has been broken. Follow these steps:
Go into Designer Mode
Select your Button
Open Properties
You'll see a button with almost a lightning bolt, click it
Locate your on click, and ensure the EventHandler is wired correctly.
This is a designer approach to the coded approach that Sean provided.
Hopefully this helps.
I need to differentiate between a single click and a double click but I need to do this during the click event. I need this because if there's a single click I want to call Function A and if there's a double click I want to call Function B. But Function A must be called only in case of a single click.
How may I achieve this using standard Winforms in C# (or VB)?
click and double click can be handled seperately
click -> http://msdn.microsoft.com/en-US/library/system.windows.forms.control.click(v=vs.80).aspx
double click -> http://msdn.microsoft.com/en-US/library/system.windows.forms.button.doubleclick(v=vs.80).aspx
You will need to use a timer wait "some amount of time" after the click event to determine if the click was a single click. You can use the SystemInformation.DoubleClickTime property to determine the maximum interval between clicks that the system will consider sequential clicks a double click. You would probably want to add a little padding to this.
In your click handler, increment a click counter and start the timer. In the double click handler, set a flag to denote a double click occurred. In the timer handler, check to see if the double click event was raised. If not, it was a single click.
Something like this:
private bool _doubleClicked = false;
private Timer _clickTrackingTimer =
new Timer(SystemInformation.DoubleClickTimer + 100);
private void ClickHandler(object sender, EventArgs e)
{
_clickTrackingTimer.Start();
}
private void DoubleClickHandler(object sender, EventArgs e)
{
_doubleClicked = true;
}
private void TimerTickHandler(object sender, EventArgs e)
{
_clickTrackingTimer.Stop();
if (!_doubleClicked)
{
// single click!
}
_doubleClicked = false;
}
Another options is to create a custom control which derives from Button, and then call the SetStyles() method, which is a protected method) in the constructor and set the ControlStyles flag
class DoubleClickButton : Button
{
public DoubleClickButton() : base()
{
SetStyle(ControlStyles.StandardDoubleClick, true);
}
}
Gretings
I need to have a custom control for my application. Basically its an expression editing GUI. You have, say, expression:
If variable_x is greater than variable_y
And you can click on "greater than" and change it to other comparator (like, equal to or less than).
The control thus must look like a label, but when you click it, it must show a dropdown (like combobox does) that has a listview inside (or maybe some other control) so that user can choose something. In a sense, i need a combobox without the box itself, replaced by something else (in this case, a label).
I know how to make custom controls, i understand i must somehow DropDown on mouse click or enter keypress, and hook events so that when whatever i dropped has closed, the choice is made, and also somehow track if user clicked elsewhere so i can close this dropdowned control. But i dont know if this is easy to do (some built-in method exists) or i have to do it all myself? Dont want to redevelop the wheel....
Please tell me if there are easy ways to do this.
Thanks!
You can extend the ComboBox control to update the DropDownStyle on Enter and LostFocus events.
public partial class MyComboBox : ComboBox
{
public MyComboBox()
{
InitializeComponent();
this.Dock = DockStyle.Fill;
this.SelectionChangeCommitted += this.OnComboBoxSelectionChangeCommitted;
this.Enter += this.OnControlEnter;
this.LostFocus += this.OnComboBoxLostFocus;
}
private void OnControlEnter(object sender, EventArgs e)
{
this.DropDownStyle = ComboBoxStyle.DropDownList;
}
private void OnComboBoxLostFocus(object sender, EventArgs e)
{
this.DropDownStyle = ComboBoxStyle.Simple;
}
private void OnComboBoxSelectionChangeCommitted(object sender, EventArgs e)
{
// Notify to update other controls that depend on the combo box value
}
}