AxMsRdpClient8NotSafeForScripting.Connect() not working No Exceptions, Not Errors - c#

I'm trying to check my RDP credentials using c#
Here is my reference : Remote Desktop using C#.NET
And here is what I've done so far :
private void testBtn_Click(object sender, EventArgs e) {
try {
AxMsRdpClient8NotSafeForScripting ax = new AxMsRdpClient8NotSafeForScripting();
ax.OnLoginComplete += Ax_OnLoginComplete;
ax.OnLogonError += Ax_OnLogonError;
ax.OnFatalError += Ax_OnFatalError;
ax.Size = new Size(1, 1);
ax.CreateControl();
ax.Server = ipTbx.Text;
ax.UserName = userNameTbx.Text;
MsRdpClient8NotSafeForScripting sec = (MsRdpClient8NotSafeForScripting)ax.GetOcx();
sec.AdvancedSettings8.ClearTextPassword = passwordTbx.Text;
sec.AdvancedSettings8.EnableCredSspSupport = true;
ax.Connect();
} catch (Exception ex) {
MessageBox.Show("Error : " + ex.Message);
}
}
private void Ax_OnFatalError(object sender, IMsTscAxEvents_OnFatalErrorEvent e) {
SaySomething();
}
private void Ax_OnLogonError(object sender, IMsTscAxEvents_OnLogonErrorEvent e) {
SaySomething();
}
private void Ax_OnLoginComplete(object sender, EventArgs e) {
SaySomething();
}
public void SaySomething() {
MessageBox.Show("Worked!");
}
As you can see, I've done everything in the article way. But nothing happens, even an exception would be worthy.
Any Idea?

Related

C# can't use variable from IF statement

I'm making easy password generator, but i cant pick int from try and string from if. Here's the code. I hope you help me. I cant make this I as textbox and i cant do nothing with it.
private void button3_Click(object sender, EventArgs e)
{
try
{
int i = Int32.Parse(textBox2.Text);
return;
}
catch
{
}
CreatePassword(i);
}
and here is part of CreatePassword function
public string CreatePassword(int length)
{
if (checkBox2.Checked)
{
const string src = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
return src;
}
else
{
const string src = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
return src;
}
}
There are several problems with your code. First, you're trying to access the variable i outside of the scope in which it is declared; it's not visible outside of the try statement. Second, it seems like you're expecting the password to be generated from the integer you parsed, but you're explicitly returning before the password can be created. Thirdly, you're not doing anything with the created password, just throwing it away.
Try the following:
private void button3_Click(object sender, EventArgs e)
{
try
{
int i = Int32.Parse(textBox2.Text);
string password = CreatePassword(i);
// TODO: use the 'password' string for something.
return;
}
catch
{
}
}
You should also consider using int.TryParse instead, which won't throw an exception.
if (int.TryParse(textbox2.Text, out int i) {
string password = CreatePassword(i);
// Do something with 'password'
} else {
// Display an error.
}
From your example, it looks like all your need is Int32.TryParse:
int.TryParse(textBox2.Text, out int i);
CreatePassword(i);
However, to answer your original question: you need to initialize i variable outside of the try block in order to be able to use it after it. For instance:
int i = 0;
try
{
i = int.Parse("test");
}
catch
{
}
Console.WriteLine(i); // 0
You logic is a bit flawed. If Textbox2 does not contain a valid integer, you ignore the exception and just create a password. What kind of password you expect to create?
I think you mean to do something like this:
private void button3_Click(object sender, EventArgs e)
{
try
{
int i = Int32.Parse(textBox2.Text);
CreatePassword(i);
}
catch
{
// Show a messagebox or something
}
}
Guys all thanks for help. I did it. Here you have my source of my application as thanks for you all. My app completely work and i believe you understand my logic :D
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Password_generator
{
public partial class Form1 : Form
{
private bool _dragging = false;
private Point _start_point = new Point(0, 0);
public Form1()
{
InitializeComponent();
}
public string CreatePassword(int length)
{
string src;
var sb = new StringBuilder();
Random RNG = new Random();
src = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (checkBox2.Checked)
{
src += "1234567890";
}
if (checkBox3.Checked)
{
src += "##$%^&*()";
}
for (var i = 0; i < length; i++)
{
var c = src[RNG.Next(0, src.Length)];
sb.Append(c);
}
textBox1.Text = sb.ToString();
if (checkBox1.Checked)
{
try
{
File.AppendAllText("hesla.txt", textBox1.Text + Environment.NewLine);
}
catch(Exception o)
{
MessageBox.Show("Něco se nepovedlo! " + Environment.NewLine + "(" + o.Message + ")");
}
}
return textBox1.Text;
}
private void button3_Click(object sender, EventArgs e)
{
try
{
int i = Int32.Parse(textBox2.Text);
CreatePassword(i);
}
catch
{
MessageBox.Show("Musíš zadat číslo!");
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
_dragging = false;
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (_dragging)
{
Point p = PointToScreen(e.Location);
Location = new Point(p.X - this._start_point.X, p.Y - this._start_point.Y);
}
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
_dragging = true;
_start_point = new Point(e.X, e.Y);
}
private void panel3_Paint(object sender, PaintEventArgs e)
{
}
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
try
{
Clipboard.SetText(textBox1.Text);
}
catch
{
}
}
}
}

GSMComm phoneConnected/phoneDisconnected Handlers

Trying to develop small application using GsmComm Library.
At the moment a have some problems with detecting if phone is connected or no.
it's detects when phone is disconnected, but doesn't want to detect phone when is connected back again ...
Any idea why ?
my code:
GsmCommMain gsm = new GsmCommMain(4, 115200, 200);
private void Form1_Load(object sender, EventArgs e)
{
gsm.PhoneConnected += new EventHandler(gsmPhoneConnected);
gsm.PhoneDisconnected += new EventHandler(gsmPhoneDisconnected);
gsm.Open();
}
private delegate void ConnctedHandler(bool connected);
private void onPhoneConnectedChange(bool connected)
{
try
{
if (connected)
{
phoneStatus.Text = "OK";
}
else
{
phoneStatus.Text = "NG";
}
}
catch (Exception exce)
{
logBox.Text += "\n\r" + exce.ToString();
}
}
public void gsmPhoneConnected(object sender, EventArgs e)
{
this.Invoke(new ConnctedHandler(onPhoneConnectedChange), new object[] { true });
}
private void gsmPhoneDisconnected(object sender, EventArgs e)
{
this.Invoke(new ConnctedHandler(onPhoneConnectedChange), new object[] { false });
}
Sorry for late answer. Just noticed your question.
There is no need to use EventHandler for connection. If you want to call some functions after phone/gsm modem is connected you should call them after you opened port and (!) checked whether connection is established using IsConnected() member function in GsmCommMain class.
var gsm = new GsmCommMain(4, 115200, 200);
private void Form1_Load(object sender, EventArgs e)
{
//gsm.PhoneConnected += new EventHandler(gsmPhoneConnected); // not needed..
gsm.PhoneDisconnected += new EventHandler(gsmPhoneDisconnected);
gsm.Open();
if(gsm.IsConnected()){
this.onPhoneConnectedChange(true);
}
}
private delegate void ConnctedHandler(bool connected);
private void onPhoneConnectedChange(bool connected)
{
try
{
if (connected)
{
phoneStatus.Text = "OK";
}
else
{
phoneStatus.Text = "NG";
}
}
catch (Exception exce)
{
logBox.Text += "\n\r" + exce.ToString();
}
}
/*public void gsmPhoneConnected(object sender, EventArgs e)
{
this.Invoke(new ConnctedHandler(onPhoneConnectedChange), new object[] { true });
}*/
private void gsmPhoneDisconnected(object sender, EventArgs e)
{
this.Invoke(new ConnctedHandler(onPhoneConnectedChange), new object[] { false });
}

Select and display Image files from ListBox, to be display within PictureBox? C#

I am trying to make my listBox (lstFiles) selectable, so it's able to display the image file within a pictureBox (pictureBox1) and change after selecting another file from listBox, Im creating a webcam program that takes pictures which works, but having trouble with with displaying the images.
I have tried many ways but can't get it to work from selecting the filename from the listbox
Any help would be grateful thank you
This is what I have so far:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using Pinvoke;
using System.IO;
namespace TestAvicap32
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitializeDevicesList();
}
private void InitializeDevicesList()
{
splitContainer1.Panel1.Enabled = true;
splitContainer1.Panel2.Enabled = false;
foreach (CaptureDevice device in CaptureDevice.GetDevices())
{
cboDevices.Items.Add(device);
}
if (cboDevices.Items.Count > 0)
{
cboDevices.SelectedIndex = 0;
}
}
private void btnStart_Click(object sender, EventArgs e)
{
int index = cboDevices.SelectedIndex;
if (index != -1)
{
splitContainer1.Panel1.Enabled = false;
splitContainer1.Panel2.Enabled = true;
((CaptureDevice)cboDevices.SelectedItem).Attach(pbImage);
}
}
private void btnStop_Click(object sender, EventArgs e)
{
splitContainer1.Panel1.Enabled = true;
splitContainer1.Panel2.Enabled = false;
((CaptureDevice)cboDevices.SelectedItem).Detach();
}
private void btnSnapshot_Click(object sender, EventArgs e)
{
try
{
Image image = ((CaptureDevice)cboDevices.SelectedItem).Capture();
image.Save(#"c:\webcapture\" + DateTime.Now.ToString("HH.mm.ss-dd-MM-yy") + ".png", ImageFormat.Png);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btntimer_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
btntimerstop.Visible = true;
btntimer.Visible = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
Image image = ((CaptureDevice)cboDevices.SelectedItem).Capture();
image.Save(#"c:\webcapture\" + DateTime.Now.ToString("HH.mm.ss-dd-MM-yy") + ".png", ImageFormat.Png);
}
private void btntimerstop_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
btntimer.Visible = true;
btntimerstop.Visible = false;
}
private void Form1_Load(object sender, EventArgs e)
{
btntimerstop.Visible = false;
foreach (DriveInfo di in DriveInfo.GetDrives())
lstDrive.Items.Add(di);
}
private void lstFolders_SelectedIndexChanged(object sender, EventArgs e)
{
lstFiles.Items.Clear();
DirectoryInfo dir = (DirectoryInfo)lstFolders.SelectedItem;
foreach (FileInfo fi in dir.GetFiles())
lstFiles.Items.Add(fi);
}
private void lstDrive_SelectedIndexChanged(object sender, EventArgs e)
{
lstFolders.Items.Clear();
try
{
DriveInfo drive = (DriveInfo)lstDrive.SelectedItem;
foreach (DirectoryInfo dirInfo in drive.RootDirectory.GetDirectories())
lstFolders.Items.Add(dirInfo);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void lstFiles_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
//I don't know If I need this?
}
}
}
Try like below... it will work....
private void lstFiles_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(((FileInfo)lstFiles.SelectedItem).FullName);
}

Movable UserControl in WPF

I created simple UserControl with several labels. How can I implement simple mechanism, that allows moving whole control like normal window (when I add it to winForms - if it makes difference)
You can use my Capture class:
public class ClsCapture
{
bool bCaptureMe;
Point pLocation = new Point();
Control dd;
//Handles dad.MouseDown, dd.MouseDown
private void Form1_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
try {
bCaptureMe = true;
pLocation = e.GetPosition(sender);
} catch {
}
}
//Handles dad.MouseMove, dd.MouseMove
private void Form1_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
try {
if (bCaptureMe) {
dd.Margin = new Thickness(dd.Margin.Left - pLocation.X + e.GetPosition(sender).X, dd.Margin.Top - pLocation.Y + e.GetPosition(sender).Y, dd.Margin.Right, dd.Margin.Bottom);
}
} catch {
}
}
//Handles dad.MouseUp, dd.MouseUp
private void Form1_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
try {
bCaptureMe = false;
} catch {
}
}
public ClsCapture(Control pnl)
{
dd = pnl;
dd.PreviewMouseLeftButtonDown += Form1_MouseDown;
dd.PreviewMouseLeftButtonUp += Form1_MouseUp;
dd.PreviewMouseMove += Form1_MouseMove;
}
public static void CaptureMe(Control pnl)
{
ClsCapture cc = new ClsCapture(pnl);
}
}
Usage:
ClsCapture.CaptureMe(AnyControlYouWant);

Calling variable from another event

I want to call a variable from another event, for example
public void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
var ccc = lblTicketsA;
}
public void btnSubmit_Click(object sender, EventArgs e)
{
try
{
ccc.text = "test";
}
catch (Exception ex)
{
lblDisplay.Text = ex.Message;
}
}
thank you
Make ccc an instance field like this:
private SomeType ccc;
public void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
this.ccc = lblTicketsA;
}
public void btnSubmit_Click(object sender, EventArgs e)
{
try
{
this.ccc.text = "test"
}
catch (Exception ex)
{
lblDisplay.Text = ex.Message;
}
}

Categories