//Copyright © 2009 by
JMethod.com. All rights reserved.
using System;
using System.Drawing;
using
System.Collections;
using
System.ComponentModel;
using
System.Windows.Forms;
using System.Data;
using csdialup;
namespace csdialsample {
/// <summary>
/// Summary description for WinForm.
/// </summary>
public class CSDialSample
: System.Windows.Forms.Form {
/// <summary>
/// Required designer variable.
/// </summary>
private
System.ComponentModel.Container components = null;
private
System.Windows.Forms.Button createModemEntry;
private
System.Windows.Forms.Button createEntry;
private
System.Windows.Forms.Button editEntry;
private
System.Windows.Forms.Button renameEntry;
private
System.Windows.Forms.Button deleteEntry;
private
System.Windows.Forms.Button updateEntry;
private
System.Windows.Forms.RichTextBox stateBox;
private
System.Windows.Forms.Button connect;
private
System.Windows.Forms.Button hangup;
private
System.Windows.Forms.ListBox entryListBox;
private
CSDial csd = null;
public
void SampleNotify(string
entry, System.Int32 state, System.UInt32 error) {
string mes = null;
if (state == CSDial.DIAL_TIME_OUT) {
mes = entry + ": state[" +
state + "] --> DIAL TIMEOUT";
} else {
mes = entry + ": state[" +
state + "] --> " + CSDial.getStateString(state);
} // if
if (error != 0) {
// if there is error
mes = entry + ": error[" +
error + "] --> " +
CSDial.getErrorMessage((int)error);
} // if
stateBox.AppendText(mes + "\r\n");
} // SampleNotify
private
CSDial.Notify snf = null;
public
CSDialSample() {
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent
call
//
// --------------------- Center the window
-----------------------
this.Visible = true;
int wScreen = Screen.AllScreens[0].WorkingArea.Width;
int hScreen =
Screen.AllScreens[0].WorkingArea.Height;
this.Location = new
Point((wScreen - this.Width) / 2,
(hScreen - this.Height) / 2);
// ---------------------------------------------------------------
try {
snf = new CSDial.Notify(SampleNotify);
this.csd = new
CSDial(snf);
} catch (Exception
ex) {
System.Windows.Forms.MessageBox.Show("Error:
" + ex.ToString());
} // try
} // CSDialSample
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected
override void
Dispose(bool disposing) {
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form
Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private
void InitializeComponent() {
this.createModemEntry = new
System.Windows.Forms.Button();
this.createEntry = new
System.Windows.Forms.Button();
this.editEntry = new
System.Windows.Forms.Button();
this.renameEntry = new
System.Windows.Forms.Button();
this.deleteEntry = new
System.Windows.Forms.Button();
this.updateEntry = new
System.Windows.Forms.Button();
this.stateBox = new
System.Windows.Forms.RichTextBox();
this.connect = new
System.Windows.Forms.Button();
this.hangup = new
System.Windows.Forms.Button();
this.entryListBox = new
System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// createModemEntry
//
this.createModemEntry.Location = new System.Drawing.Point(313, 12);
this.createModemEntry.Name = "createModemEntry";
this.createModemEntry.Size = new
System.Drawing.Size(116, 22);
this.createModemEntry.TabIndex = 1;
this.createModemEntry.Text = "Create
Modem Entry";
this.createModemEntry.Click += new System.EventHandler(this.createModemEntry_Click);
//
// createEntry
//
this.createEntry.Location = new
System.Drawing.Point(313, 40);
this.createEntry.Name = "createEntry";
this.createEntry.Size = new
System.Drawing.Size(116, 22);
this.createEntry.TabIndex = 2;
this.createEntry.Text = "Create
Entry";
this.createEntry.Click += new
System.EventHandler(this.createEntry_Click);
//
// editEntry
//
this.editEntry.Location = new
System.Drawing.Point(313, 68);
this.editEntry.Name = "editEntry";
this.editEntry.Size = new
System.Drawing.Size(116, 22);
this.editEntry.TabIndex = 3;
this.editEntry.Text = "Edit
Entry";
this.editEntry.Click += new
System.EventHandler(this.editEntry_Click);
//
// renameEntry
//
this.renameEntry.Location = new
System.Drawing.Point(313, 96);
this.renameEntry.Name = "renameEntry";
this.renameEntry.Size = new
System.Drawing.Size(116, 22);
this.renameEntry.TabIndex = 4;
this.renameEntry.Text = "Rename
Entry";
this.renameEntry.Click += new
System.EventHandler(this.renameEntry_Click);
//
// deleteEntry
//
this.deleteEntry.Location = new
System.Drawing.Point(313, 124);
this.deleteEntry.Name = "deleteEntry";
this.deleteEntry.Size = new
System.Drawing.Size(116, 22);
this.deleteEntry.TabIndex = 5;
this.deleteEntry.Text = "Delete
Entry";
this.deleteEntry.Click += new
System.EventHandler(this.deleteEntry_Click);
//
// updateEntry
//
this.updateEntry.Location = new
System.Drawing.Point(313, 163);
this.updateEntry.Name = "updateEntry";
this.updateEntry.Size = new
System.Drawing.Size(116, 22);
this.updateEntry.TabIndex = 6;
this.updateEntry.Text = "Update
Entries";
this.updateEntry.Click += new
System.EventHandler(this.updateEntry_Click);
//
// stateBox
//
this.stateBox.Location = new
System.Drawing.Point(12, 163);
this.stateBox.Name = "stateBox";
this.stateBox.ReadOnly = true;
this.stateBox.Size = new
System.Drawing.Size(288, 137);
this.stateBox.TabIndex = 7;
this.stateBox.Text = "";
//
// connect
//
this.connect.Location = new
System.Drawing.Point(12, 312);
this.connect.Name = "connect";
this.connect.Size = new
System.Drawing.Size(116, 22);
this.connect.TabIndex = 8;
this.connect.Text = "Connect";
this.connect.Click += new
System.EventHandler(this.connect_Click);
//
// hangup
//
this.hangup.Location = new
System.Drawing.Point(140, 312);
this.hangup.Name = "hangup";
this.hangup.Size = new
System.Drawing.Size(116, 22);
this.hangup.TabIndex = 9;
this.hangup.Text = "Hang
Up";
this.hangup.Click += new
System.EventHandler(this.hangup_Click);
//
// entryListBox
//
this.entryListBox.Location = new
System.Drawing.Point(12, 12);
this.entryListBox.Name = "entryListBox";
this.entryListBox.SelectionMode =
System.Windows.Forms.SelectionMode.MultiExtended;
this.entryListBox.Size = new
System.Drawing.Size(288, 134);
this.entryListBox.TabIndex = 10;
//
// CSDialSample
//
this.ClientSize = new
System.Drawing.Size(441, 346);
this.Controls.Add(this.entryListBox);
this.Controls.Add(this.hangup);
this.Controls.Add(this.connect);
this.Controls.Add(this.stateBox);
this.Controls.Add(this.updateEntry);
this.Controls.Add(this.deleteEntry);
this.Controls.Add(this.renameEntry);
this.Controls.Add(this.editEntry);
this.Controls.Add(this.createEntry);
this.Controls.Add(this.createModemEntry);
this.Name = "CSDialUpSample";
this.Text = "CSDialUp
API Sample";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static
void Main() {
try {
CSDialSample csds = new CSDialSample();
csds.updateList();
Application.Run(csds);
} catch (Exception
ex) {
MessageBox.Show("CSDialUp API Sample:
Main: " + ex.ToString(),
"Message", MessageBoxButtons.OK,
MessageBoxIcon.Information
);
} // try
} // Main
private
void createModemEntry_Click(object sender, System.EventArgs
e) {
try {
if (csd.getModemDeviceNames() == null || csd.getModemDeviceNames().Length == 0) {
MessageBox.Show(
"There are no modem devices",
"Message",
MessageBoxButtons.OK,
MessageBoxIcon.Information
);
return;
} // if
ModemEntryDialog modemDialog = new
ModemEntryDialog(csd);
modemDialog.Visible = true;
modemDialog.SetDesktopLocation(this.Location.X + 50, this.Location.Y
+ 50);
modemDialog.updateModemList();
modemDialog.Visible = false;
modemDialog.ShowDialog();
modemDialog.Dispose();
this.updateList();
} catch (Exception
ex) {
MessageBox.Show(
"CSDialUp API Sample:
createModemEntry_Click: " + ex.ToString(),
"Message",
MessageBoxButtons.OK, MessageBoxIcon.Information
);
} // try
updateList();
} // createModemEntry_Click
private
void createEntry_Click(object
sender, System.EventArgs e) {
try {
this.csd.showCreateEntryDialog();
} catch (Exception
ex) {
MessageBox.Show(
"CSDialUp API Sample:
createEntry_Click: " + ex.ToString(),
"Message",
MessageBoxButtons.OK, MessageBoxIcon.Information
);
} // try
updateList();
} // createEntry_Click
private
void editEntry_Click(object
sender, System.EventArgs e) {
try {
int count = 0;
while (count < this.entryListBox.SelectedIndices.Count) {
string entry = this.entryListBox.SelectedItems[count++].ToString();
this.csd.showEditEntryDialog(entry);
} // while
} catch (Exception
ex) {
MessageBox.Show(
"CSDialUp API Sample: editEntry_Click:
" + ex.ToString(),
"Message",
MessageBoxButtons.OK, MessageBoxIcon.Information
);
} // try
updateList();
} // editEntry_Click
private
void renameEntry_Click(object
sender, System.EventArgs e) {
try {
int count = 0;
while (count < this.entryListBox.SelectedIndices.Count) {
string entry = this.entryListBox.SelectedItems[count++].ToString();
string newStr =
InputDialog.showInputDialog(" New Entry
Name", entry, "Ok", "Cancel", this);
if (newStr != null)
{
this.csd.renameEntry(entry, newStr);
} // if
} // while
} catch (Exception
ex) {
MessageBox.Show(
"CSDialUp API Sample:
renameEntry_Click: " + ex.ToString(),
"Message", MessageBoxButtons.OK,
MessageBoxIcon.Information
);
} // try
updateList();
} // renameEntry_Click
private
void deleteEntry_Click(object
sender, System.EventArgs e) {
try {
int count = 0;
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result;
while (count < this.entryListBox.SelectedIndices.Count) {
string entry = this.entryListBox.SelectedItems[count++].ToString();
result = MessageBox.Show(this, "Delete " + entry + " ?", "Message",
buttons,
MessageBoxIcon.Question, MessageBoxDefaultButton.Button1
);
if (result == DialogResult.Yes) {
this.csd.deleteEntry(entry);
} // if
} // while
} catch (Exception
ex) {
MessageBox.Show(
"CSDialUp API Sample: deleteEntry_Click:
" + ex.ToString(),
"Message",
MessageBoxButtons.OK, MessageBoxIcon.Information
);
} // try
updateList();
} // deleteEntry_Click
private
void updateEntry_Click(object
sender, System.EventArgs e) {
try {
updateList();
} catch (Exception
ex) {
MessageBox.Show(
"CSDialUp API Sample:
updateEntry_Click: " + ex.ToString(),
"Message",
MessageBoxButtons.OK, MessageBoxIcon.Information
);
} // try
} // updateEntry_Click
private
void connect_Click(object
sender, System.EventArgs e) {
try {
int savedIndex = this.entryListBox.SelectedIndex;
int count = 0;
while (count < this.entryListBox.SelectedIndices.Count) {
this.csd.dialAsyncProcess(
this.entryListBox.SelectedItems[count++].ToString(),
"vvv",
"vvv"
);
} // while
entryListBox.SetSelected(savedIndex, true);
} catch (Exception
ex) {
MessageBox.Show(
"CSDialUp API Sample: connect_Click:
" + ex.ToString(),
"Message",
MessageBoxButtons.OK, MessageBoxIcon.Information
);
} // try
} // connect_Click
private
void hangup_Click(object
sender, System.EventArgs e) {
try {
int savedIndex = this.entryListBox.SelectedIndex;
int count = 0;
while (count < this.entryListBox.SelectedIndices.Count) {
this.csd.hangUp(
this.entryListBox.SelectedItems[count++].ToString()
);
} // while
entryListBox.SetSelected(savedIndex, true);
} catch (Exception
ex) {
MessageBox.Show(
"CSDialUp
API Sample: hangup_Click: " + ex.ToString(),
"Message",
MessageBoxButtons.OK, MessageBoxIcon.Information
);
} // try
} // hangup_Click
public
void updateList() {
this.entryListBox.Items.Clear();
try {
string[] entries = this.csd.getEntries();
if (entries == null
|| entries.Length == 0) {
return;
} // if
foreach (string
s in entries) {
entryListBox.Items.Add(s);
} // for
entryListBox.EndUpdate();
} catch (Exception
ex) {
MessageBox.Show(
"CSDialUp API Sample: updateList:
" + ex.ToString(),
"Message",
MessageBoxButtons.OK, MessageBoxIcon.Information
);
} // try
this.entryListBox.SetSelected(0, true);
}
} // class
CSDialSample
//----------------------------------------------------
//-- Rename Dialog
-----------------------------------
//----------------------------------------------------
public class InputDialog
: System.Windows.Forms.Form {
System.Windows.Forms.TextBox txtUser;
System.Windows.Forms.Button btnOK;
System.Windows.Forms.Button btnCancel;
bool
okFlag = true;
public
InputDialog(
string title, string
defaultText,
string btnOkText, string
btnCancelText
) {
this.Text = "Rename
entry";
txtUser = new
System.Windows.Forms.TextBox();
txtUser.Location = new Point(30, 15);
txtUser.Size = new Size(250, 20);
txtUser.Text = defaultText;
txtUser.Name = "txtUser";
this.Controls.Add(txtUser);
//----------------------------------------
btnOK = new
System.Windows.Forms.Button();
btnOK.Location = new Point(
40,
(txtUser.Location.Y + txtUser.Size.Height + btnOK.Size.Height)
);
btnOK.Text = btnOkText;
btnOK.Name = btnOkText;
this.Controls.Add(btnOK);
//----------------------------------------
btnCancel = new System.Windows.Forms.Button();
btnCancel.Location = new Point(
(this.Size.Width - btnCancel.Size.Width)
- 40,
(txtUser.Location.Y + txtUser.Size.Height + btnOK.Size.Height)
);
btnCancel.Text = btnCancelText;
btnCancel.Name = btnCancelText;
this.Controls.Add(btnCancel);
this.Size = new Size(
this.Size.Width, btnCancel.Location.Y +
btnCancel.Size.Height + 60
);
btnCancel.Click += new System.EventHandler(btnCancelHandler);
btnOK.Click += new System.EventHandler(btnEventHandler);
} // InputDialog
private
void btnEventHandler(object
sender, System.EventArgs e) {
this.Hide();
} // btnEventHandler
private
void btnCancelHandler(object
sender, System.EventArgs e) {
okFlag = false;
this.Hide();
} // btnCancelHandler
public
static string
showInputDialog(
string
title, string defaultText, string btnOkText, string
btnCancelText,
Form parent
) {
InputDialog id = new InputDialog(
title, defaultText, btnOkText, btnCancelText
);
id.Visible = true;
id.SetDesktopLocation(parent.Location.X + 50, parent.Location.Y + 50);
id.Visible = false;
id.ShowDialog();
string t = null;
if (id.okFlag) {
t = id.txtUser.Text;
}
// if
id.Dispose();
return t;
} // showInputDialog
} // class InputDialog
class ModemEntryDialog : System.Windows.Forms.Form {
private
csdialup.CSDial csd = null;
public
ModemEntryDialog(csdialup.CSDial csd) {
this.csd = csd;
InitializeComponent();
} // ModemEntryDialog
private
void okButton_Click(object
sender, EventArgs e) {
try {
csd.createModemEntry(this.newModemEntryNameTB.Text,
this.modemListBox.SelectedItem.ToString(),
this.loginTB.Text,
this.passwordTB.Text,
this.phoneTB.Text);
} catch (Exception
ex) {
MessageBox.Show(
"ModemEntryDialog: okButton_Click:
" + ex.ToString(),
"Message",
MessageBoxButtons.OK, MessageBoxIcon.Information
);
} // try
this.Hide();
} // okButton_Click
private
void cancelButton_Click(object sender, EventArgs e) {
this.Hide();
} // cancelButton_Click
public
void updateModemList() {
this.modemListBox.Items.Clear();
try {
string[] names = csd.getModemDeviceNames();
if (names == null
|| names.Length == 0) {
return;
} // if
foreach (string
s in names) {
modemListBox.Items.Add(s);
} // for
modemListBox.EndUpdate();
this.modemListBox.SetSelected(0, true);
} catch (Exception
ex) {
MessageBox.Show(
"ModemEntryDialog: updateModemList:
" + ex.ToString(),
"Message",
MessageBoxButtons.OK, MessageBoxIcon.Information
);
} // try
} // updateModemList
/// <summary>
/// Required designer variable.
/// </summary>
private
System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param
name="disposing">true if managed
resources should be disposed; otherwise, false.</param>
protected
override void
Dispose(bool disposing) {
if (disposing && (components != null)) {
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form
Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private
void InitializeComponent() {
this.modemListBox = new
System.Windows.Forms.ListBox();
this.modemLabel = new
System.Windows.Forms.Label();
this.newModemEntryNameTB = new
System.Windows.Forms.TextBox();
this.newModemEntryNameLabel = new System.Windows.Forms.Label();
this.loginTB = new
System.Windows.Forms.TextBox();
this.loginLabel = new
System.Windows.Forms.Label();
this.passwordLabel = new
System.Windows.Forms.Label();
this.passwordTB = new
System.Windows.Forms.TextBox();
this.phoneLabel = new
System.Windows.Forms.Label();
this.phoneTB = new
System.Windows.Forms.TextBox();
this.okButton = new
System.Windows.Forms.Button();
this.cancelButton = new
System.Windows.Forms.Button();
this.SuspendLayout();
//
// modemListBox
//
this.modemListBox.BorderStyle =
System.Windows.Forms.BorderStyle.None;
this.modemListBox.Location = new
System.Drawing.Point(22, 30);
this.modemListBox.Name = "modemListBox";
this.modemListBox.Size = new
System.Drawing.Size(245, 39);
this.modemListBox.TabIndex = 0;
//
// modemLabel
//
this.modemLabel.AutoSize = true;
this.modemLabel.Location = new
System.Drawing.Point(18, 9);
this.modemLabel.Name = "modemLabel";
this.modemLabel.Size = new
System.Drawing.Size(80, 13);
this.modemLabel.TabIndex = 1;
this.modemLabel.Text = "Choose
modem";
//
// newModemEntryNameTB
//
this.newModemEntryNameTB.Location = new System.Drawing.Point(22, 100);
this.newModemEntryNameTB.Name = "newModemEntryNameTB";
this.newModemEntryNameTB.Size = new System.Drawing.Size(244, 20);
this.newModemEntryNameTB.TabIndex = 2;
//
// newModemEntryNameLabel
//
this.newModemEntryNameLabel.AutoSize = true;
this.newModemEntryNameLabel.Location = new System.Drawing.Point(18, 80);
this.newModemEntryNameLabel.Name = "newModemEntryNameLabel";
this.newModemEntryNameLabel.Size = new System.Drawing.Size(60, 13);
this.newModemEntryNameLabel.TabIndex = 3;
this.newModemEntryNameLabel.Text = "Entry name";
//
// loginTB
//
this.loginTB.Location = new
System.Drawing.Point(24, 149);
this.loginTB.Name = "loginTB";
this.loginTB.Size = new
System.Drawing.Size(244, 20);
this.loginTB.TabIndex = 4;
//
// loginLabel
//
this.loginLabel.AutoSize = true;
this.loginLabel.Location = new
System.Drawing.Point(20, 130);
this.loginLabel.Name = "loginLabel";
this.loginLabel.Size = new
System.Drawing.Size(33, 13);
this.loginLabel.TabIndex = 5;
this.loginLabel.Text = "Login";
//
// passwordLabel
//
this.passwordLabel.AutoSize = true;
this.passwordLabel.Location = new System.Drawing.Point(22, 181);
this.passwordLabel.Name = "passwordLabel";
this.passwordLabel.Size = new
System.Drawing.Size(53, 13);
this.passwordLabel.TabIndex = 7;
this.passwordLabel.Text = "Password";
//
// passwordTB
//
this.passwordTB.Location = new
System.Drawing.Point(26, 200);
this.passwordTB.Name = "passwordTB";
this.passwordTB.Size = new
System.Drawing.Size(244, 20);
this.passwordTB.TabIndex = 6;
//
// phoneLabel
//
this.phoneLabel.AutoSize = true;
this.phoneLabel.Location = new
System.Drawing.Point(22, 231);
this.phoneLabel.Name = "phoneLabel";
this.phoneLabel.Size = new
System.Drawing.Size(48, 13);
this.phoneLabel.TabIndex = 9;
this.phoneLabel.Text = "Phone
#";
//
// phoneTB
//
this.phoneTB.Location = new
System.Drawing.Point(26, 250);
this.phoneTB.Name = "phoneTB";
this.phoneTB.Size = new
System.Drawing.Size(244, 20);
this.phoneTB.TabIndex = 8;
//
// okButton
//
this.okButton.Location = new
System.Drawing.Point(26, 284);
this.okButton.Name = "okButton";
this.okButton.Size = new
System.Drawing.Size(98, 21);
this.okButton.TabIndex = 10;
this.okButton.Text = "OK";
this.okButton.Click += new
System.EventHandler(this.okButton_Click);
//
// cancelButton
//
this.cancelButton.Location = new
System.Drawing.Point(132, 284);
this.cancelButton.Name = "cancelButton";
this.cancelButton.Size = new
System.Drawing.Size(98, 21);
this.cancelButton.TabIndex = 11;
this.cancelButton.Text = "Cancel";
this.cancelButton.Click += new
System.EventHandler(this.cancelButton_Click);
//
// ModemEntryDialog
//
this.ClientSize = new
System.Drawing.Size(292, 314);
this.Controls.Add(this.cancelButton);
this.Controls.Add(this.okButton);
this.Controls.Add(this.phoneLabel);
this.Controls.Add(this.phoneTB);
this.Controls.Add(this.passwordLabel);
this.Controls.Add(this.passwordTB);
this.Controls.Add(this.loginLabel);
this.Controls.Add(this.loginTB);
this.Controls.Add(this.newModemEntryNameLabel);
this.Controls.Add(this.newModemEntryNameTB);
this.Controls.Add(this.modemLabel);
this.Controls.Add(this.modemListBox);
this.Name = "ModemEntryDialog";
this.Text = "Fill in
data fields";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private
System.Windows.Forms.ListBox modemListBox;
private
System.Windows.Forms.Label modemLabel;
private
System.Windows.Forms.TextBox newModemEntryNameTB;
private
System.Windows.Forms.Label newModemEntryNameLabel;
private
System.Windows.Forms.TextBox loginTB;
private
System.Windows.Forms.Label loginLabel;
private
System.Windows.Forms.Label passwordLabel;
private
System.Windows.Forms.TextBox passwordTB;
private
System.Windows.Forms.Label phoneLabel;
private
System.Windows.Forms.TextBox phoneTB;
private
System.Windows.Forms.Button okButton;
private
System.Windows.Forms.Button cancelButton;
} // class
ModemEntryDialog
} //
namespace csdialsample