Example #1
0
 public static void ProtocolItemsToListBoxProtocolItems(List<ProtocolInstruction> Instructions, System.Windows.Forms.ListBox ListBoxToFill, bool AddToCurrent)
 {
     int index = 1;
     if (AddToCurrent)
     {
         index = ListBoxToFill.Items.Count + 1;
     }
     foreach (ProtocolInstruction Instruction in Instructions)
     {
         if (Instruction is StaticProtocolItem)
         {
             ListBoxProtocolItem inst = new ListBoxProtocolItem();
             inst.InstructionNumber = index;
             inst.Instruction = (StaticProtocolItem)Instruction;
             ListBoxToFill.Items.Add(inst);
         }
         else if (Instruction is LoopInstruction)
         {
             ListBoxRepeatItem rep = new ListBoxRepeatItem();
             rep.InstructionNumber = index;
             rep.RepeatInstruction = (LoopInstruction)Instruction;
             ListBoxToFill.Items.Add(rep);
         }
         else if (Instruction is DelayTime)
         {
             ListBoxDelayItem del = new ListBoxDelayItem();
             del.delay = (DelayTime)Instruction;
             del.InstructionNumber = index;
             ListBoxToFill.Items.Add(del);
         }
         index++;
     }
 }
Example #2
0
        private void btnAddMethod_Click(object sender, EventArgs e)
        {
            //This method is now greatly changed to accomodate the new format of having variables in there
            //mostly just a awful lot of type checking, input verification stuff

            if (lblMethodName.Text.Split('-').Length == 2 && MethodsView.SelectedNode!=null)
            {
                TreeNode curSelection = MethodsView.SelectedNode;
                ListBoxProtocolItem CurProtocolItem = new ListBoxProtocolItem();
                string instName = lblMethodName.Text.Split('-')[0];
                string methName = lblMethodName.Text.Split('-')[1];
                CurProtocolItem.Instruction.InstrumentName = instName;
                CurProtocolItem.Instruction.MethodName = methName;
                object[] Parameters;
                if (tblMethodParameterView.DataSource != null)
                {
                    DataTable DT = (DataTable)tblMethodParameterView.DataSource;
                    DataRow DR = DT.Rows[0];
                    Parameters = DR.ItemArray;//actual inputted values, as strings

                    //Now we will find out what this value is, to make sure it is all agreeable
                    string MethodName = curSelection.Text.ToString().Split('(')[0];
                    string InstrumentName = curSelection.Parent.Text;
                    var Instrument = GetInstrumentClass(InstrumentName);
                    var Meth = ReturnMethodInfoFromObject(MethodName, Instrument);

                    ParameterInfo[] ParametersinMeth=Meth.GetParameters();
                    bool RequiresDynamicParameter = false;
                    if (ParametersinMeth[ParametersinMeth.Length - 1].ParameterType == typeof(AdditionalMethodArguments))
                        RequiresDynamicParameter = true;
                    int pCount = RequiresDynamicParameter ? ParametersinMeth.Length - 1 : ParametersinMeth.Length;
                    Type[] ParameterTypes = new Type[pCount];
                    for (int j = 0; j < pCount; j++)
                    {ParameterTypes[j]=ParametersinMeth[j].ParameterType;}
                    if (ParameterTypes.Length != DR.ItemArray.Length)
                    { MessageBox.Show("Inputted parameters and expected parameters do not have an equal count, contact Nigel"); return; }
                    for (int i = 0; i < ParameterTypes.Length;i++ )
                    {
                        object o = Parameters[i];//actual inputted value, currently as a string
                        if (o.ToString() == "")
                        {
                            MessageBox.Show("You need to supply more parameters before this method can be added", "Parameters Not Supplied", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                            return;
                        }
                       if (ParameterTypes[i] == System.Type.GetType("System.String"))
                        {
                           //check that there are real variables in the
                            if (!ProtocolManager.CheckStringForValidVariables((string)Parameters[i], (IList)lstCurrentVariables.Items))
                            {
                                MessageBox.Show("You have entered a variable that does not exist","Bad Data Entry",MessageBoxButtons.OK,MessageBoxIcon.Error);
                            }
                            continue;//string types always get cleared
                        }
                        else//need to check if this is a variable, and if it is not, I neet to check that the method works out okay
                        {
                            Type ExpectedType = ParameterTypes[i];
                            //two cases, either they have entered a variable, or they have a value, to find out which
                            string VariableName;
                            bool ParameterIsVariable = ProtocolManager.GetVariableNameFromText((string)o, out VariableName);
                            if (ParameterIsVariable)
                            {
                                //first to check that such a variable exists

                                ReferenceToProtocolVariable Variable = new ReferenceToProtocolVariable(VariableName);
                                if (!ProtocolManager.IsVariableInList(VariableName, (IList)lstCurrentVariables.Items))
                                { MessageBox.Show("You are using a variable name that does not exist in your protocol", "Bad Variable Name", MessageBoxButtons.OK, MessageBoxIcon.Error); return; }
                                else
                                {
                                    ProtocolVariable VariableInList = new ProtocolVariable();
                                    foreach (object Varia in lstCurrentVariables.Items)
                                    { if (Varia.ToString() == VariableName)
                                    { VariableInList = (ProtocolVariable)Varia; break; } }

                                    //make sure the variable type and parameter type match
                                    if (VariableInList.DataType != ParameterTypes[i])
                                    {
                                        MessageBox.Show("You are using a variable that does not have the appropriate type for this parameter", "Bad Variable Type", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                        return;
                                    }
                                    else
                                    {
                                        //replace the "parameter" with the reference to the variable
                                        Parameters[i] = Variable;
                                    }
                                }
                            }
                            else
                            {
                                //make sure I can actually convert the damn thing to what it should be eg "A"->"a"
                                try
                                {
                                    Parameters[i]=Convert.ChangeType((string)o, ParameterTypes[i]);
                                }
                                catch
                                {
                                    MessageBox.Show("Could not convert your entry to the proper type, please make sure that numbers are used where they should be", "Bad Entry", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    return;
                                }
                            }
                        }
                    }
                }
                else
                {
                    Parameters = new object[0];
                }
                CurProtocolItem.Instruction.Parameters = Parameters;
                CurProtocolItem.InstructionNumber = GetNextInstructionNumber();
                lstProtocol.Items.Add(CurProtocolItem);
            }
        }