views:

309

answers:

2

I am making a multi-lingual (computer languages) notepad in WinForms. I have a menu strip with a toolstripmenuitem called "Languages" (Like the file, edit, view, ect.). When you click on "Languages", there are several checkable menu items. I want to do this: when an item is clicked, it appears checked; and when the user clicks it again, it appears unchecked. How do I incorperate a compiler per language like java, c, c++, ect.

A: 

you should have a settings file which store the path / location of each compiler. when user select a Languages, you should then get the path / location of the selected Language's matching compiler.

imnd
+1  A: 

You don't want a check, it doesn't make sense to have more than one language checked. You need a radio button. You can get one by overriding the renderer for the menu strip. You'll also need to handle the CheckedChanged event of the menu items so only one can be selected. This code will do the trick:

  public partial class Form1 : Form {
    private ToolStripMenuItem[] languages;
    private bool mBusy;

    public Form1() {
      InitializeComponent();
      languages = new ToolStripMenuItem[] { javaToolStripMenuItem, cSharpToolStripMenuItem, pythonToolStripMenuItem };
      foreach (var language in languages) {
        language.CheckOnClick = true;
        language.CheckedChanged += LanguageMenuItem_CheckedChanged;
      }
      menuStrip1.Renderer = new MyRenderer(languages);
    }

    void LanguageMenuItem_CheckedChanged(object sender, EventArgs e) {
      if (mBusy) return;
      mBusy = true;
      ToolStripMenuItem item = sender as ToolStripMenuItem;
      foreach (var language in languages) language.Checked = language == item;
      mBusy = false;
    }

    private class MyRenderer : ToolStripProfessionalRenderer {
      private ToolStripMenuItem[] languages;
      public MyRenderer(ToolStripMenuItem[] languages) { this.languages = languages; }

      protected override void OnRenderItemCheck(ToolStripItemImageRenderEventArgs e) {
        ToolStripMenuItem item = e.Item as ToolStripMenuItem;
        if (item != null && languages.Contains(item))
          RadioButtonRenderer.DrawRadioButton(e.Graphics, e.ImageRectangle.Location,
            System.Windows.Forms.VisualStyles.RadioButtonState.CheckedNormal);
        else
          base.OnRenderItemCheck(e);
      }
    }
  }
Hans Passant
Awesome thanks. Now could someone post the code on how to access the compilers for each programming language.
Mohit Deshpande