does not contain a definition for 'Form' ? Outlook Add-in - c#

Recently I' am working on an add-in with c# for Outlook 2010 in Visual Studio 2012 . I developed a custom Form region which contains simple textboxes and a button. In Button_click method I' am taking an error (error Code : error CS0117) and I couldn't figure out why ? I am really new of this environment :) any way here it is my problem. (if you need more code to solve just let me know)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Office = Microsoft.Office.Core;
using Outlook = Microsoft.Office.Interop.Outlook;
using MSForms = Microsoft.Vbe.Interop.Forms;
.
.
.
private void button1_Click(object sender, EventArgs e)
{
// save button
MSForms.UserForm userForm = (MSForms.UserForm) FormRegion1.Form;
MSForms.Controls formControls = userForm.Controls;
Outlook.OlkTextBox ad =
(Outlook.OlkTextBox)formControls.Item("ad");
string cariad = ad.Text;
Outlook.OlkTextBox adres =
(Outlook.OlkTextBox)formControls.Item("adres");
string cariadres = adres.Text;
Outlook.OlkTextBox vergid =
(Outlook.OlkTextBox)formControls.Item("vergid");
string carivergid = vergid.Text;
.
.
.
*MSForms.UserForm userForm = (MSForms.UserForm) in line FormRegion1.Form "Form" is not recognized. And error says
" error CS0117: 'OutlookAddIn2.FormRegion1' does not contain a
definition for 'Form'".
Thanks a lot.

I solved that problem with not using Forms:) Simply, I just typed
this.TextBoxName.Text;
to reach the text of the textBox. Thanks any way.

Related

Can't get Request.QueryString or similar methods to work

To my shame, because I can barely C#, I cannot manage to read the parameters from the URL.
I run a C# cgi executable on my IIS 7 as an application. The url that calls the executable is as below:
https://server/cgi/showEmail/showEmail.exe?email=john#gmail.com
The code starts as below:
using System;
using System.Web; // <---- isn't this for Request.QueryString ?
using System.Web.UI;
using System.Collections;
using System.Collections.Specialized;
using System.Data.SqlClient;
class showEmail
{
static void Main(string[] args)
{
Console.WriteLine("\r\n\r\n");
Console.WriteLine("<h1>Test</h1>");
try
{
Now, if I use the code below, the program compiles, but gives this a null exception when executed in the browser:
string email = HttpContext.Current.Request.QueryString["email"];
System.NullReferenceException: Object reference not set to an instance of an object. at showEmail.Main(String[] args)
and if I use this code below, the fun stops already at the compiler, who gives a "current context" exception:
string email = Request.QueryString["email"];
error CS0103: The name 'Request' does not exist in the current context
...
Am I missing something elementary that is required for the executable to see the url parameters?
Edit: I have looked through sof and many other places, but so far have not been able to connect the dots on this issue.
HttpContext.Current.Request is not available in console appliaction.
Use args parameter to receive query string parameter, like I am doing below.
for (int i = 0; i < args.Length; i++)
{
Console.WriteLine("parameter[{0}] is [{1}]", i, args[i]);
}
You might need to use below code to extract parameters from url received in args.
var url=args[0];
var queryString = url.Substring(url.IndexOf('?')).Split('#')[0]
System.Web.HttpUtility.ParseQueryString(queryString)
This is how I got it to work finally, please keep in mind this is not professionally validated code, more of try and try and try...
using System;
using System.Data;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string email = Request.QueryString["email"] ?? "null";
if (email != "null") {

Client web service for english dictionary

I use this service to translate English word:
http://services.aonaware.com/DictService/DictService.asmx?op=Define
I add this link to my windows Form application by click right on References -> Add Service Reference -> and best the URL of service in Address field.
then I write this code:
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 هجوم_الكسر_الأعمى.ServiceReference1;
namespace هجوم_الكسر_الأعمى
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//Definition a = new Definition();
WordDefinition sv = new WordDefinition();
sv.Word="Go";
string b= sv.Word;
textBox1.Text = b; ;
}
}
}
The problem is that I don't have the result, I have the same world witch I write it "Go"?
You're not doing anything here, you're just creating an instance of WordDefinition locally that you set to the word you're trying to search for.
You need to invoke the service call, for example..
using (var dictionaryService = new ServiceReference1.DictServiceSoapClient("DictServiceSoap"))
{
var definition = dictionaryService.Define("Programming");
Console.WriteLine(definition.Definitions.First().WordDefinition);
}
I am not sure if I understand you, but if you would like to have result from sv.Word method I think you shloud try to check if there is some method with Result, for example: sv.WordResult and it will add event handler to this.

C# MailMessage Display email instead of Send

I have been following the example below to send email using C# code with success:
MSDN Mail Message
However, I would like the code to display the composed email message on user machine, so that user can have a final check before hitting send button on outlook.
In the VBA world, I can use mail.Display in place of mail.Send.
Can anyone provide some advice to achieve that in C#?
Thanks.
How about this...
private void btnEmail_Click(object sender, EventArgs e)
{
string command = "mailto:somebody#domain.com?subject=The Subject&bcc=another#codegaim.com&body=Hi,I found this website and thought you might like it http://www.geocities.com/wowhtml/";
Process.Start(command);
}
Found a great solution to my problem i.e. to use Microsoft Office Interop Outlook instead of System.Net.MailMessage
followed this:
How to send a mail using Microsoft.Office.Interop.Outlook.MailItem by specifying the From Address
//using Microsoft.Office.Interop.Outlook;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Outlook;
namespace ConsoleApplication1
{
using Outlook = Microsoft.Office.Interop.Outlook;
public static class Program
{
static void Main(string[] args)
{
SendUsingAccountExample();
}
private static void SendUsingAccountExample()
{
var application = new Application();
var mail = (_MailItem)application.CreateItem(OlItemType.olMailItem);
mail.Body = "Hello";
mail.Subject = "Good Bye";
mail.To = "hello#google.com";
// Next 2 lines are optional. if not specified, the default account will be used
Outlook.Account account = Application.Session.Accounts["MyOtherAccount"];
mail.SendUsingAccount = account;
mail.Display(false); // To Display
//mail.Send(); // To Send
}
}
}

How to retrieve a value/url?

I apologize in advance as I have not used visual studio much before and I used a template for the most part to create the code below. But I really need some help.
I'm trying to generate a report from a system using visual studio through an API. The code below works and doesn't give me any errors when debugging, but I need to find a way to retrieve the reportURL variable at the end (the report is generated with the specifications below and I should receive a URL to download the report). I am building as a windows application.
Is there anything like console.log or console.writeline in visual studio I can use? Or can I output it to a textbox of some kind? (Again, I am building as a windows form and not a console application). Can anyone help me figure out some kind of code I can use to retrieve the URL based on what is provided below? (Please be detailed if possible as I am still getting used to the program). Thanks!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ReportsApplication2
{
using ServiceReference1;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ReportExecutionServiceClient client = new ReportExecutionServiceClient();
ReportSubmission submissionRequest = new ReportSubmission();
ReportSubmissionResponse submissionResponse = new ReportSubmissionResponse();
PollReportResponse pollResponse = new PollReportResponse();
WebMediaReportRequest webRepReq = new WebMediaReportRequest();
UserCredentials userCredentials = new UserCredentials();
DateFilter dateFilter = new DateFilter();
userCredentials.UserName = "xxxxx";
userCredentials.Password = "xxxxx";
submissionRequest.UserCredentials = userCredentials;
submissionRequest.DeveloperToken = "xxxxxx";
dateFilter.DateFilterType = DateFilterType.Total;
dateFilter.RelativeDateRange = RelativeDateRange.LastMonth;
webRepReq.Columns = new WebMediaReportColumn[2] { WebMediaReportColumn.MediaPlanName, WebMediaReportColumn.Impressions };
List<WebMediaFilter> webRepFilterList = new List<WebMediaFilter>();
WebMediaFilter webRepFilter = new WebMediaFilter();
webRepFilter.Column = WebMediaReportFilter.ClientGUID;
webRepFilter.Values = new string[1] {"xxxxxx"};
webRepFilterList.Add(webRepFilter);
webRepFilter = new WebMediaFilter();
webRepFilter.Column = WebMediaReportFilter.BuyGUID;
webRepFilter.Values = new string[1] { "xxxxxxxx" };
webRepFilterList.Add(webRepFilter);
webRepReq.ReportName = "test";
webRepReq.Filters = webRepFilterList.ToArray();
webRepReq.Format = FormatType.CSV;
webRepReq.DateFilter = dateFilter;
submissionRequest.ReportRequest = webRepReq;
submissionResponse = client.SubmitReport(submissionRequest);
string reportURL = string.Empty;
do { // Loop until report complete or failed
PollReportRequest pollRequest = new PollReportRequest();
pollRequest.DeveloperToken = "xxxxxxx";
pollRequest.UserCredentials = userCredentials;
pollRequest.ReportId = submissionResponse.ReportId;
pollResponse = client.PollReport(pollRequest);
reportURL = pollResponse.Url;
} while ((pollResponse.Status != ReportStatus.Failed) || ((pollResponse.Status != ReportStatus.Complete)));
}//end
You actually already know what you need. The Console.WriteLine method will write anything you want to the Console.
While you are debugging in Visual Studio, you can view the Console window by turning on the "Output" window. You can find it in the View menu
Even though you are writing a WinForms application, this will still write to the Output window while debugging. It can be an effective tool when debugging, but of course, when you actually publish the application, the command will be meaningless since you won't have a Console to write too.
If you are using Winforms, the simplest approach to show messages is MessageBox.Show():
MessageBox.Show(reportURL);
It will block the program until you click Ok.
http://msdn.microsoft.com/en-us/library/0x49kd7z(v=vs.110).aspx
If you want to output debug info during development, then Console.WriteLine() will work:
Console.WriteLine(reportURL);
It will show in the Output window. If you don't see that, go to the main menu in Visual Studio and select View -> Output, while in debug mode.
Console.WriteLine() will only work within Visual Studio for a Winforms app; after deploying it, the console window doesn't show, so you'll need to show it via a GUI method.
If you need to output it at runtime, either drag a TextBox from the toolbox onto your main form, and do:
textBox.Text = reportURL;
If you can't show it on the main form, then you may want to create your own custom message dialog that includes a TextBox and show it with ShowDialog()
The simplest solution is to use Console.WriteLine.

SharpPcap issue

This is my first time to use SharpPcap library.
I created new project with VC# 2008 and I added SharpPcap as a reference to my project.
I post a sample code to get interface of my pc but I'm getting this error:
Error 1 The type or namespace name 'PcapDeviceList' could not be found (are you missing a using directive or an assembly reference?) C:\Users\Ali\Documents\Visual Studio 2008\Projects\Pcap\Pcap\Form1.cs 28 13 Pcap
please advice to solve this problem.
here is my code:
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 SharpPcap;
using SharpPcap.Packets;
using SharpPcap.Protocols;
using SharpPcap.Util;
namespace Pcap
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
/* Retrieve the device list */
PcapDeviceList devices = SharpPcap.GetAllDevices();
/*If no device exists, print error */
if (devices.Count < 1)
{
Console.WriteLine("No device found on this machine");
return;
}
int i = 0;
/* Scan the list printing every entry */
foreach (PcapDevice dev in devices)
{
/* Description */
label1.Text = "{0}) {1}" + i + dev.PcapDescription +"\n"+
/* Name */
"\tName:\t{0}" + dev.PcapName+"\n"+
/* IP Address */
"\tIP Address: \t\t{0}"+ dev.PcapIpAddress+"\n"+
/* Is Loopback */
"\tLoopback: \t\t{0}"+ dev.PcapLoopback;
i++;
}
}
}
}
What version of SharpPcap are you using?
I'm moving a small in-work project from 2.1.1 to 2.4.1, and parts of the library have changed significantly. I struggled with this myself just a bit ago.
LivePcapDeviceList devices = LivePcapDeviceList.Instance;
foreach (LivePcapDevice device in devices)
{
// Do stuff here...
}
Sorry about the API changes. I'm still trying to get the API named properly. As of v3.0 the API is considerably cleaner and should be a bit more static going forward. If you have any trouble post on the devel list or sf forums and you'll get a quick reply.
Chris
SharpPcap maintainer/author
http://sharppcap.sf.net
Try using Pcap.Net. It's a clear WinPcap wrapper in .NET.

Categories