// This subroutine handles the btnCreateSurvey.Click event and creates // a new frmSurveyForm. The controls that are generated are added to the // created survey form. There are no event handlers associated with the // created controls. // The created form is fairly general, and creates a survey with questions // that are based off information provided by the Questions.xml document. // By changing, adding, or removing nodes in the XML document, you can // change the structure and form of the survey. private void btnCreateSurvey_Click(object sender, System.EventArgs e) { // Create a new Survey Form to display to the User. frmSurveyForm survey = new frmSurveyForm(); // get {the controls collection of the Survey form. Control.ControlCollection surveyControls = survey.SurveyFormControls; // Reset the m_Location in case the user creates multiple forms m_Location = new Point(10, 10); // Create an XML document to read in the survey questions XmlDocument xr = new XmlDocument(); xr.Load(@"..\..\Questions.xml"); // get {the Tag used for each of the Controls we'll create. This may // be useful later, if the example was extended to break apart // different types of questions/responses for analysis. string myTag = xr.SelectSingleNode("//survey").Attributes["name"].Value; // set {the Title on the survey form to the Display Name of the Survey survey.SurveyTitle = xr.SelectSingleNode("//survey").Attributes["displayName"].Value; // Create an XmlNodeList to contain each of the questions. Fill it. XmlNodeList nodeList; nodeList = xr.GetElementsByTagName("question"); // Create a temporary XML Node to use when retrieving information // about the nodes in the nodeList just created. foreach (XmlNode myNode in nodeList) { if (myNode.Attributes != null) { // Determine what type of control should be created. Pass // in the required information, including the Controls collection // from the frmSurveyForm form. switch (myNode.Attributes["type"].Value) { case "dropdown": m_Location = Survey_AddComboBox(myNode, surveyControls, m_Location, myTag); break; case "multilist": m_Location = Survey_AddListBox(myNode, surveyControls, m_Location, myTag, true); break; case "text": m_Location = Survey_AddTextBox(myNode, surveyControls, m_Location, myTag); break; case "radio": m_Location = Survey_AddRadioButtons(myNode, surveyControls, m_Location, myTag); break; } } } // set {the size of the form, based off of how many controls // have been placed on the form, and their dimensions. survey.Width = m_Location.X + CONTROL_WIDTH + 30; // Add a bit extra to leave room for the OK and Cancel buttons. survey.Height = m_Location.Y + 75; // Show the form. You can also use the Show() method if you like. survey.ShowDialog(); // Show the response to the user. MessageBox.Show(survey.SurveyResponse, this.Text, MessageBoxButtons.OK); }
// This subroutine handles the btnCreateSurvey.Click event and creates // a new frmSurveyForm. The controls that are generated are added to the // created survey form. There are no event handlers associated with the // created controls. // The created form is fairly general, and creates a survey with questions // that are based off information provided by the Questions.xml document. // By changing, adding, or removing nodes in the XML document, you can // change the structure and form of the survey. private void btnCreateSurvey_Click(object sender, System.EventArgs e) { // Create a new Survey Form to display to the User. frmSurveyForm survey = new frmSurveyForm(); // get {the controls collection of the Survey form. Control.ControlCollection surveyControls = survey.SurveyFormControls; // Reset the m_Location in case the user creates multiple forms m_Location = new Point(10, 10); // Create an XML document to read in the survey questions XmlDocument xr = new XmlDocument(); xr.Load(@"..\..\Questions.xml"); // get {the Tag used for each of the Controls we'll create. This may // be useful later, if the example was extended to break apart // different types of questions/responses for analysis. string myTag = xr.SelectSingleNode("//survey").Attributes["name"].Value; // set {the Title on the survey form to the Display Name of the Survey survey.SurveyTitle = xr.SelectSingleNode("//survey").Attributes["displayName"].Value; // Create an XmlNodeList to contain each of the questions. Fill it. XmlNodeList nodeList; nodeList = xr.GetElementsByTagName("question"); // Create a temporary XML Node to use when retrieving information // about the nodes in the nodeList just created. foreach(XmlNode myNode in nodeList) { if (myNode.Attributes != null) { // Determine what type of control should be created. Pass // in the required information, including the Controls collection // from the frmSurveyForm form. switch (myNode.Attributes["type"].Value) { case "dropdown": m_Location = Survey_AddComboBox(myNode, surveyControls, m_Location, myTag); break; case "multilist": m_Location = Survey_AddListBox(myNode, surveyControls, m_Location, myTag, true); break; case "text": m_Location = Survey_AddTextBox(myNode, surveyControls, m_Location, myTag); break; case "radio": m_Location = Survey_AddRadioButtons(myNode, surveyControls, m_Location, myTag); break; } } } // set {the size of the form, based off of how many controls // have been placed on the form, and their dimensions. survey.Width = m_Location.X + CONTROL_WIDTH + 30; // Add a bit extra to leave room for the OK and Cancel buttons. survey.Height = m_Location.Y + 75; // Show the form. You can also use the Show() method if you like. survey.ShowDialog(); // Show the response to the user. MessageBox.Show(survey.SurveyResponse, this.Text, MessageBoxButtons.OK); }