- Creating the User Feedback Form
- Sending the Data
- Defining Feedback Form Linkage
- Summary
The feedback form collects the data. At some point, you'll need to send the data somewhere.
For this example, I chose to send the data to my e-mail address. You could just as easily use a Web service or a database. The big thing is to send the data where someone will look at it.
Listing 2 shows the code I used to send the data.
Listing 2 Sending the data using e-mail
private: System::Void btnSubmit_Click(System::Object * sender, System::EventArgs * e) { StringBuilder* Body; // Message content. MailMessage* Msg; // Complete message. Int32 Counter; // Loop counter. // Create the standard content. Body = new StringBuilder(); Body->Append("Name="); Body->Append(txtName->Text); Body->Append("\r\nTitle="); Body->Append(txtTitle->Text); Body->Append("\r\nComment="); Body->Append(txtComment->Text); // Create the extra content, if any. if (this->Controls->get_Count() > 8) { // Process each control in turn. for (Counter = 8; Counter < this->Controls->get_Count(); Counter = Counter + 2) { // Add the label. Body->Append("\r\n"); Body->Append( this->Controls->get_Item(Counter)->Text); Body->Append("="); // Add the Text. Body->Append( this->Controls->get_Item(Counter + 1)->Text); } } // Create the message. Msg = new MailMessage(); Msg->From = "John Mueller <JMueller@mwt.net>"; Msg->To = "John Mueller <JMueller@mwt.net>"; Msg->Subject = this->Text; Msg->Body = Body->ToString(); try { // Create a connection to the mail server. SmtpMail::SmtpServer = "Mail Server Address"; // Send the message. SmtpMail::Send(Msg); } catch (System::Web::HttpException* err) { // Display an error message. MessageBox::Show(err->Message); return; } // Display a success message. MessageBox::Show("Feedback Sent Successfully!"); }
The code begins by creating a StringBuilder object to hold the body text, which includes all the feedback form data. It appends all the standard data first. The example form contains eight controls. When the this->Controls->get_Count() method returns a value higher than 8, the form must contain additional controls. Because the constructor adds the controls in a specific fashion, the processing loop can make assumptions about the structure of these controls and use that information to generate additional body text.
Once the code creates the body text, it creates the MailMessage object. In this case, the sender and the recipient are the same. For a real application, you'd set the Msg->From property value to match the current user's e-mail address. Finally, the code sends the message using Simple Mail Transfer Protocol (SMTP).