8 Replies - 1393 Views - Last Post: 13 June 2010 - 12:55 PM Rate Topic: -----

#1 uncboog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-June 10

help with editing C# code on one of my command butons

Posted 13 June 2010 - 07:55 AM

hi again people,

I have a small problem on my code, i have copy and pasted the whole code from the form iam working on.


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;
using safeprojectname.Entities;
using safeprojectname.Handlers;
using System.Xml;
using System.Net;
using System.IO;

namespace safeprojectname
{
    public partial class frmAPIKey : Form
    {
        private APIDetails _apiDet = new APIDetails();

        public frmAPIKey()
        {
            InitializeComponent();
        }

        public APIDetails ShowModal()
        {
            this.ShowDialog();
            return _apiDet;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.eveonline.com/api/default.asp");
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Do you wish to stop creating a profile record?", "My Application",
          MessageBoxButtons.YesNo, MessageBoxIcon.Question)
          == DialogResult.Yes)
            {
                Close();
            }
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            // Checks the value of the text.

            if (txtAPIKey.Text.Length == 0 && txtUserID.Text.Length == 0)

            // If any of the fields empty, then error message.
            {

                MessageBox.Show("To create a profile record, You must enter a valid UserID & Apikey ", "Error Message");
                
            }
        
            {
                APIDetails apidet = ShowModal();
                APICommunications apiComm = new APICommunications(apidet.userID, apidet.apiKey, "");
                XmlDocument xmlDoc = apiComm.GetCharacters();
                xmlHandler xmlHand = new xmlHandler();
                Character cha = xmlHand.getCharFromXML(xmlDoc);
                //
                apiComm.CharacterID = cha.CharID;
                xmlDoc = apiComm.GetCharacterSheet();
                CharacterSheet chaSheet = xmlHand.getCharSheetFromXML(xmlDoc);
                _populategroupBox2FromCharacterSheet(chaSheet);
            }

            groupBox1.Visible = false;
            label5.Visible = false;

            
        }
        private void _populategroupBox2FromCharacterSheet(CharacterSheet cha)
        {
            
            cb1Name.Text = cha.Name;
            cb2Name.Text = cha.Name;
            cb3Name.Text = cha.Name;
            
        }
       

    }
    
} 




I can debug it, run it, when i try the commandbutton it gives me the error message that i need to input my userid & apikey into the fields, that part works ok, but when i put data in the fields an click the commandbutton

the following error happens

System.InvalidOperationException was unhandled
  Message=Form that is already visible cannot be displayed as a modal dialog box. Set the form's visible property to false before calling showDialog.
  Source=System.Windows.Forms
  StackTrace:
       at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
       at System.Windows.Forms.Form.ShowDialog()
       at safeprojectname.frmAPIKey.ShowModal() in C:\Documents and Settings\uncboog\My Documents\eve project files\eveTest\frmAPIKey.cs:line 28
       at safeprojectname.frmAPIKey.btnCreate_Click(Object sender, EventArgs e) in C:\Documents and Settings\uncboog\My Documents\eve project files\eveTest\frmAPIKey.cs:line 61
       at System.Windows.Forms.Control.onclick(EventArgs e)
       at System.Windows.Forms.Button.onclick(EventArgs e)
       at System.Windows.Forms.Button.onmouseup(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativewindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativewindow.WndProc(Message& m)
       at System.Windows.Forms.Nativewindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.RunDialog(Form form)
       at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
       at System.Windows.Forms.Form.ShowDialog()
       at safeprojectname.frmAPIKey.ShowModal() in C:\Documents and Settings\uncboog\My Documents\eve project files\eveTest\frmAPIKey.cs:line 28
       at safeprojectname.frmEmployee.btnCreate_Click(Object sender, EventArgs e) in C:\Documents and Settings\uncboog\My Documents\eve project files\eveTest\frmEmployee.cs:line 26
       at System.Windows.Forms.Control.onclick(EventArgs e)
       at System.Windows.Forms.Button.onclick(EventArgs e)
       at System.Windows.Forms.Button.onmouseup(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativewindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativewindow.WndProc(Message& m)
       at System.Windows.Forms.Nativewindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at safeprojectname.Program.Main() in C:\Documents and Settings\uncboog\My Documents\eve project files\eveTest\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 



Please can anyone advise as to what part i have totaly messed up and some possible answers, as i have no clue, been a total noob at C#.

Thanks again

Is This A Good Question/Topic? 0
  • +

Replies To: help with editing C# code on one of my command butons

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: help with editing C# code on one of my command butons

Posted 13 June 2010 - 08:06 AM

Well if you look at this code

if (txtAPIKey.Text.Length == 0 && txtUserID.Text.Length == 0)

            // If any of the fields empty, then error message.
            {

                MessageBox.Show("To create a profile record, You must enter a valid UserID & Apikey ", "Error Message");
                
            }
        
            {
                APIDetails apidet = ShowModal();
                APICommunications apiComm = new APICommunications(apidet.userID, apidet.apiKey, "");
                XmlDocument xmlDoc = apiComm.GetCharacters();
                xmlHandler xmlHand = new xmlHandler();
                Character cha = xmlHand.getCharFromXML(xmlDoc);
                //
                apiComm.CharacterID = cha.CharID;
                xmlDoc = apiComm.GetCharacterSheet();
                CharacterSheet chaSheet = xmlHand.getCharSheetFromXML(xmlDoc);
                _populategroupBox2FromCharacterSheet(chaSheet);
            }

            groupBox1.Visible = false;
            label5.Visible = false;


It does the if but since there's no else it goes right into that section of code as well. Then when you click the button again it's trying to show a form that's already being shown, which is a no-no. Add an else in that code like this

if (txtAPIKey.Text.Length == 0 && txtUserID.Text.Length == 0)

            // If any of the fields empty, then error message.
            {

                MessageBox.Show("To create a profile record, You must enter a valid UserID & Apikey ", "Error Message");
                
            }
            else
            {
                APIDetails apidet = ShowModal();
                APICommunications apiComm = new APICommunications(apidet.userID, apidet.apiKey, "");
                XmlDocument xmlDoc = apiComm.GetCharacters();
                xmlHandler xmlHand = new xmlHandler();
                Character cha = xmlHand.getCharFromXML(xmlDoc);
                //
                apiComm.CharacterID = cha.CharID;
                xmlDoc = apiComm.GetCharacterSheet();
                CharacterSheet chaSheet = xmlHand.getCharSheetFromXML(xmlDoc);
                _populategroupBox2FromCharacterSheet(chaSheet);
            }

            groupBox1.Visible = false;
            label5.Visible = false;


Was This Post Helpful? 0
  • +
  • -

#3 uncboog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-June 10

Re: help with editing C# code on one of my command butons

Posted 13 June 2010 - 09:04 AM

Hi again,

thanks bud, the else is in, and at moment working nicely,
Do you have any suggestions on how i should get this to stop trying to show a form that is already open,


public APIDetails ShowModal()
        {
            this.ShowDialog();
            return _apiDet;
        }



Because if i try to remove or edit parts, it starts up all different kinds of errors, but i believe this is a required section.

return _apiDet;



so any advice would be great,

thanks

This post has been edited by uncboog: 13 June 2010 - 09:06 AM

Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: help with editing C# code on one of my command butons

Posted 13 June 2010 - 09:33 AM

Actually in the code you've shown there is no else in your btnCreate_Click method (just an if).

Now on to your next question, checking if a form is already open. You can use a method like this one that utilizes Application.OpenForms to see if a certain form is in the list of opened forms

public bool IsFormOpen(Type form)
{
    foreach (Form frm in System.Windows.Forms.Application.OpenForms)
    {
        if (frm.GetType() == form)
            return true;
    }

    return false;
}



Then before calling this.ShowDialog(); just use the above method

if(!IsFormOpen(this)
{
    this.ShowDialog();
}


Was This Post Helpful? 0
  • +
  • -

#5 uncboog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-June 10

Re: help with editing C# code on one of my command butons

Posted 13 June 2010 - 11:44 AM

Hi again bud,

Iam just going over this code, i have added what you mentioned in your last post, can you advise if this is anywere near close, as i have errors,


 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;
using safeprojectname.Entities;
using safeprojectname.Handlers;
using System.Xml;
using System.Net;
using System.IO;

namespace safeprojectname
{
    public partial class frmAPIKey : Form
    {
        private APIDetails _apiDet = new APIDetails();

        public frmAPIKey()
        {
            InitializeComponent();
        }
        public bool IsFormOpen(Type form)
        {
            foreach (Form frm in System.Windows.Forms.Application.OpenForms)
            {
                if (frm.GetType() == form)
                    return true;
            }

            return false;
        }
       
        public APIDetails ShowModal() 
        {
            if(!IsFormOpen(this)
            {
                this.ShowDialog(
                return _apiDet;
            }

         }
       
        
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.eveonline.com/api/default.asp");
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Do you wish to stop creating a profile record?", "My Application",
          MessageBoxButtons.YesNo, MessageBoxIcon.Question)
          == DialogResult.Yes)
            {
                Close();
            }
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {

            // Checks the value of the text.

            if (txtAPIKey.Text.Length == 0 && txtUserID.Text.Length == 0)

            // If any of the fields empty, then error message.
            {

                MessageBox.Show("To create a profile record, You must enter a valid UserID & Apikey ", "Error Message");
                
            }
            else


                // Make the comms and return data to xml Document, Take xml document an populate the checkboxes within groupBox2.
            {
                frmAPIKey newform = new frmAPIKey();
                APIDetails apidet = ShowModal();
                APICommunications apiComm = new APICommunications(apidet.userID, apidet.apiKey, "");
                XmlDocument xmlDoc = apiComm.GetCharacters();
                xmlHandler xmlHand = new xmlHandler();
                Character cha = xmlHand.getCharFromXML(xmlDoc);
                //
                apiComm.CharacterID = cha.CharID;
                xmlDoc = apiComm.GetCharacterSheet();
                CharacterSheet chaSheet = xmlHand.getCharSheetFromXML(xmlDoc);
                _populategroupBox2FromCharacterSheet(chaSheet);
            }
    
        }
        private void _populategroupBox2FromCharacterSheet(CharacterSheet cha)
        {
            groupBox1.Visible = false;
            label5.Visible = false;
            groupBox2.Visible = true;
            
            cb1Name.Text = cha.Name;
            cb2Name.Text = cha.Name;
            cb3Name.Text = cha.Name;
            
        }
       

    }
    
} 



sorry to be such a noob at this,
Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: help with editing C# code on one of my command butons

Posted 13 June 2010 - 11:53 AM

Well you need to tell us what error(s) you have, not just expect us to guess it :)
Was This Post Helpful? 0
  • +
  • -

#7 uncboog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-June 10

Re: help with editing C# code on one of my command butons

Posted 13 June 2010 - 12:13 PM

Hi again,

Sorry bud, that would help too, lololololol

error
Error	3	The best overloaded method match for 'safeprojectname.frmAPIKey.IsFormOpen(System.Type)' has some invalid arguments	C:\Documents and Settings\uncboog\My Documents\eve project files\eveTest\frmAPIKey.cs	39	18	CHD1.0.2b



Getting this error for this line.

!IsFormOpen(this)


Was This Post Helpful? 0
  • +
  • -

#8 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: help with editing C# code on one of my command butons

Posted 13 June 2010 - 12:32 PM

Sorry, wrote that in Notepad. It's giving that error because this is type Form and the method is expecting Type. Try something like this

!IsFormOpen(this.GetType())


Was This Post Helpful? 1
  • +
  • -

#9 uncboog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-June 10

Re: help with editing C# code on one of my command butons

Posted 13 June 2010 - 12:55 PM

Hi again bud,

Thanks for all the advise bud, it working now, your a master of your trade. lololol

I was also trying to find out if it would be possible to get the character names from the xml document, to populate the three checkboxes within a groupbox, as i have tryed here in the code.


 private void _populateScreenFromCharacterSheet(CharacterSheet cha)
        {
            groupBox1.Visible = false;
            label5.Visible = false;
            groupBox2.Visible = true;
            
            cb1Name.Text = cha.Name;
            cb2Name.Text = cha.Name;
            cb3Name.Text = cha.Name;
            
        }



But i dont even know if this is possible.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1