C# form Close message box on button click - c#

Is there a way to get the message box to close and return to the form without it emailing the data?
halfway there atm just need a help with the second if/else if statement. I'm very new to all this.
my code is below :)
//Button
private void submitButton_Click(object sender, EventArgs e)
{
string software = null;
string hardware = null;
foreach (string s in softwareNeededCheckedListBox.CheckedItems)
{
software = software + '\n' + s;
}
foreach (string s in hardwareNeededCheckedListBox.CheckedItems)
{
hardware = hardware + '\n' + s;
}
if (yesRadioButton.Checked == true)
{
yesRadioButton.Text = "Yes";
noRadioButton.Text = " ";
}
if (noRadioButton.Checked == true)
{
noRadioButton.Text = "No";
yesRadioButton.Text = " ";
}
if (MessageBox.Show("First name: " + firstNameTextBox.Text + " " + "Last name: " + lastNameTextBox.Text + "\n" + "\n" +
"Will be working in " + areaOfTheCompanyComboBox.SelectedItem + "\n" + "\n" +
"They will need the following software:" + software + "\n" + "\n" +
"They will need the following hardware:" + hardware + "\n" + "\n" +
"They will be seated: " + seatingLocationRichTextBox.Text + "\n" + "\n" +
"Any further Comments: " + notesRichTextBox.Text + "\n" + "\n" +
"Do they require a phone: " + noRadioButton.Text + yesRadioButton.Text + "\n" + "\n" +
"Date they start: " + completionDateTimePicker.Text + "\n" + "\n" +
"Are you happy with the information shown above?" + "\n" + "\n" +
MessageBoxButtons.OKCancel) == DialogResult.OK)
{
MailMessage mail = new MailMessage();
SmtpClient smtpserver = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("xxxxx#gmail.com");
mail.To.Add("xxxxx#xxxxxx.co.uk");
mail.Subject = "Test Mail";
mail.Body = "First name: " + firstNameTextBox.Text + " " + "Last name: " + lastNameTextBox.Text + "\n" + "\n" +
"Will be working in " + areaOfTheCompanyComboBox.SelectedItem + "\n" + "\n" +
"They will need the following software:" + software + "\n" + "\n" +
"They will need the following hardware:" + hardware + "\n" + "\n" +
"They will be seated: " + seatingLocationRichTextBox.Text + "\n" + "\n" +
"Any further Comments: " + notesRichTextBox.Text + "\n" + "\n" +
"Do they require a phone: " + noRadioButton.Text + yesRadioButton.Text + "\n" + "\n" +
"Date they start: " + completionDateTimePicker.Text;
smtpserver.Port = 587;
smtpserver.Credentials = new System.Net.NetworkCredential("**", "********");
smtpserver.Send(mail);
MessageBox.Show("Your Form has been sent ");
}

You actually forgot you place commas to your MessageBox.Show to have the correct arguments/parameters.
if (MessageBox.Show("Body of your message box",
"Title Of Your MessageBox",
MessageBoxButtons.OKCancel,
MessageBoxIcon.Information) == DialogResult.OK)
{
}

That will only e-mail if the user clicks OK. You can handle the cancel button by using DialogResult.Cancel
I would personally put the MessageBox in a dialogResult variable. For instance:
var dialogResult = MessageBox.Show("FIrst name .... ");
Then you can check if the user pressed OK or Cancel
if (dialogResult == DialogResult.OK) {
// Send the e-mail
} else if (dialogResult == DialogResult.Cancel) {
// Do what you need to do / return to the form.
}
To show a messagebox with OK and Cancel you need:
MessageBox.Show(this, "Message", "Title", MessageBoxButtons.OKCancel);

Related

How to replace word with hyperlink?

Starting with an example, I have some keywords like
Narendra Modi, Modi, India, Speech, Parliament
And I have a story with text
Narendra Modi will give a speech in the parliament of India.
Now I want my regex to replace the keyword with the hyperlink in the story.
<a>Narendra Modi</a> will give speech in the <a>parliament</a> of <a>India.</a>
My Code for this is
var tagArray = bodykeywords.Split(',').ToList();
foreach (var tag in tagArray.OrderBy(a => a.Length))
{
var replaceTag = tag.Replace("(", "");
replaceTag = replaceTag.Replace(")", "");
DataDesc = Regex.Replace(DataDesc, "\"" + replaceTag.Trim() + "\"", " \"" + replaceTag.Trim() + "\" ");
DataDesc = Regex.Replace(DataDesc, " " + replaceTag.Trim() + ", ", " " + replaceTag.Trim() + ", ");
DataDesc = Regex.Replace(DataDesc, " " + replaceTag.Trim() + " ", " " + replaceTag.Trim() + " ");
}
Problem is I am not able to replace the word with full stop like India in the given example and Word with repetition like Narendra Modi, Modi in the keyword.

Add pause to Alexa without using SSML

Is there a way to add a pause (preferably 1 second) in Amazon Alexa without using SSML? Perhaps there is a trick I can do with the Outputspeech.Text and I just don't know it.
Below, I am saying "Here are works of art by {artist name}" but the name and the start of the works of art become mixed together - in spite of the period - so I end up with things like "Here are the works of art by Pablo Picasso Harlequin..."
I am using C# and my own https endpoint, not AWS Lambda.
Any suggestions? Otherwise I will add it as SSML. Thanks.
var output = new StringBuilder();
var outputCard = new StringBuilder();
string m_location;
string m_current_location;
string m_artist = dt_artist.Rows[0]["DisplayName"].ToString();
output.Append("here are works of art for " + m_artist + ". ");
outputCard.Append("Here are works of art for " + m_artist + ".\n\n");
foreach (DataRow dr in dt_artist_objs.Rows)
{
m_current_location = dr["CurrentLocation"].ToString();
if (m_current_location == " ")
{
m_location = "The location is not available.";
}
else
{
m_location = "It is located on the " + m_current_location;
}
output.Append(dr["Title"].ToString() + " is a " + dr["Classification"].ToString() + ". The medium is " + dr["Medium"].ToString() + ". " + m_location);
outputCard.Append(dr["Title"].ToString() + ", " + dr["Dated"].ToString() + " is a " + dr["Classification"].ToString() + ". The medium is " + dr["Medium"].ToString() + ". " + dr["Creditline"].ToString() + ". " + m_location + ".\n"); // It is located on the " + dr["CurrentLocation"].ToString());
}
sql_conn_data.Close();
response.Response.OutputSpeech.Text = output.ToString();
response.Response.Card.Title = "Art";
response.Response.Card.Type = "Standard";
response.Response.Card.Text = outputCard.ToString();
response.Response.ShouldEndSession = true;
return response;
UPDATE
OK. Ended up going the SSML route which looks like this:
var output = new StringBuilder();
var outputCard = new StringBuilder();
string m_location;
string m_current_location;
string m_location_card;
string m_artist = dt_artist.Rows[0]["DisplayName"].ToString();
output.Append("<speak>");
output.Append("here are works of art for " + m_artist + ". <break time='1s'/> ");
outputCard.Append("Here are works of art for " + m_artist + ".\n\n");
foreach (DataRow dr in dt_artist_objs.Rows)
{
m_current_location = dr["CurrentLocation"].ToString();
if (m_current_location == " ")
{
m_location = "The location is not available. <break time='1s' />";
m_location_card = "The location is not available. ";
}
else
{
m_location = "It is located on the " + m_current_location + "<break time = '1s' />";
m_location_card = "It is located on the " + m_current_location;
}
output.Append(dr["Title"].ToString() + " is a " + dr["Classification"].ToString() + ". The medium is " + dr["Medium"].ToString() + ". " + m_location);
outputCard.Append(dr["Title"].ToString() + ", " + dr["Dated"].ToString() + " is a " + dr["Classification"].ToString() + ". The medium is " + dr["Medium"].ToString() + ". " + dr["Creditline"].ToString() + ". " + m_location_card + ". \n");
}
output.Append("</speak>");
sql_conn_data.Close();
response.Response.OutputSpeech.Ssml = output.ToString();
response.Response.OutputSpeech.Type = "SSML";
response.Response.Card.Title = "Art";
response.Response.Card.Type = "Standard";
response.Response.Card.Text = outputCard.ToString();
response.Response.ShouldEndSession = true;
return response;
}
There is not a way to introduce a pause in Alexa without SSML. You will need to build the ssml string and return it back to Alexa using the pause, or the cadence strings.

send List<> values with Body in mail - SMTP server

I have to send mail through SMTP server. I can able to send it with single values. But, I want to send List<> values as message body in table format or some other structure. I include my code following :
MailMessage mailObj = new MailMessage("abc#gmail.com", "xyz#gmail.com", "Reg : send mail",
"Emp Name :" + "Emp1" + Environment.NewLine + "Date From : " + Mon + "Date To : " + Fri);
SmtpClient SMTPServer = new SmtpClient("smtp.gmail.com", ***);
SMTPServer.Host = "smtp.gmail.com";
SMTPServer.EnableSsl = true;
SMTPServer.Timeout = 200000;
SMTPServer.Credentials = new System.Net.NetworkCredential("asd#gmail.com", "******");
SMTPServer.Send(mailObj);
I have my list of values as follows :
List<TimesheetValues> mailBody = new SampleDAO().GetDataForMail(dt1, satDate);
How can I include this List<> values with body and send?
I try with following :
List<TimesheetValues> Msg = new List<TimesheetValues>(); string strMsg = ""; int n=1;
foreach(var item in mailBody)
{
strMsg = strMsg + "<table><tr><td>" + n + "</td><td>" + item.Project_Name + "</td><td>" + item.Task_Name + "</td><td>" + item.Notes + "</td><td>" + item.Hrs_Worked + "</td></tr></table>";
n++;
}
strMsg = strMsg + "</br>";
MailMessage mailObj = new MailMessage("abc123#gmail.com", "xyz123#gmail.com", "Reg : Timesheet",
"Emp Name :" + "Emp1" + Environment.NewLine + "Date From : " + Mon + "Date To : " + Fri);
mailObj.Body = "Emp Name : " + "Emp1" + Environment.NewLine + "Date From : " + Date2 + " Date To : " + Date6 + Environment.NewLine + strMsg;
Now I get all records but i have tr td tags with in records and not display as a table. How can i acheive this ?
can anyone help to overcome this..
Thanks in advance...
If you want to read the string into a list again, serialize is better.
using Newtonsoft.Json;
var json = JsonConvert.SerializeObject(yourList);
mailObj.Body = json;
You can also deserialize in the other side:
List<string> g = JsonConvert.DeserializeObject<List<string>>(body);
I try with this following :
string strMsg = ""
strMsg = timsheetMail + " <table style=\"border-collapse:collapse; text-align:center;\" border=\"1\"><tr><td> Date </td><td> Working Hours </td><td> Task </td><td>Comments</td></tr>";
List<TimesheetValues> Msg = new List<TimesheetValues>(); int n=1;
foreach(var item in mailBody)
{
timesheetData = new TimesheetDetailModel();
timesheetData.Task_Id = matrix.Task_Id;
timesheetData.Hours = matrix.Hours;
//timesheetData.Date = matrix.Date.Date;
timesheetData.Date = matrix.Date.AddDays(1);
timesheetData.EmpId = Convert.ToInt32(Session["EmpId"].ToString());
timesheetData.Notes = matrix.Notes;
strMsg = strMsg+ " <tr><td> " + timesheetData.Date.ToString("dd/MM/yyyy") + "</td><td>" + timesheetData.Hours + "</td><td>" + task + "</td><td>" + timesheetData.Notes + "</td></tr>";
n++;
}
strMsg = strMsg + "</table>";
Its working good now..

how to know whether compatibility is on or off using asp.net(c#)

I have to do if user's browser compatibility is on then need to show message to user that your browser's compatibility is on.
I have searched this a lot on google but yet not found a proper answer.
I have tried below code but HttpContext.Current.Request.UserAgent always contains MSIE 7.0
string isOn = string.Empty;
if (HttpContext.Current.Request.UserAgent.IndexOf("MSIE 7.0") > -1)
{
isOn = "IE8 Compatibility View";`
}
else
{
isOn = "IE8";
}
}
You may try like this
if (Request.Browser.Type.ToUpper().Contains("IE"))
{
if (Request.Browser.MajorVersion < 7)
{
//Show the message here
}
...
}
else if (Request.Browser.Type.Contains("Firefox"))
{
//code to show message
}
else if (Request.Browser.Type.Contains("Chrome"))
{
//code to show message
}
Also check this MSDN which has its own way of detecting the browser
Query the Browser property, which contains an HttpBrowserCapabilities
object. This object gets information from the browser or client device
during an HTTP request, telling your application the type and level of
support the browser or client device offers. The object in turn
exposes information about browser capabilities using strongly typed
properties and a generic name-value dictionary.
private void Button1_Click(object sender, System.EventArgs e)
{
System.Web.HttpBrowserCapabilities browser = Request.Browser;
string s = "Browser Capabilities\n"
+ "Type = " + browser.Type + "\n"
+ "Name = " + browser.Browser + "\n"
+ "Version = " + browser.Version + "\n"
+ "Major Version = " + browser.MajorVersion + "\n"
+ "Minor Version = " + browser.MinorVersion + "\n"
+ "Platform = " + browser.Platform + "\n"
+ "Is Beta = " + browser.Beta + "\n"
+ "Is Crawler = " + browser.Crawler + "\n"
+ "Is AOL = " + browser.AOL + "\n"
+ "Is Win16 = " + browser.Win16 + "\n"
+ "Is Win32 = " + browser.Win32 + "\n"
+ "Supports Frames = " + browser.Frames + "\n"
+ "Supports Tables = " + browser.Tables + "\n"
+ "Supports Cookies = " + browser.Cookies + "\n"
+ "Supports VBScript = " + browser.VBScript + "\n"
+ "Supports JavaScript = " +
browser.EcmaScriptVersion.ToString() + "\n"
+ "Supports Java Applets = " + browser.JavaApplets + "\n"
+ "Supports ActiveX Controls = " + browser.ActiveXControls
+ "\n"
+ "Supports JavaScript Version = " +
browser["JavaScriptVersion"] + "\n";
TextBox1.Text = s;
}

Browser detection

I need to separate IE and FF browsers from others
it's a pseudo-code :
If (CurrentBrowser == IE(6+) or FF(2+) )
{
...
}
else
{
...
}
in protected void Page_Load() event (think so)
if ((Request.Browser.Type == "IE") || (Request.Browser.Type == "FF"))
{
WebMsgBox.Show("1111");
}
no effects :-/ what is IE and FF types?
if (Request.Browser.Type.Contains("Firefox")) // replace with your check
{
...
}
else if (Request.Browser.Type.ToUpper().Contains("IE")) // replace with your check
{
if (Request.Browser.MajorVersion < 7)
{
DoSomething();
}
...
}
else { }
Here's a way you can request info about the browser being used, you can use this to do your if statement
System.Web.HttpBrowserCapabilities browser = Request.Browser;
string s = "Browser Capabilities\n"
+ "Type = " + browser.Type + "\n"
+ "Name = " + browser.Browser + "\n"
+ "Version = " + browser.Version + "\n"
+ "Major Version = " + browser.MajorVersion + "\n"
+ "Minor Version = " + browser.MinorVersion + "\n"
+ "Platform = " + browser.Platform + "\n"
+ "Is Beta = " + browser.Beta + "\n"
+ "Is Crawler = " + browser.Crawler + "\n"
+ "Is AOL = " + browser.AOL + "\n"
+ "Is Win16 = " + browser.Win16 + "\n"
+ "Is Win32 = " + browser.Win32 + "\n"
+ "Supports Frames = " + browser.Frames + "\n"
+ "Supports Tables = " + browser.Tables + "\n"
+ "Supports Cookies = " + browser.Cookies + "\n"
+ "Supports VBScript = " + browser.VBScript + "\n"
+ "Supports JavaScript = " +
browser.EcmaScriptVersion.ToString() + "\n"
+ "Supports Java Applets = " + browser.JavaApplets + "\n"
+ "Supports ActiveX Controls = " + browser.ActiveXControls
+ "\n";
MSDN Article
Try the below code
HttpRequest req = System.Web.HttpContext.Current.Request
string browserName = req.Browser.Browser;
private void BindDataBInfo()
{
System.Web.HttpBrowserCapabilities browser = Request.Browser;
Literal1.Text = "<table border=\"1\" cellspacing=\"3\" cellpadding=\"2\">";
foreach (string key in browser.Capabilities.Keys)
{
Literal1.Text += "<tr><td>" + key + "</td><td>" + browser[key] + "</tr>";
}
Literal1.Text += "</table>";
browser = null;
}
I would not advise hacking browser-specific things manually with JS. Either use a javascript library like "prototype" or "jquery", which will handle all the specific issues transparently.
Or use these libs to determine the browser type if you really must.
Also see Browser & version in prototype library?
For browser compatibility you can use this code. This method returns browser name and version :
private string GetBrowserNameWithVersion
{
var userAgent = Request.UserAgent;
var browserWithVersion = "";
if (userAgent.IndexOf("Edge") > -1)
{
//Edge
browserWithVersion = "Edge Browser Version : " + userAgent.Split(new string[] { "Edge/" }, StringSplitOptions.None)[1].Split('.')[0];
}
else if (userAgent.IndexOf("Chrome") > -1)
{
//Chrome
browserWithVersion = "Chrome Browser Version : " + userAgent.Split(new string[] { "Chrome/" }, StringSplitOptions.None)[1].Split('.')[0];
}
else if (userAgent.IndexOf("Safari") > -1)
{
//Safari
browserWithVersion = "Safari Browser Version : " + userAgent.Split(new string[] { "Safari/" }, StringSplitOptions.None)[1].Split('.')[0];
}
else if (userAgent.IndexOf("Firefox") > -1)
{
//Firefox
browserWithVersion = "Firefox Browser Version : " + userAgent.Split(new string[] { "Firefox/" }, StringSplitOptions.None)[1].Split('.')[0];
}
else if (userAgent.IndexOf("rv") > -1)
{
//IE11
browserWithVersion = "Internet Explorer Browser Version : " + userAgent.Split(new string[] { "rv:" }, StringSplitOptions.None)[1].Split('.')[0];
}
else if (userAgent.IndexOf("MSIE") > -1)
{
//IE6-10
browserWithVersion = "Internet Explorer Browser Version : " + userAgent.Split(new string[] { "MSIE" }, StringSplitOptions.None)[1].Split('.')[0];
}
else if (userAgent.IndexOf("Other") > -1)
{
//Other
browserWithVersion = "Other Browser Version : " + userAgent.Split(new string[] { "Other" }, StringSplitOptions.None)[1].Split('.')[0];
}
return browserWithVersion;
}
I tried and found the solution for the same
public static string GetBrowserDetails()
{
string BrowserDetails = HttpContext.Current.Request.Browser.Browser + " - " + HttpContext.Current.Request.Browser.Version + "; Operating System : " + HttpContext.Current.Request.Browser.Platform;
return BrowserDetails;
}
OUTPUT :
Chrome - 88.0; Operating System : WinNT
use from
Request.Browser
this link will help you :
Detect the browser using ASP.NET and C#

Categories