Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-03-16
4
Medium Priority
?
577 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 2000 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

916 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