Tell me a method by which in C # it would be possible to generate a GridView Event, for example, FocusedRowChanged, like the on ..... methods for WinForms?
If you want to create an Event like FocusedRowChanged
You can use this codes
//using DevExpress.XtraGrid.Views.Base;
//the All is ok
public event FocusedRowChangedEventHandler MyFocusedRowChanged;
//Or
public event EventHandler<FocusedRowChangedEventArgs> MyFocusedRowChanged2;
If you want to handle the FocusedRowChanged event,
You can use this codes
private void YourForm_Load(object sender, EventArgs e)
{
gridView.FocusedRowChanged += GridView_FocusedRowChanged;
this.MyFocusedRowChanged += GridView_FocusedRowChanged;
this.MyFocusedRowChanged2 += GridView_FocusedRowChanged;
}
private void GridView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
{
//your code
throw new NotImplementedException();
}
If you want to raise FocusedRowChanged,
You can use this codes
private void YourForm_Load(object sender, EventArgs e)
{
//gridView.FocusedRowChanged += GridView_FocusedRowChanged;
//this.MyFocusedRowChanged += GridView_FocusedRowChanged;
//this.MyFocusedRowChanged2 += GridView_FocusedRowChanged;
//invoke the handle method
GridView_FocusedRowChanged(gridView, new FocusedRowChangedEventArgs(-1, gridView.FocusedRowHandle));
//or change focused row to fire event
gridView.FocusedRowHandle++;
}
Which one do you mean?
Related
I am creating one button on a page dynamically. Now I want to use the button click event on that button.
How can I do this in C# ASP.NET?
Button button = new Button();
button.Click += (s,e) => { your code; };
//button.Click += new EventHandler(button_Click);
container.Controls.Add(button);
//protected void button_Click (object sender, EventArgs e) { }
The easier one for newbies:
Button button = new Button();
button.Click += new EventHandler(button_Click);
protected void button_Click (object sender, EventArgs e)
{
Button button = sender as Button;
// identify which button was clicked and perform necessary actions
}
Simply add the eventhandler to the button when creating it.
button.Click += new EventHandler(this.button_Click);
void button_Click(object sender, System.EventArgs e)
{
//your stuff...
}
It is much easier to do:
Button button = new Button();
button.Click += delegate
{
// Your code
};
You can create button in a simple way, such as:
Button button = new Button();
button.Click += new EventHandler(button_Click);
protected void button_Click (object sender, EventArgs e)
{
Button button = sender as Button;
// identify which button was clicked and perform necessary actions
}
But event probably will not fire, because the element/elements must be recreated at every postback or you will lose the event handler.
I tried this solution that verify that ViewState is already Generated and recreate elements at every postback,
for example, imagine you create your button on an event click:
protected void Button_Click(object sender, EventArgs e)
{
if (Convert.ToString(ViewState["Generated"]) != "true")
{
CreateDynamicElements();
}
}
on postback, for example on page load, you should do this:
protected void Page_Load(object sender, EventArgs e)
{
if (Convert.ToString(ViewState["Generated"]) == "true") {
CreateDynamicElements();
}
}
In CreateDynamicElements() you can put all the elements you need, such as your button.
This worked very well for me.
public void CreateDynamicElements(){
Button button = new Button();
button.Click += new EventHandler(button_Click);
}
Let's say you have 25 objects and want one process to handle any one objects click event. You could write 25 delegates or use a loop to handle the click event.
public form1()
{
foreach (Panel pl in Container.Components)
{
pl.Click += Panel_Click;
}
}
private void Panel_Click(object sender, EventArgs e)
{
// Process the panel clicks here
int index = Panels.FindIndex(a => a == sender);
...
}
I am new on C# so sorry if it look easy for some of you :)
I have this button click:
private void buttonSwitchCamera_Click(object sender, RoutedEventArgs e)
{
_blManager.SendTrackerCmd(TrackerCmdType.PrimaryAVT_ActiveSensor, (float)IR_Id.IR_1); // Switch to IR1
_blManager.SendTrackerCmd(TrackerCmdType.PrimaryAVT_ActiveSensor, (float)IR_Id.IR_2); // Switch to IR2
}
How can I switch between the two methods when clicking the button?
one click will be for method #1
second click will be for method #2
third click will be for method #1
etc..
You can do something like that:
bool executeMethodOne;
private void buttonSwitchCamera_Click(object sender, RoutedEventArgs e)
{
executeMethodOne = !executeMethodOne;
var IrId = executeMethodOne ? IR_Id.IR_1 : IR_Id.IR_2;
_blManager.SendTrackerCmd(TrackerCmdType.PrimaryAVT_ActiveSensor, (float)IrId);
}
Have a bool called executeMethodOne which you will invert everytime you click on the button. Depending on if it is true or false you can execute the first or the second method
First click is old:
private void OnButtonClickOdd(object sender, RoutedEventArgs e)
{
// Unsubscribe OnButtonClickOdd
button.Click -= OnButtonClickOdd;
// Subcribe to OnButtonClickEven
button.Click += OnButtonClickEven;
// Do your job here
}
private void OnButtonClickEven(object sender, RoutedEventArgs e)
{
// Unsubscribe OnButtonClickEven
button.Click -= OnButtonClickEven;
// Subcribe to OnButtonClickOdd
button.Click += OnButtonClickOdd;
// Do your job here
}
Other way: just use bool flag to know it odd or even click:
private bool odd = true;
private void buttonSwitchCamera_Click(object sender, RoutedEventArgs e)
{
if(old)
{
// Odd click job
}
else
{
// Even click job
}
old = !old;
}
Looking to refactor some application code. I have a GUI that has several of the same events that are reflected for different labels, textboxes, etc...
For example:
private void textBox1_Enter(object sender, EventArgs e)
{
textBox1.BackColor = Color.LightCyan;
}
and
private void textBox2_Enter(object sender, EventArgs e)
{
textBox2.BackColor = Color.LightCyan;
}
I just assign these methods to the event on the object properties in Visual Studio. Is there an efficient way to combine multiple event methods to clean up the code? Thanks!
Define a single event like:
private void textBox_Enter(object sender, EventArgs e)
{
TextBox textBox = sender as TextBox;
if (textBox != null)
textBox.BackColor = Color.LightCyan;
}
and then assign that event to all your TextBox Enter event like:
textBox1.Enter += textBox_Enter; //Same event handler
textBox2.Enter += textBox_Enter; //Same event handler
If both event handlers do the same exact logic, as you probably have listed here, just create 1 event handler and assign it to both components.
private void TextBox_Enter(object sender, EventArgs e)
{
TextBox textBox = sender as TextBox;
if (textBox != null) {
textBox.BackColor = Color.LightCyan;
}
}
To elaborate slightly you can take this approach even when you want to perform different actions depending on the control that fired the event.
private void textBox_Enter(object sender, EventArgs e)
{
//Check if the sender is textBox1
if(ReferenceEquals(sender, textBox1))
{
//Perform action on textBox1
}
//Check if the sender is textBox2
if(ReferenceEquals(sender, textBox2))
{
//Perform action on textBox2
}
}
I managed to create textboxes that are created at runtime on every button click. I want the text from textboxes to disappear when I click on them. I know how to create events, but not for dynamically created textboxes.
How would I wire this up to my new textboxes?
private void buttonClear_Text(object sender, EventArgs e)
{
myText.Text = "";
}
This is how you assign the event handler for every newly created textbox :
myTextbox.Click += new System.EventHandler(buttonClear_Text);
The sender parameter here should be the textbox which sent the even you will need to cast it to the correct control type and set the text as normal
if (sender is TextBox) {
((TextBox)sender).Text = "";
}
To register the event to the textbox
myText.Click += new System.EventHandler(buttonClear_Text);
Your question isn't very clear, but I suspect you just need to use the sender parameter:
private void buttonClear_Text(object sender, EventArgs e)
{
TextBox textBox = (TextBox) sender;
textBox.Text = "";
}
(The name of the method isn't particularly clear here, but as the question isn't either, I wasn't able to suggest a better one...)
when you create the textBoxObj:
RoutedEventHandler reh = new RoutedEventHandler(buttonClear_Text);
textBoxObj.Click += reh;
and I think (not 100% sure) you have to change the listener to
private void buttonClear_Text(object sender, RoutedEventArgs e)
{
...
}
I guess the OP wants to clear all the text from the created textBoxes
private void buttonClear_Text(object sender, EventArgs e)
{
ClearSpace(this);
}
public static void ClearSpace(Control control)
{
foreach (var c in control.Controls.OfType<TextBox>())
{
(c).Clear();
if (c.HasChildren)
ClearSpace(c);
}
}
This should do the job :
private void button2_Click(object sender, EventArgs e)
{
Button btn = new Button();
this.Controls.Add(btn);
// adtionally set the button location & position
//register the click handler
btn.Click += OnClickOfDynamicButton;
}
private void OnClickOfDynamicButton(object sender, EventArgs eventArgs)
{
//since you dont not need to know which of the created button is click, you just need the text to be ""
((Button) sender).Text = string.Empty;
}
I have a button which slides out a menu using a storyboard by calling begin() on it like so
private void ShareBtn_Click(object sender, RoutedEventArgs e)
{
SlideIn.Begin();
}
On the grid which then slides out there are buttons.
Each button then slides the grid back and when that storyboard completes the action for the button then runs so like so,
private void PictureBtn_Click(object sender, RoutedEventArgs e)
{
CertificateDisplay.SaveAsPicture();
}
private void FacebookBtn_Click(object sender, RoutedEventArgs e)
{
App.facebookSuccess = false;
NavigationService.Navigate(new Uri("/FBLogin.xaml", UriKind.Relative));
}
private void SMSBtn_Click(object sender, RoutedEventArgs e)
{
SlideOut.Begin();
SlideOut.Completed += delegate(object s, EventArgs se) { SlideOut_Completed(s, se, "Email"); };
}
private void EmailBtn_Click(object sender, RoutedEventArgs e)
{
SlideOut.Begin();
SlideOut.Completed += delegate(object s, EventArgs se) { SlideOut_Completed(s, se, "Email"); };
}
void SlideOut_Completed(object sender, EventArgs e, String shareType)
{
switch (shareType)
{
case "Email":
...
default:
break;
}
}
The flaw I encountered if that I cannot remove the anonymous functions from the event stack.
I've managed to solve it by making shareType a common variable for all of the above functions and not using a anonymous delegate and then removing the "named" functions from the event stack when OnNavigatedFrom is called.
Is there a way to do this by still using those delegates because it looks neater?
One option is to remove it within the handler itself:
EventHandler handler = null;
handler = delegate(object s, EventArgs se) {
SlideOut_Completed(s, se, "Email");
SlideOut.Completed -= handler;
};
SlideOut.Completed += handler;
SlideOut.Begin();
Why assign the Completed event handler EmailBtn_Click at each click? Do it in the form constructor or in the form load event.