Derive from base class and override functions - c#

Here's a portion of the standard Label control in WinForms:
public class Label : Control
{
protected override void OnTextChanged(EventArgs e)
{
...
}
}
I'd like to override the OnTextChanged event but I'm not sure of the best way.
Should I derive a subclass from Label class and then override the function like this?
public class Class1 : Label
{
protected override void OnTextChanged(EventArgs e)
{
MessageBox.Show("S");
}
}
If so, how and where should I add this class?
If not, how can I override functions which are defined inside a control?

This is the way you can override the method for control. As you have done is absolutely right but detailed implementation is here.
This is the form part
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class TestForm : Form
{
MyLabel newLable;
public TestForm()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
newLable = new MyLabel();
newLable.Height = 30;
newLable.Width = 40;
newLable.Text = "hello";
this.Controls.Add(newLable);
}
}
}
You can use MyLabel from toolbox also.
And MyLabel class is
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public class MyLabel:Label
{
public MyLabel()
{
}
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
MessageBox.Show("Label Clicked");
}
}
}

Related

How can I make refresh a Textbox from another form? C#

I'm trying to update a Textbox from another form with this code:
private void Button2_Click(object sender, EventArgs e)
{
Variables.revenu += Variables.LAIR * 30;
Cartel_Form.Textbox_Revenu.Text = Variables.revenu.ToString();
}
But I get this error:
An object reference is required for the non-static field, method or property 'Cartel.Form.Textbox_Revenu'
Here is the content of the Textbox in the first form:
Textbox_Revenu.Text = Variables.revenu.ToString();
In the same form I can refresh/modify the Textbox but not in another form. The textbox modifier is set on public.
Here is an example using events where the child form contains the events while the main form listens for these events. Both forms have one TextBox and one NumericUpDown control. Or as #jimi mentioned use bindings.
Main form
using System;
using System.Windows.Forms;
namespace PassingDataBetweenFormsSimple
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void ShowChildButton_Click(object sender, EventArgs e)
{
ChildForm childForm = new ChildForm();
childForm.PassingData += ChildFormOnPassingData;
childForm.PassingNumber += ChildFormOnPassingNumber;
try
{
childForm.ShowDialog();
}
finally
{
childForm.Dispose();
}
}
private void ChildFormOnPassingNumber(int value)
{
numericUpDown1.Value = numericUpDown1.Value + value;
}
private void ChildFormOnPassingData(string text)
{
FirstNameTextBox.Text = string.IsNullOrWhiteSpace(text) ?
"(empty)" :
text;
}
}
}
Child form
using System;
using System.Windows.Forms;
namespace PassingDataBetweenFormsSimple
{
public partial class ChildForm : Form
{
public delegate void OnPassingText(string text);
public event OnPassingText PassingData;
public delegate void OnPassingNumber(int value);
public event OnPassingNumber PassingNumber;
public ChildForm()
{
InitializeComponent();
}
private void PassDataButton_Click(object sender, EventArgs e)
{
PassingData?.Invoke(FirstNameTextBox.Text);
PassingNumber?.Invoke((int)numericUpDown1.Value);
}
}
}

WPF Programming, How to move an event to another class (outside)

I have the problem that I want to add an event from XAML directly to another class.
The standard class, which is used, is the MainWindow.
In my situation I want to define, which class should be used for the event.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Closing_Event(object sender, System.ComponentModel.CancelEventArgs e)
{
}
}
public class differentClass
{
public differentClass()
{
}
private void Window_Closing_Event(object sender, System.ComponentModel.CancelEventArgs e)
{
}
}
Maybe someone can help me, how I can use the event from the second class without any code in the MainWindow.
There is a Behavior class for this purpose. You will need to add the reference to the System.Windows.Interactivity in the project: How to add System.Windows.Interactivity to project?
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
public class CustomWindowHandlerBehavior: Behavior<Window>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.Closing+= Window_Closing_Event;
}
protected override void OnDetaching()
{
AssociatedObject.Closing-= Window_Closing_Event;
base.OnDetaching();
}
private void Window_Closing_Event(object sender, System.ComponentModel.CancelEventArgs e)
{
//...
}
}
using this behavior in XAML:
<Window
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity">
<i:Interaction.Behaviors>
<local:CustomWindowHandlerBehaviour />
</i:Interaction.Behaviors>
<Window/>

C# ListBox.Items.Add in other class

I've just recently started with C# and I know this has been kind of asked here before but all with complex examples which i could not really grasp yet. So here is my simple example.
I'm trying to add a string to a ListBox from an added class.
It compiles fine but the "TEST" doesn't get added to the ListBox
Form1.cs
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ListBoxAdder myAdder = new ListBoxAdder();
myAdder.Testing();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
ListBoxAdder.cs
namespace WindowsFormsApplication1
{
class ListBoxAdder : Form1
{
public void Testing()
{
listBox1.Items.Add("TEST");
}
}
}
I assume nothing is happening because of creating another instance of "ListBoxAdder"? But I couldn't make it static because I wouldn't make Testing() static or I would have no access to listBox1.
namespace WindowsFormsApplication1
{
class ListBoxAdder
{
ListBox listBox;
public ListBoxAdder (ListBox listBox)
{
this.listBox = listBox;
}
public void Testing()
{
listBox.Items.Add("TEST");
}
}
}
And:
private void button1_Click(object sender, EventArgs e)
{
ListBoxAdder myAdder = new ListBoxAdder(listBox1); // pass ListBox instance here
myAdder.Testing();
}

Add OnLoad method to usercontrol

I have several controls that inherit BaseUserControl. BaseUserControl inherits System.Web.UI.UserControl.
I want to override OnLoad event like this:
public partial class MyControl1 : BaseUserControl
{
protected override void OnLoad(EventArgs e)
{
this.Value = myCustomService.GetBoolValue();
///More code here...
base.OnLoad(e);
}
}
This works great, the only problem is that I have to copy this piece of code across 3 controls, which I do not like. (I do not have access to Base class, since it is inherited by 100s of controls.)
So, my result currently looks like this:
public partial class MyControl2 : BaseUserControl
{
protected override void OnLoad(EventArgs e)
{
this.Value = myCustomService.GetBoolValue();
///More code here...
base.OnLoad(e);
}
}
public partial class MyControl3 : BaseUserControl
{
protected override void OnLoad(EventArgs e)
{
this.Value = myCustomService.GetBoolValue();
///More code here...
base.OnLoad(e);
}
}
What are the good ways to refactor this? One way is to extract
this.Value = myCustomService.GetBoolValue();
///More code here...
to a separate method, but I was wondering if there's a way that will allow us specify override event only once?
You could create an extra base class for those controls sharing functionality and make this class inherits from BaseUserControl
// Change YourBaseControl by a meaningful name
public partial class YourBaseControl : BaseUserControl
{
protected override void OnLoad(EventArgs e)
{
this.Value = myCustomService.GetBoolValue();
///More code here...
base.OnLoad(e);
}
}
public partial class MyControl2 : YourBaseControl
{
...
}
public partial class MyControl3 : YourBaseControl
{
...
}

Refresh or add ascx control from another ascx control

I am working on an asp .net project and i have a main ascx control which is divided by two RadSpliters. On load of the main.ascx the app is loading two other controls, the control1.ascx and control2.ascx. In the control1 i have a treeview and on selected node of the treeview i want to reload the control2.ascx. Is there a way to do this. Below i am pasting the code that i am using to do that but is not working. Any help or suggestions please?
public partial class Control1: System.Web.UI.UserControl
{
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
BrowseProject b = new BrowseProject();
b.load();
}
public partial class MainControl : System.Web.UI.UserControl
{
public void load()
{
Control codeEditor = Page.LoadControl("Control2.ascx");
PlaceHolder4.Controls.Clear();
PlaceHolder4.ID = "PlaceHolder4";
PlaceHolder4.Controls.Add(codeEditor);
}
public interface IReload{
public void reload();
}
public partial class Control2: System.Web.UI.UserControl, IReload
{
}
public partial class Control1: System.Web.UI.UserControl
{
IReload _r;
public IReload setReload
{
set { _r = value; }
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
BrowseProject b = new BrowseProject();
b.load();
if(_r != null){
_r.reload();
}
}
public partial class MainControl : System.Web.UI.UserControl
{
public void load()
{
Control codeEditor = Page.LoadControl("Control2.ascx");
PlaceHolder4.Controls.Clear();
PlaceHolder4.ID = "PlaceHolder4";
PlaceHolder4.Controls.Add(codeEditor);
c.setReload(codeEditor);
}

Categories

Resources