Solved

c# how to put this code into a function...

Posted on 2011-03-16
4
566 Views
Last Modified: 2013-12-17
hi all, i want to put the below code into a function but dont know how to

the cmd will change everytime as will the state.EditValue

is what im after possible?

Thanks
ToggleFontBoldCommand cmd = new ToggleFontBoldCommand(CurrentEditor);
            ICommandUIState state = cmd.CreateDefaultCommandUIState();
            cmd.UpdateUIState(state);
            if (!state.Enabled && CurrentEditor.ReadOnly) state.Enabled = true;
            state.EditValue = FontStyle.Bold;
            cmd.ForceExecute(state);

Open in new window

0
Comment
Question by:awilderbeast
  • 2
4 Comments
 
LVL 11

Expert Comment

by:Kusala Wijayasena
ID: 35150964
How about this:

public void ToggleFontBoldStyle(IRichEditControl currentEditor)
{
           ToggleFontBoldCommand cmd = new ToggleFontBoldCommand(currentEditor);
            ICommandUIState state = cmd.CreateDefaultCommandUIState();
            cmd.UpdateUIState(state);
            if (!state.Enabled && currentEditor.ReadOnly) state.Enabled = true;
            state.EditValue = FontStyle.Bold;
            cmd.ForceExecute(state);
}

Open in new window


-Kusala


0
 
LVL 1

Author Comment

by:awilderbeast
ID: 35154824
the toggle command will change each time i want to use it though

what im tryign to do is turn 8 lines of code into 1


so i do:-

(type i dont know) action = functioname(togglefontitaliccommand, fonstyle.italic);
action.forceexecute();

something like the above if you understand what im trying to to?
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 35157789
What about this?

Calling code:
ToggleFontBoldCommand cmd = new ToggleFontBoldCommand(CurrentEditor);
ForceExecute(cmd, FontStyle.Bold);

Open in new window


Method: (it assumes that CurrentEditor is accessible in the method, otherwise it needs to be passed as a parameter)
public void ForceExecute(RichEditCommand cmd, object editValue)
{
            ICommandUIState state = cmd.CreateDefaultCommandUIState();
            cmd.UpdateUIState(state);
            if (!state.Enabled && CurrentEditor.ReadOnly) state.Enabled = true;
            state.EditValue = editValue;
            cmd.ForceExecute(state);
}

Open in new window

0
 
LVL 1

Author Closing Comment

by:awilderbeast
ID: 35157817
brilliant, thankyou!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now