Functions for C# dynamic item [duplicate] - c#

This question already has answers here:
How can I create a dynamic button click event on a dynamic button?
(6 answers)
Closed 7 years ago.
I wonder whether it is possible to create functions for dynamic items (textboxes, buttons) like
private void Button_Click(object sender, EventArgs e)
private void TextBox_TextChanged(object sender, EventArgs e)
and other?
If yes, then how?
I know you can use DynamicalButton.Click(); but that doesn't help much.
EDIT
As example, you can make a function like Button_Click or TextBox_TextChanged, PictureBox_MouseOver easily when you make the objects with designer and they work properly. How to do that with dynamic objects?

Found the answer
DynamicalObject.Click += new EventHandler(DynamicalObject_Click);
protected void DynamicalObject_Click (object sender, EventArgs e)
{
what happens on the event
}
as example

Related

Call Paint event handler from Timer to update form controls [duplicate]

This question already has answers here:
How do I call paint event?
(8 answers)
Why did I get the compile error "Use of unassigned local variable"?
(10 answers)
Closed 1 year ago.
I am trying to create a method for a timer event that takes 3 arguments. I have had a look at similar questions and tried to implement the solutions shown but the solution do not work in my case
GraphDrawingTimer.Elapsed += new ElapsedEventHandler(GraphPainter);
GraphDrawingTimer.Interval = 350;
GraphDrawingTimer.Enabled = true;
Above is the timer initialization
static void GraphPainter(object sender, ElapsedEventArgs e)
{
//Show_Graph(c);
}
thats method that will be called once the timer fires.
I want to add a PaintEventArgs c extra argument to draw a graph. I was using
private void tabPage2_Paint(object sender, PaintEventArgs e)
method but buttons do not get refreshed on my tabpage, thats why I want to create my own graph drawing method that will refresh every 350 milli-second.
I have tried
GraphDrawingTimer.Elapsed += (object sender, ElapsedEventArgs e) => { GraphPainter(sender, e, c); };
GraphDrawingTimer.Interval = 350;
GraphDrawingTimer.Enabled = true;
.....
static void GraphPainter(object sender, ElapsedEventArgs e, PaintEventArgs c)
{
Show_Graph(c);
}
but that does not work. I get the error "The name 'c' does not exist in the current context".
I also tried
PaintEventArgs c;
GraphDrawingTimer.Elapsed += (object sender, ElapsedEventArgs e)=> { GraphPainter(sender, e, c); };
GraphDrawingTimer.Interval = 350;
GraphDrawingTimer.Enabled = true;
.....
static void GraphPainter(object sender, ElapsedEventArgs e, PaintEventArgs c)
{
Show_Graph(c);
}
I get the following error
Error CS0165 Use of unassigned local variable 'c'
Which indeed makes sense since I'm not assigning any value to it - so how to assign a value to that PaintEventArgs c to pass correct parameter of the Paint event? Or maybe there should be some other approach to invoke Paint event?
PaintEventArgs might not be valid that way. The best option is to use the .Invalidate() method on the controls you want to update from your timer and that will cause their paint events to be called.
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.invalidate?view=net-5.0
This might look something like:
GraphDrawingTimer.Elapsed +=
(object sender, ElapsedEventArgs e) => MyGraphControl.Invalidate();

How do I send a message from a different section of code from where the new instance of the WebSocket class was created in websocket-sharp C# [duplicate]

This question already has answers here:
How can I use a local variable in a method from another method?
(1 answer)
"X" does not exist in this context
(4 answers)
The name '...' does not exist in the current context
(5 answers)
Closed 2 years ago.
This is my first post on Stack Overflow, so please excuse me if my question isn't very clear. To add more context, I want to open a connection to a WebSocket when the connect button is clicked and for a message to be sent from the connection opened by the connect button when the sent button is clicked. The problem is that I can not access the ws variable that I have created in the connect button from the send message button. The code below may give you more of an idea of what I want to do:
private void buttonConnect_Click(object sender, EventArgs e)
{
var ws = new WebSocket(textBoxSocketUrl.Text);
ws.Connect();
}
private void buttonSendMessage_Click(object sender, EventArgs e)
{
ws.Send(textBoxMessage.Text);
}
Thanks.
This is expected, that you can not see ws variable, because it was created in another "context". In order to get it, ws should be part of your class. Try to change your code to something like this:
private WebSocket ws;
private void buttonConnect_Click(object sender, EventArgs e)
{
ws = new WebSocket(textBoxSocketUrl.Text);
ws.Connect();
}
private void buttonSendMessage_Click(object sender, EventArgs e)
{
ws.Send(textBoxMessage.Text);
}

How to get form going to "Background state" Event in c# [duplicate]

This question already has answers here:
How to detect when an application loses focus?
(2 answers)
Closed 6 years ago.
We have form_load event in c# forms. I want to do some job when my form1 will go in background and form2 will come to foreground. I have wasted my time in searching that, but could not found any help in this regard.
Form.Activated Event
DOC
Occurs when the form is activated in code or by the user.
Form.Deactivate Event
DOC
Occurs when the form loses focus and is no longer the active form.
UPDATE
to start and stop the timer:
class Form1: Form{
void Form_Load(object sender, EventArgs arg)
{
this.Activated += form_Activate;
this.Deactivate += form_Deactivate;
}
void form_Activate(object sender, EventArgs arg){
timer.Start();
}
void form_Deactivate(object sender, EventArgs arg){
timer.Stop();
}
}

Action when Form is closed c#

I'm in College and this is my first (major) project.
I'm trying to perform an action when a form is closed. I don't seem to be getting the terminology right when searching online, or the answer given doesn't match what I want to do.
At the moment i'm declaring a Class and displaying the from -
private void createuser_Click(object sender, EventArgs e)
{
User_Modification mod = new User_Modification("Create", "Create");
mod.ShowDialog();
}
What I want to do is this -
WHEN mod IS CLOSED {
// Do stuff
}
You're using ShowDialog, so the code following it is not executed until after the dialog box is closed. mod.ShowDialog(); doStuff(); will work pretty well.
You need to create a handler to capture the FormClosed event:
In your constructor do:
this.FormClosed += Form_Closed;
Then in the body of your form, add this method.
private void Form_Closed(object sender, FormClosedEventArgs e)
{
// Do stuff
}
You should attach handler to FormClosed event:
private void createuser_Click(object sender, EventArgs e)
{
User_Modification mod = new User_Modification("Create", "Create");
mod.FormClosed += new FormClosedEventHandler(FormClosed);
mod.ShowDialog();
}
void FormClosed(object sender, FormClosedEventArgs e)
{
MessageBox.Show("Closed");
}
if you're using WinForms you can override OnFormClosing event:
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
// your code...
}
You'll want to take a look at two events:
Form.FormClosing : https://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing(v=vs.110).aspx
Form.FormClosed : https://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosed%28v=vs.110%29.aspx
First one will allow you to perform actions prior to the form being closed completely, such as canceling the closing procedure. The second one is what you would use if you want to perform actions after the form is closed (perhaps to clean up resources, as an example).
So, as an example, let's say that you want to perform an action when the form is in fact closed:
// Somewhere in your code where you create the form object.
form.FormClosed += Form_FormClosed;
// Somewhere else in your code.
private void Form_FormClosed(Object sender, FormClosedEventArgs e)
{
MessageBox.Show("Form closed");
}

C# WPF Disable the exit/close button [duplicate]

This question already has answers here:
How to hide close button in WPF window?
(23 answers)
Closed 9 years ago.
Is it possible to disable the close button in a WPF form?
How can I disable the close button?
I have been searching around and found the solution below. But that works only in Windows Form!
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}
in wpf this event called Closing :
public Window4()
{
InitializeComponent();
this.Closing += new System.ComponentModel.CancelEventHandler(Window4_Closing);
}
void Window4_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
}
You need to implement a windows hook to accomplish that. See this MSDN post for details.

Categories