How do add a right click menu

How do i add a right click menu?

Page 1 of 1

4 Replies - 13448 Views - Last Post: 01 November 2009 - 01:22 PM Rate Topic: -----

#1 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 548
  • Joined: 12-June 08

How do add a right click menu

Post icon  Posted 01 November 2009 - 10:02 AM

So, I have made a Notepad program in C#, and I was wondering how do add a RightClick handler in the program, so that when the user right clicks it shows the normal stuff for a right click in a notepad.

So, what I need is how to handle right clicks and then how to handle the buttons that are in the right click menu.

Thank you in advance,

here is my code if anyone needs it to help.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TextPad
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void newToolStripMenuItem_Click(object sender, EventArgs e)
		{
			richTextBox.Clear();
		}

		private void openToolStripMenuItem_Click_1(object sender, EventArgs e)
		{
			openFileDialog.Title = "Open File";
			openFileDialog.Filter = "Text Document (.txt)|*.txt|Rich Text Document (.rtf)|*.rtf|Word Document (.doc)|*.doc|Batch File (.bat)|*.bat|All Files (*.*)|*.*";

			if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				richTextBox.LoadFile(openFileDialog.FileName);
			}
		}

		private void saveToolStripMenuItem_Click_1(object sender, EventArgs e)
		{
			saveFileDialog.Filter = "Text Document (.txt)|*.txt|Rich Text Document (.rtf)|*.rtf|Word Document (.doc)|*.doc|Batch File (.bat)|*.bat|All Files (*.*)|*.*";
			saveFileDialog.Title = "Save File";
			saveFileDialog.OverwritePrompt = true;

			if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				richTextBox.SaveFile(saveFileDialog.FileName, RichTextBoxStreamType.RichText);
			}
		}

		private void exitToolStripMenuItem_Click(object sender, EventArgs e)
		{
			Close();
		}

		private void undoToolStripMenuItem_Click(object sender, EventArgs e)
		{
			richTextBox.Undo();
		}

		private void redoToolStripMenuItem_Click(object sender, EventArgs e)
		{
			richTextBox.Redo();
		}

		private void cutToolStripMenuItem_Click(object sender, EventArgs e)
		{
			richTextBox.Cut();
		}

		private void copyToolStripMenuItem_Click(object sender, EventArgs e)
		{
			richTextBox.Copy();
		}

		private void paseToolStripMenuItem_Click(object sender, EventArgs e)
		{
			richTextBox.Paste();
		}

		private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
		{
			richTextBox.SelectAll();
		}

		private void clearToolStripMenuItem_Click(object sender, EventArgs e)
		{
			richTextBox.Clear();
		}

		private void fontToolStripMenuItem_Click(object sender, EventArgs e)
		{
			FontDialog Font = new FontDialog();
			Font.Font = richTextBox.SelectionFont;
			if (Font.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				richTextBox.SelectionFont = (Font)Font.Font.Clone();
			}
		}

		private void colorToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ColorDialog Color = new ColorDialog();
			Color.Color = richTextBox.SelectionColor;
			if (Color.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				richTextBox.SelectionColor = Color.Color;
			}
		}
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: How do add a right click menu

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: How do add a right click menu

Posted 01 November 2009 - 10:28 AM

You want to add a ContextMenuStrip to the control. It works just like a normal menu strip but shows up on right clicks on the control.
Was This Post Helpful? 0
  • +
  • -

#3 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 548
  • Joined: 12-June 08

Re: How do add a right click menu

Posted 01 November 2009 - 10:41 AM

Ok, well I added a ContextMenuStrip, then added my stuff, and coded the events for them, and changed the properties of the main form to have default ContextMenuStrip as the one that I just made, but even when I do all that and open up my project, it doesn't have a right click menu.

Here is my new code with the buttons from the context menu coded in.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TextPad
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void newToolStripMenuItem_Click(object sender, EventArgs e)
		{
			richTextBox.Clear();
		}

		private void openToolStripMenuItem_Click_1(object sender, EventArgs e)
		{
			openFileDialog.Title = "Open File";
			openFileDialog.Filter = "Text Document (.txt)|*.txt|Rich Text Document (.rtf)|*.rtf|Word Document (.doc)|*.doc|Batch File (.bat)|*.bat|All Files (*.*)|*.*";

			if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				richTextBox.LoadFile(openFileDialog.FileName);
			}
		}

		private void saveToolStripMenuItem_Click_1(object sender, EventArgs e)
		{
			saveFileDialog.Filter = "Text Document (.txt)|*.txt|Rich Text Document (.rtf)|*.rtf|Word Document (.doc)|*.doc|Batch File (.bat)|*.bat|All Files (*.*)|*.*";
			saveFileDialog.Title = "Save File";
			saveFileDialog.OverwritePrompt = true;

			if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				richTextBox.SaveFile(saveFileDialog.FileName, RichTextBoxStreamType.RichText);
			}
		}

		private void exitToolStripMenuItem_Click(object sender, EventArgs e)
		{
			Close();
		}

		private void undoToolStripMenuItem_Click(object sender, EventArgs e)
		{
			richTextBox.Undo();
		}

		private void redoToolStripMenuItem_Click(object sender, EventArgs e)
		{
			richTextBox.Redo();
		}

		private void cutToolStripMenuItem_Click(object sender, EventArgs e)
		{
			richTextBox.Cut();
		}

		private void copyToolStripMenuItem_Click(object sender, EventArgs e)
		{
			richTextBox.Copy();
		}

		private void paseToolStripMenuItem_Click(object sender, EventArgs e)
		{
			richTextBox.Paste();
		}

		private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
		{
			richTextBox.SelectAll();
		}

		private void clearToolStripMenuItem_Click(object sender, EventArgs e)
		{
			richTextBox.Clear();
		}

		private void fontToolStripMenuItem_Click(object sender, EventArgs e)
		{
			FontDialog Font = new FontDialog();
			Font.Font = richTextBox.SelectionFont;
			if (Font.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				richTextBox.SelectionFont = (Font)Font.Font.Clone();
			}
		}

		private void colorToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ColorDialog Color = new ColorDialog();
			Color.Color = richTextBox.SelectionColor;
			if (Color.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				richTextBox.SelectionColor = Color.Color;
			}
		}

		// This is where the buttons for the context menu strip start.

		private void undoToolStripMenuItem1_Click(object sender, EventArgs e)
		{
			richTextBox.Undo();
		}

		private void cutToolStripMenuItem1_Click(object sender, EventArgs e)
		{
			richTextBox.Cut();
		}

		private void copyToolStripMenuItem1_Click(object sender, EventArgs e)
		{
			richTextBox.Copy();
		}

		private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
		{
			richTextBox.Paste();
		}

		private void selectAllToolStripMenuItem1_Click(object sender, EventArgs e)
		{
			richTextBox.SelectAll();
		}
	}
}


thanks in advance for help on this problem.

This post has been edited by C++ Programmer: 01 November 2009 - 10:55 AM

Was This Post Helpful? 0
  • +
  • -

#4 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: How do add a right click menu

Posted 01 November 2009 - 12:50 PM

The problem is that you assigned the ContextMenuStrip control to the form. You need to assign it to your RichTextBox control instead for it to be displayed on right clicking the control.
Was This Post Helpful? 0
  • +
  • -

#5 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 548
  • Joined: 12-June 08

Re: How do add a right click menu

Posted 01 November 2009 - 01:22 PM

Thanks, now it works.

This post has been edited by C++ Programmer: 01 November 2009 - 01:23 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1