How to call multiple methods from another form C# - c#

I have multiple forms with the same method called "UpdateTheme" which changes the back colour of the form. I want to be able to call all of these methods from another form.
I tried to make a base form with the "UpdateTheme" method then have all other forms inherit from the base form, But I didnt know how/ if it was possible to then call every instance of the derived forms methods from a separate "Settings" form.
public abstract class CustomForm : Form
{
public void UpdateTheme(string theme)
{
if (theme == "dark")
{
this.BackColor = Color.Black;
}
else if (theme == "light")
{
this.BackColor = Color.White;
}
}
}
In the settings form I would have something like
public void btnSetThemeToDark_Click(object sender, EventArgs e)
{
foreach (instance of derived form)
{
derivedForm.UpdateTheme("dark");
}
}
Whats the best way to do this?

You could create a singleton called StyleManager that contains the global style properties. This singleton has an event called style changed that can be handled by all forms, or a base form. So all of your forms get the information from one source.
StyleManager
public class StyleManager
{
#region singleton
public static StyleManager Instance { get; } = new StyleManager();
private StyleManager()
{
}
#endregion
#region events
public event EventHandler StyleChanged;
private void OnStyleChanged()
{
this.StyleChanged?.Invoke(this, EventArgs.Empty);
}
#endregion
#region properties
public Color BackColor { get; set; }
#endregion
#region methods
public void UpdateBackColor(Color color)
{
this.BackColor = color;
this.OnStyleChanged();
}
#endregion
}
and use it in your forms like this:
public Form()
{
this.InitializeComponent();
//Attach to the event
StyleManager.Instance.StyleChanged += this.StyleChanged;
}
//Handle event
private void StyleChanged(object sender, EventArgs eventArgs)
{
this.BackColor = StyleManager.Instance.BackColor;
}
//set backcolor of all forms
StyleManager.Instance.UpdateBackColor(Color.Yellow);

Assuming this forms are MdiChildren of the form, you would do it this way:
foreach (var form in this.MdiChildren)
{
var castedForm = form as CustomForm;
if (myObjRef != null)
{
castedForm.UpdateTheme("dark");
}
}
This functionality works beyond themes to any common method of the child forms.

I don't think this is the best way to do this. But, you can archive what you want by using this code.
CustomForm mainFrm = (CustomForm)Application.OpenForms["YouCustomFormNameHere"];
mainFrm.UpdateTheme("dark");
Replace 'YouCustomFormNameHere' with your CustomForm form name.

Related

How do i specify that i want to change a label on a particular form? [duplicate]

I have a winform called Form1 and a textbox called textBox1
In the Form1 I can set the text by typing:
textBox1.text = "change text";
Now I have created another class. How do I call textBox1 in this class?
so I want to change the text for textBox1 in this class.
How can I access the Form1 from this new class?
I would recommend that you don't. Do you really want to have a class that is dependent on how the text editing is implemented in the form, or do you want a mechanism allowing you to get and set the text?
I would suggest the latter. So in your form, create a property that wraps the Text property of the TextBox control in question:
public string FirstName
{
get { return firstNameTextBox.Text; }
set { firstNameTextBox.Text = value; }
}
Next, create some mechanism through which you class can get a reference to the form (through the contructor for instance). Then that class can use the property to access and modify the text:
class SomeClass
{
private readonly YourFormClass form;
public SomeClass(YourFormClass form)
{
this.form = form;
}
private void SomeMethodDoingStuffWithText()
{
string firstName = form.FirstName;
form.FirstName = "some name";
}
}
An even better solution would be to define the possible interactions in an interface, and let that interface be the contract between your form and the other class. That way the class is completely decoupled from the form, and can use anyting implementing the interface (which opens the door for far easier testing):
interface IYourForm
{
string FirstName { get; set; }
}
In your form class:
class YourFormClass : Form, IYourForm
{
// lots of other code here
public string FirstName
{
get { return firstNameTextBox.Text; }
set { firstNameTextBox.Text = value; }
}
}
...and the class:
class SomeClass
{
private readonly IYourForm form;
public SomeClass(IYourForm form)
{
this.form = form;
}
// and so on
}
I was also facing the same problem where I was not able to appendText to richTextBox of Form class. So I created a method called update, where I used to pass a message from Class1.
class: Form1.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
_Form1 = this;
}
public static Form1 _Form1;
public void update(string message)
{
textBox1.Text = message;
}
private void Form1_Load(object sender, EventArgs e)
{
Class1 sample = new Class1();
}
}
class: Class1.cs
public class Class1
{
public Class1()
{
Form1._Form1.update("change text");
}
}
You can change the access modifier for the generated field in Form1.Designer.cs from private to public. Change this
private System.Windows.Forms.TextBox textBox1;
by this
public System.Windows.Forms.TextBox textBox1;
You can now handle it using a reference of the form Form1.textBox1.
Visual Studio will not overwrite this if you make any changes to the control properties, unless you delete it and recreate it.
You can also chane it from the UI if you are not confortable with editing code directly. Look for the Modifiers property:
public partial class Form1 : Form
{
public static Form1 gui;
public Form1()
{
InitializeComponent();
gui = this;
}
public void WriteLog(string log)
{
this.Invoke(new Action(() => { txtbx_test1.Text += log; }));
}
}
public class SomeAnotherClass
{
public void Test()
{
Form1.gui.WriteLog("1234");
}
}
I like this solution.
You will need to have some access to the Form's Instance to access its Controls collection and thereby changing the Text Box's Text.
One of ways could be that You can have a Your Form's Instance Available as Public or More better Create a new Constructor For your Second Form and have it receive the Form1's instance during initialization.
Define a property of the form like, then use this in other places it would be available with the form instance
public string SetText
{
get { return textBox1.Text; }
set { textBox1.Text = value; }
}
If your other class inherits Form1 and if your textBox1 is declared public, then you can access that text box from your other class by simply calling:
otherClassInstance.textBox1.Text = "hello world";
Use, a global variable or property for assigning the value to the textbox, give the value for the variable in another class and assign it to the textbox.text in form class.
I Found an easy way to do this,I've tested it,it works Properly.
First I created a Windows Project,on the form I Inserted a TextBox and I named it textBox1
then I inserted a button named button1,then add a class named class1.
in the class1 I created a TextBox:
class class1
{
public static TextBox txt1=new TextBox(); //a global textbox to interfece with form1
public static void Hello()
{
txt1.Text="Hello";
}
}
Now in your Form Do this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
class1.txt1=textBox1;
class1.Hello();
}
}
in the button1_Click I coppied the object textBox1 into txt1,so now txt1 has the properties
of textBox1 and u can change textBox1 text in another form or class.
Form1 form = new Form1();
form.textBox1.Text = "test";
I tried the examples above, but none worked as described. However, I have a solution that is combined from some of the examples:
public static Form1 gui;
public Form1()
{
InitializeComponent();
gui = this;
comms = new Comms();
}
public Comms()
{
Form1.gui.tsStatus.Text = "test";
Form1.gui.addLogLine("Hello from Comms class");
Form1.gui.bn_connect.Text = "Comms";
}
This works so long as you're not using threads. Using threads would require more code and was not needed for my task.
I used this method for updating a label but you could easily change it to a textbox:
Class:
public Class1
{
public Form_Class formToOutput;
public Class1(Form_Class f){
formToOutput = f;
}
// Then call this method and pass whatever string
private void Write(string s)
{
formToOutput.MethodToBeCalledByClass(s);
}
}
Form methods that will do the updating:
public Form_Class{
// Methods that will do the updating
public void MethodToBeCalledByClass(string messageToSend)
{
if (InvokeRequired) {
Invoke(new OutputDelegate(UpdateText),messageToSend);
}
}
public delegate void OutputDelegate(string messageToSend);
public void UpdateText(string messageToSend)
{
label1.Text = messageToSend;
}
}
Finally
Just pass the form through the constructor:
Class1 c = new Class1(this);
Form frm1 = new Form1();
frm1.Controls.Find("control_name",true)[0].Text = "I changed this from another form";
// Take the Active form to a form variable.
Form F1 = myForm1.ActiveForm;
//Findout the Conntrol and Change the properties
F1.Controls.Find("Textbox1", true).ElementAt(0).Text= "Whatever you want to write";
What about
Form1.textBox1.text = "change text";
note:
1. you have to "include" Form1 to your second form source file by
using Form1;
textBox1 should be public

Creating Smart Tag for Form (not other control) using c#

As the Form of System.Windows.Forms inherits from Control, I was wondering if there is a way to create a Custom Form and its Designer with some options (shortcuts) to create a title or somthings like that.
I tried this, but nothings happend, the Form I calles ManagedForm
[Designer(typeof(ManagedFormDesigner))]
public class ManagedForm : Form{
//code here
}
[PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
public class ManagedFormDesigner : ControlDesigner {
private DesignerActionListCollection actionLists;
public override DesignerActionListCollection ActionLists {
get {
if (actionLists == null) {
actionLists = new DesignerActionListCollection();
actionLists.Add(new ManagedFormDesignerActionList(this.Component));
}
return actionLists;
}
}
}
public class ManagedFormDesignerActionList : DesignerActionList {
private ManagedForm managedForm = null;
private DesignerActionUIService designerActionUISvc = null;
public ManagedFormDesignerActionList(IComponent component) : base(component) {
this.managedForm = component as ManagedForm;
this.designerActionUISvc =
GetService(typeof(DesignerActionUIService))
as DesignerActionUIService;
}
public override DesignerActionItemCollection GetSortedActionItems() {
DesignerActionItemCollection items = new DesignerActionItemCollection();
items.Add(new DesignerActionMethodItem(this, "CreateTitle", "Create Title", "Appearence", true));
return items;
}
public void CreateTitle() {
Panel pTitulo = new Panel();
pTitulo.Size= new Size(100,25);
pTitulo.Dock = DockStyle.Top;
(this.Component as ManagedForm).Controls.Add(pTitulo);
}
}
Action list are show when you click on the little arrow on the control inside a form (or on a component on the bottom of the designer if the object is a component).
Other things you can do is to manage verbs.
Verbs Handling is implemented on the ControlDesigner class (ManagedFormDesigner in your case).
You can see verbs clicking right mouse button or on the bottom of the properties (i.e. TabControl ha 2 verbs, add tab and remove tab).
You can implement verbs adding to ControlDesigner (or ComponentDesigner) class something like this
private DesignerVerbCollection _verbs;
public override DesignerVerbCollection Verbs
{
get
{
if (_verbs == null)
{
_verbs = new DesignerVerbCollection();
_verbs.Add(new DesignerVerb("Create Title", new EventHandler(MyCreateTitleHandler)));
}
return _verbs;
}
}
private void MyCreateTitleHandler(object sender, EventArgs e)
{
// Do here something but take care to show things via IUIService service
IUIService uiService = GetService(typeof(IUIService)) as IUIService;
}

How to enable shortcuts in child form

I have wrote a frame application, it's a windows form app as parent form.When it starts, it will find dlls in /modules and load them as extensions. And when I click the menuItem in the parent form, the specific dll will work.If the dll is a Form app, it will show.But when i try to use shortcuts(only build-ins,eg : CTRL-C...) in the childForm ,the hotkeys does not work. Anyone kindly tell me why and how can i fix the issue? Here's my code:
//parent.exe--ModuleEntrance.cs
public abstract class ModuleEntrance {
public abstract string[] GetMenuNames();
public abstract string[] GetMenuItemNames();
public abstract EventHandler[] GetEventHandlers();
}
//parent.exe--ParentForm.cs
public partial class MDIParent : Form {
public MDIParent() { //CTOR
InitializeComponent();
ModuleEntrance oneEntrance;
string oneMenuName, oneMenuItemName;
ToolStripMenuItem theMenu, theMenuItem;
for(){ //iterate dlls in /modules, if it implement ModuleEntrance, load it.
//And 1)load menu&menuItem.
//2) connect handler to menuItem.click through
//<code:theMenuItem.Click += new EventHandler(oneEntrance.GetEventHandlers()[i]);>
}
}
//--------------
//child.dll-- EntranceImp.cs //implement AC
public class EntranceImp : ModuleEntrance {
public override string[] GetMenuNames() {
return new string[] { "MENU"};
}
public override string[] GetMenuItemNames() {
return new string[] { "OpenChildForm"};
}
public override EventHandler[] GetEventHandlers() {
return new EventHandler[]{
(EventHandler)delegate(object sender, EventArgs e) { //Anonymous method
childForm form = new childForm();
//find MDIparent and connect them
ToolStripMenuItem mi = (ToolStripMenuItem)sender;
form.MdiParent = (Form)(mi.OwnerItem.Owner.Parent); //It works!
form.Show();
}
};
}
}
//child.dll--childForm.dll
//...
The child form has to notify the parent form about the key-down events somehow.
A way to do this is to have the child form expose key-down events that the parent form can listen to. Remember to remove the parent event handlers every time that you change child form, or you'll end up with a memory leak since objects are not garbage collected until you release all references to them, including event handlers.
class Parent
{
KeyEventHandler KeyDownHandler;
public Parent()
{
KeyDownHandler = new KeyEventHandler(form_TextBoxKeyDown);
}
void SetChildForm(Child form)
{
form.TextBoxKeyDown += KeyDownHandler;
}
void RemoveChildForm(Child form)
{
form.TextBoxKeyDown -= KeyDownHandler;
}
void form_TextBoxKeyDown(object sender, KeyEventArgs e)
{
if (e.Control)
{
switch (e.KeyCode)
{
case Keys.C:
break;
case Keys.X:
break;
case Keys.V:
break;
}
}
}
}
class Child
{
TextBox txtBox;
public event KeyEventHandler TextBoxKeyDown;
internal Child()
{
txtBox.KeyDown += new KeyEventHandler(txtBox_KeyDown);
}
void txtBox_KeyDown(object sender, KeyEventArgs e)
{
if (TextBoxKeyDown != null)
TextBoxKeyDown(sender, e);
}
}

C# Accessing controls from an outside class without "public"

I know this has been asked before but I believe my situation is a bit different -- or I don't understand the answers given. I have spent about 4 hours working on this solidly and finally realized, I just don't know what to do.
I have 2 Forms (Form1, Settings) and a class I created called Themes.
I have get/set properties that currently work but are all within Form1 and I would like to move as much code related to themeing as I can OUTSIDE of Form1 and into Themes.cs.
Changing Theme: To change the theme, the user opens up the Settings form and selects a theme from the dropdown menu and presses the 'Set' button -- this all works, but now I want to move it into my own class and I can't get the code to compile.
Here is example code that works before moving -- note that this is only 2 different controls I want to modify but there are about 30 total. I am abridging the code:
Form 1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnSettings_Click(object sender, EventArgs e)
{
Settings frm = new Settings(this);
frm.Show();
}
private Color txtRSSURLBGProperty;
private Color txtRSSURLFGProperty;
public Color TxtRSSURLBGProperty
{
get { return txtRSSURLBGProperty; }
set { txtRSSURL.BackColor = value; }
}
public Color TxtRSSURLFGProperty
{
get { return txtRSSURLFGProperty; }
set { txtRSSURL.ForeColor = value; }
}
Settings Form:
public partial class Settings : Form
{
public Settings()
{
InitializeComponent();
}
private Form1 rssReaderMain = null;
public Settings(Form requestingForm)
{
rssReaderMain = requestingForm as Form1;
InitializeComponent();
}
private void button2_Click(object sender, EventArgs args)
{
// Appearence settings for DEFAULT THEME
if (cbThemeSelect.SelectedIndex == 1)
{
this.rssReaderMain.TxtRSSURLBGProperty = Color.DarkSeaGreen;
this.rssReaderMain.TxtRSSURLFGProperty = Color.White;
[......about 25 more of these....]
}
The theme class is currently empty. Again, the goal is to move as much code into the themes class (specifically the get/set statements if at all possible!) and hopefully just use a method similar to this within the Settings form once the proper drowndown item is selected: SetTheme(Default);
I hope someone can help, and I hope I explained it right! I have been racking my brain and I need to have this done fairly soon! Much thanks in advance as I'm sure everyone says. I have teamviewer or logmein if someone wants to remote in -- that is just as easy.
I can also send my project as a zip if needed.
Thanks so much,
Kurt
Modified code for review:
Form1 form:
public partial class Form1 : ThemeableForm
{
public Form1()
{
InitializeComponent();
}
ThemeableForm form:
internal abstract class ThemeableForm : Form
{
private Color rssLabelBGProperty;
private Color rssLabelFGProperty;
public Color RssLabelBGProperty
{
get { return rssLabelBGProperty; }
set { lRSS.BackColor = value; }
}
public Color RssLabelFGProperty
{
get { return rssLabelFGProperty; }
set { lRSS.ForeColor = value; }
}
Settings form:
public Settings(ThemeableForm requestingForm)
{
rssReaderMain = requestingForm as ThemeableForm;
InitializeComponent();
}
private ThemeableForm rssReaderMain = null;
private void button2_Click(object sender, EventArgs args)
{
// Appearence settings for DEFAULT THEME
if (cbThemeSelect.SelectedIndex == 1)
{
this.rssReaderMain.LRSSBGProperty = Color.DarkSeaGreen;
this.rssReaderMain.LRSSFGProperty = Color.White;
}
Now the all the controls in my get/set (lRSS in the example code above) error out with does not exist in the current context. I also get the warning:
Warning 1The designer could not be shown for this file because none of
the classes within it can be designed. The designer inspected the
following classes in the file:
Form1 --- The base class 'RSSReader_BKRF.ThemeableForm' could not be
loaded. Ensure the assembly has been referenced and that all projects
have been built. 0 0
Let the Themes class be composed largely of data that changes when a theme changes: Color, Fonts, etc.
Let the Settings form choose a theme and write it out as the Default Theme. If this is WinForms, then you can just have a static CurrentTheme property of the Themes class which returns the theme chosen on the Settings form.
Let the Form1 and any other forms delegate some of their properties to the current theme:
private Color BackgroundColor
{
get {return Themes.CurrentTheme.BackgroundColor;}
}
private Color TextColor
{
get {return Themes.CurrentTheme.TextColor;}
}
You might then want to push these delegated properties up to a base form class, to be shared by multiple forms.
Ok, I see you are trying to make the Settings form manipulate the values of properties on several (many?) other forms.
One solution is to have every other form inherit from the same abstract class, let's call it ThemeableForm. Now you can define ThemeableForm to have all the common properties.
A short example:
internal abstract class ThemeableForm : Form {
private Color txtRSSURLBGProperty;
private Color txtRSSURLFGProperty;
public Color TxtRSSURLBGProperty
{
get { return txtRSSURLBGProperty; }
set { txtRSSURL.BackColor = value; }
}
public Color TxtRSSURLFGProperty
{
get { return txtRSSURLFGProperty; }
set { txtRSSURL.ForeColor = value; }
}
}
And declare Form1:
public class Form1 : ThemeableForm {
// custom stuff for Form1, no need to write the common properties
}
I declared it as "internal" because you might want to control who/how THemeableForm is inherited. But, you could make it public too. And Settings can work with a ThemeableForm:
public Settings(ThemeableForm requestingForm)
{
rssReaderMain = requestingForm as ThemeableForm;
InitializeComponent();
}
private ThemeableForm rssReaderMain = null;
private void button2_Click(object sender, EventArgs args) {
// Appearence settings for DEFAULT THEME
if (cbThemeSelect.SelectedIndex == 1)
{
this.rssReaderMain.TxtRSSURLBGProperty = Color.DarkSeaGreen;
this.rssReaderMain.TxtRSSURLFGProperty = Color.White;
[......about 25 more of these....]
}
}
So you don't need to copy any of the Settings code for each and every other form type.

How to Enable/Disable MenuItem and ToolButton together

I'm a newbie in C# bu I'm experienced Delphi developer.
In Delphi I can use same code for MenuItem and ToolButton using TAction.OnExecute event and I can disable/enable MenuItem and ToolButton together using TAction.OnUpdate event.
Is there a similar way to do this in C# without using external libraries? Or more - How C# developers share code between different controls?
Ok, may be I write my question in wrong way. I want to know not witch property to use (I know about Enabled property) but I want to know on witch event I should attach to if I want to enable/disable more than one control. In delphi TAction.OnUpdate event ocurs when Application is idle - is there similar event in C#?
Try the a modification of the command pattern:
public abstract class ToolStripItemCommand
{
private bool enabled = true;
private bool visible = true;
private readonly List<ToolStripItem> controls;
protected ToolStripItemCommand()
{
controls = new List<ToolStripItem>();
}
public void RegisterControl(ToolStripItem item, string eventName)
{
item.Click += delegate { Execute(); };
controls.Add(item);
}
public bool Enabled
{
get { return enabled; }
set
{
enabled = value;
foreach (ToolStripItem item in controls)
item.Enabled = value;
}
}
public bool Visible
{
get { return visible; }
set
{
visible = value;
foreach (ToolStripItem item in controls)
item.Visible = value;
}
}
protected abstract void Execute();
}
Your implementations of this command can be stateful in order to support your view's state. This also enables the ability to build "undo" into your form. Here's some toy code that consumes this:
private ToolStripItemCommand fooCommand;
private void wireUpCommands()
{
fooCommand = new HelloWorldCommand();
fooCommand.RegisterControl(fooToolStripMenuItem, "Click");
fooCommand.RegisterControl(fooToolStripButton, "Click");
}
private void toggleEnabledClicked(object sender, EventArgs e)
{
fooCommand.Enabled = !fooCommand.Enabled;
}
private void toggleVisibleClicked(object sender, EventArgs e)
{
fooCommand.Visible = !fooCommand.Visible;
}
HelloWorldCommand:
public class HelloWorldCommand : ToolStripItemCommand
{
#region Overrides of ControlCommand
protected override void Execute()
{
MessageBox.Show("Hello World");
}
#endregion
}
It's unfortunate that Control and ToolStripItem do not share a common interface since they both have "Enabled" and "Visible" properties. In order to support both types, you would have to composite a command for both, or use reflection. Both solutions infringe on the elegance afforded by simple inheritance.
You can enable or disable a control and all its children by setting its Enabled property.
You can hook the code for the MenuItem and the ToolButton in the same handler. For example:
menuItem.Click += HandleClick;
toolbarButton.Click += handleClick;
This way clicking both the MenuItem and the Button will execute the same code and provide the same functionality.

Categories

Resources