Click here to Skip to main content
15,914,642 members
Home / Discussions / C#
   

C#

 
QuestionCLR Pin
Anonymous15-Sep-05 23:37
Anonymous15-Sep-05 23:37 
AnswerRe: CLR Pin
Guffa15-Sep-05 23:57
Guffa15-Sep-05 23:57 
QuestionJoin video on-the-fly Pin
karnalta15-Sep-05 23:36
karnalta15-Sep-05 23:36 
QuestionHow to use < sign in comments? Pin
Dr Reedo15-Sep-05 23:09
Dr Reedo15-Sep-05 23:09 
AnswerRe: How to use < sign in comments? Pin
g00fyman15-Sep-05 23:17
g00fyman15-Sep-05 23:17 
GeneralRe: How to use &lt; sign in comments? Pin
Dr Reedo16-Sep-05 1:16
Dr Reedo16-Sep-05 1:16 
GeneralRe: How to use &lt; sign in comments? Pin
g00fyman16-Sep-05 1:17
g00fyman16-Sep-05 1:17 
QuestionProblems with Array being readonly (HELP) Pin
Yannielsen15-Sep-05 22:28
Yannielsen15-Sep-05 22:28 
Below i have posted the code for my little project, my problem is located at the line looking like this

"tempArray[iCounter++] = btnArray[iButtonIndex];"

and i'm getting this error:

Property or indexer 'ButtonArray.ButtonArray.this[int]' cannot be assigned to -- it is read only

What i'm trying to do is this, once i have removed an object from btnArray, i want to copy the remaining stuff of btnArray to tempArray in order to re-arrange the elements. This should be done through the CopyArray().

However i can't figure out why im getting a readonly error, can anyone please help me out?

Thanks alot in advance!

-----------------------------------------------------------------
CODE FOR FORM1.CS BELOW
-----------------------------------------------------------------

using System;<br />
using System.Drawing;<br />
using System.Collections;<br />
using System.ComponentModel;<br />
using System.Windows.Forms;<br />
using System.Data;<br />
using ButtonArray;<br />
<br />
namespace ButtonArray<br />
{<br />
	/// <summary><br />
	/// Summary description for Form1.<br />
	/// </summary><br />
	public class Form1 : System.Windows.Forms.Form<br />
	{<br />
		private System.Windows.Forms.Button btnAdd;<br />
		private System.Windows.Forms.Button btnRemove;<br />
		private System.Windows.Forms.Button btnRemoveX;<br />
		private System.Windows.Forms.TextBox txtRemoveX;<br />
<br />
		public int iRemoveX;<br />
<br />
		// Declare a new ButtonArray object.<br />
		ButtonArray btnArray;<br />
		ButtonArray tempArray;<br />
<br />
		/// <summary><br />
		/// Required designer variable.<br />
		/// </summary><br />
		private System.ComponentModel.Container components = null;<br />
<br />
		public Form1()<br />
		{<br />
			//<br />
			// Required for Windows Form Designer support<br />
			//<br />
			InitializeComponent();<br />
<br />
			//<br />
			// TODO: Add any constructor code after InitializeComponent call<br />
			//<br />
		}<br />
<br />
		/// <summary><br />
		/// Clean up any resources being used.<br />
		/// </summary><br />
		protected override void Dispose( bool disposing )<br />
		{<br />
			if( disposing )<br />
			{<br />
				if (components != null) <br />
				{<br />
					components.Dispose();<br />
				}<br />
			}<br />
			base.Dispose( disposing );<br />
		}<br />
<br />
		#region Windows Form Designer generated code<br />
		/// <summary><br />
		/// Required method for Designer support - do not modify<br />
		/// the contents of this method with the code editor.<br />
		/// </summary><br />
		private void InitializeComponent()<br />
		{<br />
			this.btnAdd = new System.Windows.Forms.Button();<br />
			this.btnRemove = new System.Windows.Forms.Button();<br />
			this.btnRemoveX = new System.Windows.Forms.Button();<br />
			this.txtRemoveX = new System.Windows.Forms.TextBox();<br />
			this.SuspendLayout();<br />
			// <br />
			// btnAdd<br />
			// <br />
			this.btnAdd.Location = new System.Drawing.Point(192, 8);<br />
			this.btnAdd.Name = "btnAdd";<br />
			this.btnAdd.Size = new System.Drawing.Size(96, 23);<br />
			this.btnAdd.TabIndex = 0;<br />
			this.btnAdd.Text = "Add Button";<br />
			this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);<br />
			// <br />
			// btnRemove<br />
			// <br />
			this.btnRemove.Location = new System.Drawing.Point(192, 40);<br />
			this.btnRemove.Name = "btnRemove";<br />
			this.btnRemove.Size = new System.Drawing.Size(96, 23);<br />
			this.btnRemove.TabIndex = 1;<br />
			this.btnRemove.Text = "Remove Button";<br />
			this.btnRemove.Click += new System.EventHandler(this.btnRemove_Click);<br />
			// <br />
			// btnRemoveX<br />
			// <br />
			this.btnRemoveX.Location = new System.Drawing.Point(192, 104);<br />
			this.btnRemoveX.Name = "btnRemoveX";<br />
			this.btnRemoveX.Size = new System.Drawing.Size(96, 23);<br />
			this.btnRemoveX.TabIndex = 2;<br />
			this.btnRemoveX.Text = "Remove X";<br />
			this.btnRemoveX.Click += new System.EventHandler(this.btnRemoveX_Click);<br />
			// <br />
			// txtRemoveX<br />
			// <br />
			this.txtRemoveX.Location = new System.Drawing.Point(192, 136);<br />
			this.txtRemoveX.Name = "txtRemoveX";<br />
			this.txtRemoveX.Size = new System.Drawing.Size(96, 20);<br />
			this.txtRemoveX.TabIndex = 3;<br />
			this.txtRemoveX.Text = "";<br />
			// <br />
			// Form1<br />
			// <br />
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);<br />
			this.ClientSize = new System.Drawing.Size(292, 266);<br />
			this.Controls.Add(this.txtRemoveX);<br />
			this.Controls.Add(this.btnRemoveX);<br />
			this.Controls.Add(this.btnRemove);<br />
			this.Controls.Add(this.btnAdd);<br />
			this.Name = "Form1";<br />
			this.Text = "Form1";<br />
			this.Load += new System.EventHandler(this.Form1_Load);<br />
			this.ResumeLayout(false);<br />
<br />
			btnArray = new ButtonArray(this);<br />
			tempArray = new ButtonArray(this);<br />
<br />
		}<br />
		#endregion<br />
<br />
		/// <summary><br />
		/// The main entry point for the application.<br />
		/// </summary><br />
		[STAThread]<br />
		static void Main() <br />
		{<br />
			Application.Run(new Form1());<br />
		}<br />
<br />
		private void Form1_Load(object sender, System.EventArgs e)<br />
		{<br />
		<br />
		}<br />
<br />
		private void aButton_Click(object sender, System.EventArgs e)<br />
		{<br />
<br />
		}<br />
<br />
		private void btnAdd_Click(object sender, System.EventArgs e)<br />
		{<br />
			// Call the AddNewButton method of MyControlArray.<br />
			btnArray.AddNewButton();<br />
			// Change the BackColor property of the Button 0.<br />
			btnArray[0].BackColor = System.Drawing.Color.Red;<br />
		}<br />
<br />
		private void btnRemove_Click(object sender, System.EventArgs e)<br />
		{<br />
			<br />
			if(btnArray.Count > 0)<br />
			{<br />
				btnArray.Remove(btnArray.Count-1);<br />
			}<br />
			else <br />
			{<br />
				MessageBox.Show("ARRAY IS EMPTY");<br />
			}// Call the Remove method of MyControlArray.<br />
		}<br />
		<br />
//		public void Collapse()<br />
//		{<br />
//			if(btnArray.Count > 0)<br />
//			{<br />
//				for(int i = btnArray.Count; i < btnArray.Count; i++)<br />
//				{<br />
//					if(i+1 <= btnArray.Count)<br />
//						btnArray = btnArray[i+1];<br />
//                }<br />
//				btnArray[btnArray.Count] = null;<br />
//			}<br />
<br />
<br />
<br />
			/*<br />
			public void removeAndCollapse(object[] array, int index)<br />
			{<br />
				if(array.Length > index)<br />
				{<br />
					for(int i = index; i < array.Length; i++)<br />
					{<br />
						if(i+1 <= array.Length)<br />
							array = array[i+1];<br />
					}<br />
					<br />
					array[array.Length] = null;<br />
				}<br />
			}<br />
			*/<br />
<br />
//		}<br />
<br />
		private void btnRemoveX_Click(object sender, System.EventArgs e)<br />
		{<br />
			try<br />
			{			<br />
				iRemoveX = Convert.ToInt32(txtRemoveX.Text);<br />
<br />
				if(btnArray.Count > 0)<br />
				{<br />
					btnArray.Remove(iRemoveX);<br />
				}<br />
				else <br />
				{<br />
					MessageBox.Show("(REMOVE) ARRAY IS EMPTY");<br />
				}<br />
			}<br />
<br />
			catch(Exception ex)<br />
			{<br />
				MessageBox.Show("ERROR: " + ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error,MessageBoxDefaultButton.Button1);			<br />
			}<br />
		}<br />
<br />
		private void CopyArray()<br />
		{<br />
			try<br />
			{<br />
				if(btnArray.Count > 0)<br />
				{<br />
					int iCounter;<br />
					for (int iButtonIndex = 0; iButtonIndex < btnArray.Count; iButtonIndex++)<br />
					{<br />
						// READ ONLY ?!<br />
						if (btnArray[iButtonIndex] != null)<br />
						{<br />
							tempArray[iCounter++] = btnArray[iButtonIndex];<br />
							iCounter++;<br />
						}<br />
						//btnArray[iButtonIndex] = tempArray[iButtonIndex];<br />
					}<br />
				}<br />
				else<br />
				{<br />
					MessageBox.Show("(COPY) ARRAY IS EMPTY");<br />
				}<br />
<br />
			}<br />
			catch(Exception ex)<br />
			{<br />
				MessageBox.Show("ERROR: " + ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error,MessageBoxDefaultButton.Button1);<br />
			}<br />
		}<br />
<br />
		private void ShowArray()<br />
		{<br />
		}<br />
	}<br />
}

----------------------------------------------------------------
CODE FOR BUTTONARRAY.CS BELOW
----------------------------------------------------------------
using System;<br />
<br />
namespace ButtonArray<br />
{<br />
	/// <summary><br />
	/// Summary description for ButtonArray.<br />
	/// </summary><br />
	public class ButtonArray : System.Collections.CollectionBase<br />
	{<br />
		private readonly System.Windows.Forms.Form HostForm;<br />
		<br />
		public System.Windows.Forms.Button AddNewButton()<br />
		{<br />
			// Create a new instance of the Button class.<br />
			System.Windows.Forms.Button aButton = new System.Windows.Forms.Button();<br />
			<br />
			// Add the button to the collection's internal list.<br />
			this.List.Add(aButton);<br />
			<br />
			// Add the button to the controls collection of the form <br />
			// referenced by the HostForm field.<br />
			HostForm.Controls.Add(aButton);<br />
			<br />
			// Set intial properties for the button object.<br />
			aButton.Top = Count * 25;<br />
			aButton.Left = 100;<br />
			aButton.Tag = this.Count;<br />
			aButton.Text = "Button " + this.Count.ToString();<br />
			<br />
<br />
			aButton.Click += new System.EventHandler(ClickHandler);<br />
			<br />
			return aButton;<br />
		}<br />
		public ButtonArray(System.Windows.Forms.Form host)<br />
		{<br />
			HostForm = host;<br />
			this.AddNewButton();<br />
		}<br />
		<br />
		public System.Windows.Forms.Button this [int Index]<br />
		{<br />
			get<br />
			{<br />
				return (System.Windows.Forms.Button) this.List[Index];<br />
			}<br />
		}<br />
		<br />
		public void Remove(int iRemoveX)<br />
		{<br />
			// Check to be sure there is a button to remove.<br />
			if (this.Count > 0)<br />
			{<br />
				// Remove the last button added to the array from the host form <br />
				// controls collection. Note the use of the indexer in accessing <br />
				// the array.<br />
				//HostForm.Controls.Remove(this[this.Count -1]);<br />
				//this.List.RemoveAt(this.Count -1);<br />
<br />
				// Remove the button indexed with the value of iRemoveX<br />
				HostForm.Controls.Remove(this[iRemoveX]);<br />
				this.List.RemoveAt(this.Count -1);<br />
			}<br />
		}<br />
		<br />
		public void ClickHandler(Object sender, System.EventArgs e)<br />
		{<br />
			System.Windows.Forms.MessageBox.Show("You have clicked button " + <br />
				((System.Windows.Forms.Button) sender).Tag.ToString());<br />
		}<br />
	}<br />
}

GeneralRe: Problems with Array being readonly (HELP) Pin
Guffa16-Sep-05 0:31
Guffa16-Sep-05 0:31 
GeneralRe: Problems with Array being readonly (HELP) Pin
Yannielsen16-Sep-05 0:56
Yannielsen16-Sep-05 0:56 
GeneralRe: Problems with Array being readonly (HELP) Pin
Guffa16-Sep-05 1:59
Guffa16-Sep-05 1:59 
GeneralRe: Problems with Array being readonly (HELP) Pin
Yannielsen16-Sep-05 2:12
Yannielsen16-Sep-05 2:12 
GeneralRe: Problems with Array being readonly (HELP) Pin
Guffa16-Sep-05 5:11
Guffa16-Sep-05 5:11 
GeneralRe: Problems with Array being readonly (HELP) Pin
Yannielsen19-Sep-05 10:13
Yannielsen19-Sep-05 10:13 
QuestionDataGrid/DataGridBoolColumn - event Pin
JuergenLissmann15-Sep-05 21:48
JuergenLissmann15-Sep-05 21:48 
QuestionHow to override the Text property of Control class? Pin
dreamwinter15-Sep-05 20:54
dreamwinter15-Sep-05 20:54 
AnswerRe: How to override the Text property of Control class? Pin
Libra15-Sep-05 23:33
Libra15-Sep-05 23:33 
QuestionSearching in Linked List Pin
Member 204556015-Sep-05 20:54
Member 204556015-Sep-05 20:54 
AnswerRe: Searching in Linked List Pin
g00fyman15-Sep-05 22:56
g00fyman15-Sep-05 22:56 
AnswerRe: Searching in Linked List Pin
enjoycrack15-Sep-05 22:56
enjoycrack15-Sep-05 22:56 
AnswerRe: Searching in Linked List Pin
S. Senthil Kumar16-Sep-05 5:13
S. Senthil Kumar16-Sep-05 5:13 
QuestionCreating Custom Shaped Windows Forms Pin
Member 204556015-Sep-05 20:21
Member 204556015-Sep-05 20:21 
AnswerRe: Creating Custom Shaped Windows Forms Pin
g00fyman15-Sep-05 23:05
g00fyman15-Sep-05 23:05 
QuestionInputting data in Graphics of C. Pin
Rana isthiaq Ahamd15-Sep-05 19:26
Rana isthiaq Ahamd15-Sep-05 19:26 
QuestionHow do I get a HACK task to span multiple lines? Pin
Vikram A Punathambekar15-Sep-05 19:18
Vikram A Punathambekar15-Sep-05 19:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.