
Клиент csClientChangedRemoting.Cpp на языке с#
/////////////////////////
// C# Клиент
// Приложение клиент с управляющими элементами
using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
using csLorryAndWarehouseChangedRemotingDll;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
namespace csClientChangedRemoting
{
class LorryAndWarehouses: Form
{
csLorryAndWarehouseChangedRemotingDll.IRemObj iRemObj;
Button butRun, butStop; // Ссылки на интерфейсные кнопки
Button butAdd, butDel; // Ссылки на интерфейсные кнопки
ListBox listBox; // Список номеров грузовиков
// Конструктор
LorryAndWarehouses ( )
{
this.Text= "Client";
this.ClientSize= new Size(350, 50);
// Создать кнопки
// Создать кнопку пуска
butRun= new Button ( );
butRun.Location= new Point (10,5);
butRun.Size= new Size (45, 20);
butRun.Text= "Run";
butRun.BackColor= Color.LightGray;
butRun.Click += new EventHandler(But_Run);
this.Controls.Add(butRun);
// Создать кнопку останова
butStop= new Button ( );
butStop.Location= new Point (60,5);
butStop.Size= new Size (45, 20);
butStop.Text= "Stop";
butStop.BackColor= Color.LightGray;
butStop.Click += new EventHandler(But_Stop);
this.Controls.Add(butStop);
// Создать кнопку добавления компонента
butAdd= new Button ( );
butAdd.Location= new Point (110,5);
butAdd.Size= new Size (65, 20);
butAdd.Text= "Add";
butAdd.BackColor= Color.LightGray;
butAdd.Click += new EventHandler(But_Add);
this.Controls.Add(butAdd);
// Создать кнопку удаления компонента
butDel= new Button ( );
butDel.Location= new Point (185,5);
butDel.Size= new Size (65, 20);
butDel.Text= "Delete";
butDel.BackColor= Color.LightGray;
butDel.Click += new EventHandler(But_Del);
this.Controls.Add(butDel);
// Создать элемент списка
listBox= new ListBox ( );
listBox.Location= new Point (265, 5);
listBox.Size= new System.Drawing.Size (60, 20);
this.Controls.Add (listBox);
this.Show ( );
// Создать канал и зарегистрировать его
HttpChannel httpChannel= new HttpChannel (0);
ChannelServices.RegisterChannel (httpChannel);
iRemObj= (IRemObj) Activator.GetObject (
typeof (csLorryAndWarehouseChangedRemotingDll.IRemObj),
"http://localhost:8080/RemoteObject");
}
// Обработать нажатие кнопки пуска
private void But_Run (object o, EventArgs e)
{
iRemObj.RunLorries ( );
}
// Обработать нажатие кнопки останова
private void But_Stop (object o, EventArgs e )
{
iRemObj.StopLorries ( );
}
// Обработать нажатие кнопки добавления грузовика
private void But_Add (object o, EventArgs e)
{
iRemObj.AddLorry ( );
iRemObj.ShowComponents ( );
ArrayList aL= new ArrayList ( );
aL= iRemObj.GetNumLorries ( );
listBox.Items.Clear ( );
for (int i= 0; i < aL.Count; i++)
listBox.Items.Add (aL[i]);
iRemObj.ShowComponents ( );
}
// Обработать нажатие кнопки удаления грузовика
private void But_Del (object o, EventArgs e )
{
if (listBox.SelectedIndex==-1)
{
MessageBox.Show
("Выбрать номер удаляемого грузовика в"
+ "\nэлементе списка перед нажатием кнопки");
}
else
{
int numSel= (int) listBox.SelectedItem;
listBox.Items.Remove (numSel);
iRemObj.RemoveLorry (numSel);
ArrayList aL= new ArrayList ( );
aL= iRemObj.GetNumLorries ( );
listBox.Items.Clear ( );
for (int i= 0; i < aL.Count; i++)
listBox.Items.Add (aL[i]);
iRemObj.ShowComponents ( );
}
}
// Обработать кнопку закрытия окна
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
iRemObj.RemoveAllLorries ( );
}
// Основная функция
static void Main ( )
{
Application.Run (new LorryAndWarehouses ( ));
}
}
}