[.net] Control problem, won't convert one control to another :(

Started by
8 comments, last by GameMasterXL 18 years, 3 months ago
I currently have a problem with my controls that i am using this is the errors i keep on getting. Error 1 Cannot implicitly convert type 'System.Windows.Forms.Control[]' to 'System.Windows.Forms.Control' C:\Documents and Settings\Michael\my documents\visual studio 2005\projects\MSE Script Editor Beta1\MSE Script Editor Beta1\MSE_Classes\Save.cs 28 35 MSE Script Editor Beta1 and i get that error twise for each control assignment. Here is my code:

namespace MSE_Script_Editor_Beta.MSE_Classes
{
    class Save
    {
        /// <summary>
        /// returns true if we have more than one script open else false if not
        /// </summary>
        /// <returns>true or false</returns>
        public bool MultipleScriptsOpen()
        {
            if (MSEGlobal.saveDef.ScriptsOpen > 1)
                return true; // yes we have more than one script open
            return false; // no we don't
        }
        public void SaveScript()
        {
            int i;
            Control[] foundControl; // the control that was found is stored in here

            // find tab controls in our main tab control
            for (i = 0; i < MSEGlobal.saveDef.ScriptsOpen; i++)
            {
                foundControl = MSEGlobal.scrView.ScriptTabControl.Controls.Find(MSEGlobal.saveDef.ScriptTabNames, false);
            }
            // find syntaxbox objects in each tab control found
            for (i = 0; i < MSEGlobal.saveDef.ScriptsOpen; i++)
            {
                foundControl = foundControl.Controls.Find(MSEGlobal.saveDef.ScriptNames, false); // find our control to obtain its text property
            }
            // now find each text member of the syntaxbox object in our control list of syntax boxes
            for (i = 0; i < MSEGlobal.saveDef.ScriptsOpen; i++)
            {
                string Data; // this stores our script
                string CtrlName; // our script name with file extention
                Data = foundControl.Text;
                CtrlName = foundControl.Name.ToString();
                foundControl.
                SaveFile(Data, CtrlName, MSEGlobal.saveDef.SaveDirectorys); // save the file by passing the contents within our data string to it to save
            }
        }
        /// <summary>
        /// This function saves a script file obtaining all the nessacery properties needed for the file.
        /// </summary>
        /// <param name="script">This is passed our script data to save to the file specified</param>
        /// <param name="scriptName">This is used when saving the script file since this will store the file name</param>
        /// <param name="directory">The directory of were the script will be saved to</param>
        public void SaveFile(string script, string scriptName, string directory)
        {
            //TODO ADD BODY
        }
    }
}

Hope someone can help thnx in advance.
Advertisement
You're trying to assign an array of controls to a single control. I'm gonna go out on a a limb an guess that Controls.Find() returns an *array* of matching controls and not a single control.

If you know it will only ever return 1 match, you could use the following:
foundControl = MSEGlobal.scrView.ScriptTabControl.Controls.Find(MSEGlobal.saveDef.ScriptTabNames, false)[0];

Notice the [0] index at the end.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
Thnx but now i seem to be getting this error

Error 1 Use of unassigned local variable 'foundControl' C:\Documents and Settings\Michael\my documents\visual studio 2005\projects\MSE Script Editor Beta1\MSE Script Editor Beta1\MSE_Classes\Save.cs 30 17 MSE Script Editor Beta1

What could this be due to?
Because foundControl hasn't been assigned to before you used it? [smile]
//Control[] foundControl;Control[] foundControl = new Control[MSEGlobal.saveDef.ScriptsOpen];
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
Thanks for that but now i seem to be getting a run-time exception saying that foundControl = foundControl.Controls.Find(MSEGlobal.saveDef.ScriptNames, false)[0];

Object reference not set to an instance of an object.

What could be causing this since it seems to look ok.
Controls.Find() is likely returning null when it doesn't find any matches, so you'll have to check it's return value, then either set foundControl to null or result[0] depending on the result.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
Can you post an example please?
The control is returning a null but i don't know why this is happening since it just dosn't seem to be returning the control found does anyone know of what is happening? thnkx in advance.
Quote:Original post by GameMasterXL
The control is returning a null but i don't know why this is happening since it just dosn't seem to be returning the control found does anyone know of what is happening? thnkx in advance.

Myabe there's no control with that name, or a control with that name resides in a different control collection.

Pseudo code:
Control c = Find("idofcontrolthatdoesnotexist");if (c == null)  // no such controlelse  // yay! found it!
I am using an array of names that i store to search by through each one so it should find it since each array element stores the object name. Now i changed my code to this but it won't save the data at all can you please see what the problem could be.

using System;using System.Collections.Generic;using System.Text;using System.Windows.Forms;using System.IO;namespace MSE_Script_Editor_Beta.MSE_Classes{    class Save    {        /// <summary>        /// returns true if we have more than one script open else false if not        /// </summary>        /// <returns>true or false</returns>        public bool MultipleScriptsOpen()        {            if (MSEGlobal.saveDef.ScriptsOpen > 1)                return true; // yes we have more than one script open            return false; // no we don't        }        public void SaveScript()        {            int i;            Control[] foundControl = new Control[MSEGlobal.saveDef.ScriptsOpen]; // the control that was found is stored in here            Control[] foundControl2 = new Control[MSEGlobal.saveDef.ScriptsOpen];            Control[] foundControl3 = new Control[MSEGlobal.saveDef.ScriptsOpen];            // find tab controls in our main tab control            MSEGlobal.scrView.ScriptTabControl.Controls.CopyTo(foundControl, 0);            // find syntaxbox objects in each tab control found            foundControl.CopyTo(foundControl2, 0);            foundControl2.CopyTo(foundControl3, 0);            MessageBox.Show(foundControl[0].ToString() + MSEGlobal.saveDef.SaveDirectorys[1], "gg");            // now find each text member of the syntaxbox object in our control list of syntax boxes            for (i = 1; i < foundControl3.Length; i++)            {                string Data; // this stores our script                string CtrlName; // our script name with file extention                Data = foundControl3.Text;                CtrlName = foundControl.Name.ToString();                SaveFile(Data, CtrlName, MSEGlobal.saveDef.SaveDirectorys); // save the file by passing the contents within our data string to it to save            }        }        /// <summary>        /// This function saves a script file obtaining all the nessacery properties needed for the file.        /// </summary>        /// <param name="script">This is passed our script data to save to the file specified</param>        /// <param name="scriptName">This is used when saving the script file since this will store the file name</param>        /// <param name="directory">The directory of were the script will be saved to</param>        private void SaveFile(string script, string scriptName, string directory)        {            try            {                string wholeDir = directory + @"\" + scriptName; // script directory and script file name                FileStream scrFile = new FileStream(wholeDir, FileMode.Create, FileAccess.Write); // create file stream to write our script to                StreamWriter stream = new StreamWriter(scrFile); // create stream writer                stream.Write(script); // write our script out                stream.Flush(); // flush contents                scrFile.Flush(); // flush contents                scrFile.Close(); // close our file we have finished :)            }            catch (Exception ex)            {                if (MessageBox.Show(MSEGlobal.MseErrors.saveError + scriptName + ", " + ex.Message + "\nYou may have to save each file individualy", MSEGlobal.MseErrors.runTimeEx, MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK)                {                    return; // return out of saving function to carry on with program execution                }            }        }        public void SaveScriptDoc()        {            SaveScript(); // save our script        }    }}


It won't save and for some reason my variables which i am storing my values in are showing has Null which is annoying since i call a global static object that i created of my class that stores the script name and directory ect and then i store them into an array of which for some reason it isn't doing here is my code.

using System;using System.Collections.Generic;using System.Text;using System.Windows.Forms;using Boot.UI;using System.Drawing;namespace MSE_Script_Editor_Beta.MSE_Classes{    /// <summary>    /// Prompts the user for a script name and script file save location and also creates a new tab in the    /// script view window or creates a new script view window with a new tab control and tab if the window    /// isn't all ready created.    /// </summary>    public class NewScript    {        public string Name; // stores our script file name        public string ScriptDirectory; // stores the directory were our script got created in        /// <summary>        /// Shows a dialog box that prompts the user for a new script name and script save directory.        /// </summary>        public void showNewScriptPrompt() // shows a dialog box that prompts the user for a script name and the directory to save the script to or create a script in        {            NewScriptPrompt prompt = new NewScriptPrompt(); // initiate our dialog box            prompt.Show(); // show prompt        }        public NewScript()        {        }        /// <summary>        /// Creates a new script document        /// </summary>        public void CreateNewScript()        {            // check if the scriptview window is open or not, if so add our tab to it and syntaxbox to the tab            if (MSEGlobal.mainApp.ScriptViewCreated == true)            {                CreateTab(); // create our tab            }            else // not open so create it            {                MSEGlobal.scrView.MdiParent = MSEGlobal.mainApp; // our msemain form is our scriptviews parent so that scriptview is docked within mse main application                MSEGlobal.scrView.Show(); // show the script view window                CreateTab(); // create our tab                MSEGlobal.mainApp.ScriptViewCreated = true; // window is now open so set this to true            }        }        /// <summary>        /// Create a Tab for the TabControl        /// </summary>        public void CreateTab()        {            Control[] col = new Control[1];            SimpleTabPage Tab = new SimpleTabPage(); // create a tab for our tab control to store the SyntaxBox control on            SyntaxBox ScriptBoxCtrl = new SyntaxBox();            //Tab            Tab.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)                        | System.Windows.Forms.AnchorStyles.Left)                        | System.Windows.Forms.AnchorStyles.Right))); // anchor styles            Tab.Controls.Add(ScriptBoxCtrl);            Tab.BackColor = System.Drawing.Color.Transparent; // back colour for our tabs are transparent so we can see the gradient on them instead            Tab.Size = new System.Drawing.Size(766, 372);            Tab.Location = new System.Drawing.Point(0, 0); // location to draw our tab            Tab.Name = MSE_Classes.MSEGlobal.ScriptData.Name+"1"; // use generated tab name            Tab.Text = MSE_Classes.MSEGlobal.ScriptData.Name; // Assigne our script name from our globaly created object we made to store our script name and directory in            // Syntaxbox control            ScriptBoxCtrl.AutoSize = true;            ScriptBoxCtrl.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;            ScriptBoxCtrl.BackColor = System.Drawing.SystemColors.ActiveCaption;            ScriptBoxCtrl.Location = new System.Drawing.Point(3, 3);            ScriptBoxCtrl.Name = MSEGlobal.ScriptData.Name; // assigne script name to each syntaxbox control            ScriptBoxCtrl.Size = new System.Drawing.Size(971, 265);            MSEGlobal.scrView.ScriptTabControl.Tabs.AddRange(new SimpleTabPage[] { Tab }); // add our tab to the tab control            Name = MSEGlobal.ScriptData.Name; // assigne name out of our global object into our new object            MessageBox.Show(MSEGlobal.saveDef.ScriptsOpen.ToString() + " " + ScriptDirectory + " " + ScriptBoxCtrl.Name.ToString() + " " +ScriptBoxCtrl.SyntaxBoxInput.Text, "ggg");            ScriptDirectory = MSEGlobal.ScriptData.ScriptDirectory; // assigne script directory from our global object into our new object            MSEGlobal.saveDef.ScriptNames[MSEGlobal.saveDef.ScriptsOpen] = Name;            MSEGlobal.saveDef.SaveDirectorys[MSEGlobal.saveDef.ScriptsOpen] = ScriptDirectory;            MSEGlobal.saveDef.ScriptsOpen++;        }        public SimpleTabPage Tab;    }}


For some reason it seems to be skipping the lines past the messagebox and i don't know why. This is realy annoying now since it won't give enougth details on the problems and it is just weird of why it is skipping.

This topic is closed to new replies.

Advertisement