.NET Resize Event: Get old size? - c#

I've added a resize event to one of my widgets, which looks like this:
void glControl_Resize(object sender, EventArgs e) {
Is there a way I can get the old size of the widget (before resizing)? Maybe I can cast e to something that will give me more info? Or should I just save it during that event?

Yes, just tracking the old size in a class field is the simple solution. For example:
Size mOldSize;
private void glControl_Resize(object sender, EventArgs e) {
if (mOldSize != Size.Empty && mOldSize != glControl.Size) {
// do something...
}
mOldSize = glControl.Size;
}

By convention you should add an OnResizing event, which fires just when it is about to change but hasn't changed, and then you fire the OnResize after it has been resized. You would get the old value from your EventArg in the OnResizing event.
Edit:
Are you creating your own event or firing one of an included control?
If you are doing your own event, you can derive from EventArg and make something like ResizeEventArg that include the size of the thing you want.
I would use the ResizeEventArg for both the Resize and OnResizing events, and still follow what I said earlier.
Or if you know which type of control it is, you could cast the Object sender into the type and then read the property.

Related

Repeat same steps on every textChanged event

I have in my project 5 text boxes.
Every TextBox should accept only digits.
For that I created a function which takes not prepared text and returns the proper one.
Now I'm wondering if there is any simpler way to perform this action on every TextBox, on every TextChanged event without repeating almost same code?
private void TextGoldPack_TextChanged(object sender, EventArgs e)
{
(sender as TextBox).Text = Only_digits((sender as TextBox).Text);
}
private void TextGoldTake_TextChanged(object sender, EventArgs e)
{
//repeat here and on every _TextChanged event
}
If I'm understanding you correctly, just because it's named TextGoldTake_TextChanged, doesn't mean that's the only textbox that can use that code. On the events tab, you can set the TextChanged function for all you textboxes to lead to that function. If it helps, rename it something that doesn't sound textbox-specific such as TextChanged.
Change all the TextBoxes to refer this method upon TextChanged.
Use the sender property to get the actual caller TextBox.

Method for Focus Change

Is there a method which initiates on focus change and can be overridden?
My goal is for the program to fetch closest data automatically from database to input fields whenever user changes his focus/presses enter or tab when on corresponding field. I'm still looking for a way to do this when user selects an item by mouse.
I'm aware that this could be implemented on mouse click but I refuse to believe that there is not a general method for focus change.
What about something like this:
foreach(Control ctrl in this.Controls)
{
ctrl.Enter += new EventHandler(Focus_Changed); // Your method to fire
}
Iterate through all controls and add a enter-event. Bind this handler to your method.
Edit:
Just in case you are wondering why "Enter" and not "LostFocus" or something like that: From my knowledge not every control got focus-events. As I've seen so far "Enter" is presented for all. Maybe there are exceptions. Should be checked out...
You could use Control.Enter event and Control.Leave event for that purpose.
See on MSDN Control.Enter and
Control.Leave.
textBox1.Enter += textBox1_Enter;
textBox1.Leave += textBox1_Leave;
private void textBox1_Enter(object sender, System.EventArgs e)
{
// the control got focus
}
private void textBox1_Leave(object sender, System.EventArgs e)
{
// the control lost focus
}

C# button click EventArgs e information

When I click on a button in e.g. a WinForms application, what information is passed to the EventArgs e of the event method? I'm just wondering because I'm using the as keyword to "convert" e to a mouse event in order to get the coordinates of the point where the button was clicked.
EDIT:
In the following example I can convert the variable e to an object of the type MouseEventArgs, and I want to know to what other types of event arguments e can be converted.
private void someEvent(object sender, EventArgs e)
{
int xCoord = (e as MouseEventArgs).X;
}
This depends on the event, The underlying windows messages for most winforms events have no such concept, so where ever the EventArgs were created will determine the type and the information it contains. It could be something from the framework or you can just make up your own class derived from EventArgs.
After .Net4.5 it doesn't even have to derive from EventArgs
You should use System.Windows.Forms.Cursor.Position: "A Point that represents the cursor's position in screen coordinates."
There are 2 parameters: a sender, and an EventArgs. The sender is the object that initialized the event. The EventArgs contains additional information about the event.
From MSDN
// This example uses the Parent property and the Find method of Control to set
// properties on the parent control of a Button and its Form. The example assumes
// that a Button control named button1 is located within a GroupBox control. The
// example also assumes that the Click event of the Button control is connected to
// the event handler method defined in the example.
private void button1_Click(object sender, System.EventArgs e)
{
// Get the control the Button control is located in. In this case a GroupBox.
Control control = button1.Parent;
// Set the text and backcolor of the parent control.
control.Text = "My Groupbox";
control.BackColor = Color.Blue;
// Get the form that the Button control is contained within.
Form myForm = button1.FindForm();
// Set the text and color of the form containing the Button.
myForm.Text = "The Form of My Control";
myForm.BackColor = Color.Red;
}
I'm using the as keyword to "convert" e to a mouse event
Don't do it. Why?
Because if someone uses keyboard (TABs + Enter) or Button.PerformClick call to trigger your button, the conversion will fail and your:
MouseEventArgs mouseArgs = e as MouseEventArgs ;
if (mouseArgs.MouseButton == MouseButton.Left) ...
will lead to NullReferenceException.
You can't guarantee that EventArgs e in OnClick event will be MouseEventArgs. It is just the most common scenario, but not the only one possible.
P.S.: As it has been already pointed by #terribleProgrammer you could use Cursor.Position static property to get the current cursor position in more independent and robust way.
But if the cursor information makes sense only if the button has been triggered by mouse, then you will have just to be careful and handle possible conversion failure:
MouseEventArgs mouseArgs = e as MouseEventArgs ;
if (mouseArgs == null)
{
Logger.Log("mouseArgs is null");
return;
}
if (mouseArgs.MouseButton == MouseButton.Left) ...
EDIT:
There are three main ways to raise the event and three corresponding eventArgs classes:
Using mouse - MouseEventArgs
Using keyboard - KeyEventArgs
Using PerformClick method - I am not sure, but it will probably be just plain EventArgs(to verify just raise it in such a way and take a look at e.GetType()).
Whether there are any other scenarios that will raise it, I don't know. MSDN is of no use for such requests.
P.S.1.: And I want to repeat again - do not assume that e parameter will be of some specific type. Windows Forms API even does not guarantee that e will contain some concrete useful EventArgs-derived class.

Pass name of button to click event

I am generating a number of buttons with similar or identical content and was hoping to use the names they are given to differentiate them. As the program is creating the buttons dynamically I can't create a separate event for them all and instead need to be able to grab the name to be able to know which button triggered the event.
Is there a way to pass through the name of a button to the click event it initiates? the sender object seems to contain the content but not the name.
It is for a event like this:
private void Button_Click(object sender, RoutedEventArgs e)
{
//getname of button
Canvas.Children.Remove(//name of button\\)
}
Far as I know, WPF does not even assign anything to the Name property automatically - that's just for developers to assign so we can reference the control.
However, you should be able to just pass in the sender to the remove method since it accepts a UIElement argument which Button is derived from.
Canvas.Children.Remove((Button)sender);
I'm not familiar with WPF. If this answer appears to be a junk, I'll delete it.
In ASP.Net, we can cast to a button to get the sender's information. Something like this -
private void Button_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
button.Name
}
Perhaps getting at the control's name will work for you
private void button1_Click(object sender, RoutedEventArgs e)
{
Control control = (Control)sender;
Canvas.Children.Remove(control.Name);
}

How to identify column in ColumnFilterChanged (devexpress) event

In this event, I want to identify for which column is this called. I basically want to modify the filterInfo of that column to append % at starting i.e. finally ti becomes {[COLUMNNAME] like "%SearchTerm%"} instead of "SearchTerm%"
here sender is the gridview which i anyways know, and e is basic EventArgs with no extra info.
private void gridView2_ColumnFilterChanged(object sender, EventArgs e)
{
}
An old post here suggests looking into the RowFilter property.
This may also be worth a look.

Categories