Solved

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

Posted on 2011-03-16
4
569 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
RLDC Reporting in Visual studio 11 31
how to remove duplicate code from my project 5 37
VS 2017 18 57
C# Linq Select From List 3 18
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

820 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