Sending cancel parameter C# - c#

Okay, this is my whole code. Now... Detail_BeforePrint() will be called first, and xrPictureBox8_BeforePrint second.
Now I want to call Detail_BeforePrint e.Cancel = true; inside the else in xrPictureBox8_BeforePrint event.
private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
//here should e.Cancel = true be if it came from xrPictureBox_BeforePrint()
}
private void xrPictureBox8_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
try
{
if (xrPictureBox8.ImageUrl.Length > 0) { }
else
{
Detail_BeforePrint(null,[call Cancel parameter]);
}
}
catch (Exception)
{
}
}

Maybe something like this would help?
private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
DoDetail_BeforePrint(e, false);
}
private void DoDetail_BeforePrint(System.Drawing.Printing.PrintEventArgs e, bool cancel)
{
if (cancel) e.Cancel = true;
//other things
}
private void xrPictureBox8_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
try
{
if (xrPictureBox8.ImageUrl.Length > 0) { }
else
{
DoDetail_BeforePrint(e, true);
//or just call e.Cancel = true here?
}
}
catch (Exception)
{
}
}

Related

How append Rx data on textBox?

I trying to do an inteface to monitoring the Serial Port. I am using Visual forms. So, I had created a combobox to select the PortCOM, a TextBox to send the data to Serial Port and a TextBoxReceber to receive the Serial Data. I trying print the data received in the TextBoxReceber, I'm using the AppendText but I haven't sucess. Anybody can help me?
My Form1.cs is:
namespace ConsoleESP
{
public partial class Form1 : Form
{
string RxString = "";
public Form1()
{
InitializeComponent();
timerCOM.Enabled = true;
atualizaCOMs();
}
private void atualizaCOMs()
{
int i = 0;
bool quantDif = false;
if (comboBox1.Items.Count == SerialPort.GetPortNames().Length)
{
foreach (string s in SerialPort.GetPortNames())
{
if (comboBox1.Items[i++].Equals(s) == false)
{
quantDif = true;
}
}
}
else quantDif = true;
if (quantDif == false) return;
comboBox1.Items.Clear();
foreach(string s in SerialPort.GetPortNames())
{
comboBox1.Items.Add(s);
}
comboBox1.SelectedIndex = 0;
}
private void timerCOM_Tick(object sender, EventArgs e)
{
atualizaCOMs();
}
private void btConnect_Click(object sender, EventArgs e)
{
if(serialPort1.IsOpen == false)
{
try
{
serialPort1.PortName = comboBox1.Items[comboBox1.SelectedIndex].ToString();
serialPort1.Open();
}
catch
{
return;
}
if (serialPort1.IsOpen)
{
btConnect.Text = "Desconectar";
comboBox1.Enabled = false;
}
}
else
{
try
{
serialPort1.Close();
comboBox1.Enabled = true;
btConnect.Text = "Conectar";
}
catch
{
return;
}
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (serialPort1.IsOpen == true) serialPort1.Close();
}
private void btEnviar_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen == true)
serialPort1.Write(textBoxEnviar.Text);
}
private delegate void RefreshTextBox();
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
RxString = serialPort1.ReadExisting();
this.Invoke(new EventHandler(TrataDadoRecebido));
}
private void TrataDadoRecebido(object sender, EventArgs e)
{
textBoxReceber.AppendText(RxString);
}
}
}

Combobox and timer in Visual Studio 2012

The code after edit is:
private void Form1_load(object sender, EventArgs e)
{
}
private void ComboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
try
{
if (comboBox1.Text == "Present")
{
label47.Text = DateTime.Now.ToShortDateString();
label9.Text = DateTime.Now.ToShortTimeString();
}
}
catch (Exception)
{
}
}
}
}
use selected index changed event
private void ComboBox1_SelectedIndexChanged(object sender,System.EventArgs e)
{
try
{
if(ComboBox1.Text == "present")
{
label.Text = DateTime.Now.ToString(#"MM\/dd\/yyyy HH:mm");
}
}catch(Exception)
{
}
}

Cancel a Service Reference Call in Background Worker C#

i am calling an asmx service from my windows form application in a background worker. i want to be able to cancel/stop the call once a button is pressed.
i am currently performing the following:
backgroundWorker1 = new BackgroundWorker();
backgroundWorker1.WorkerSupportsCancellation = true;
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
public void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
GlobalVariables.worker = sender as BackgroundWorker;
try
{
if (GlobalVariables.worker.CancellationPending == true)
{
e.Cancel = true;
return;
}
else
{
e.Result = CallServer(ServerName, ActionName); // call on a web method in the referenced service
}
}
catch (Exception ee)
{
GlobalFunctions.ShowError(ee);
}
}
private void button1_Click(object sender, EventArgs e) //button to cancel the call
{
if (GlobalVariables.worker.IsBusy == true)
{
//GlobalVariables.worker.thre;
GlobalVariables.worker.CancelAsync();
GlobalVariables.worker.Dispose();
}
//GlobalVariables.worker.CancelAsync();
Form1.f.Enabled = true;
Form1.f.progressBar1.Text = "Done!";
Form1.f.progressBar1.Visible = false;
Form1.f.textBox1.Visible = true;
Close();
}
the following doesnt seem to work because when i press on cancel, the web method called in the service reference is not stopped and still returns data
You can make client react immediately to the Abort button by using a Task and a CancellationToken. The client will do its best to abort the call, but the server will continue execution of the call unless it does something special about that.
Here is the code of the client side:
public partial class Form1 : Form
{
private CancellationTokenSource _cancellationTokenSource;
private WebService1SoapClient _client;
public Form1()
{
InitializeComponent();
btnAbort.Enabled = false;
}
private void btnCall_Click(object sender, EventArgs e)
{
btnCall.Enabled = false;
btnAbort.Enabled = true;
lblStatus.Text = "CALLING SERVER...";
_cancellationTokenSource = new CancellationTokenSource();
_cancellationTokenSource.Token.Register(() => backgroundWorker1.CancelAsync());
backgroundWorker1.RunWorkerAsync();
}
private void btnAbort_Click(object sender, EventArgs e)
{
if (_cancellationTokenSource != null)
{
_cancellationTokenSource.Cancel();
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
if (backgroundWorker1.CancellationPending)
{
e.Cancel = true;
return;
}
try
{
_client = new WebService1SoapClient();
var call = CallServerAsync("ServerName", "ActionName");
call.Wait(_cancellationTokenSource.Token);
e.Result = call.Result;
}
catch (TaskCanceledException)
{
e.Cancel = true;
_client.Abort();
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
btnCall.Enabled = true;
btnAbort.Enabled = false;
lblStatus.Text = (e.Error != null ? "CALL FAILED: " + e.Error.Message : "CALL COMPLETED!");
}
private async Task<string> CallServerAsync(string serverName, string actionName)
{
var response = await _client.HelloWorldAsync();
return response.Body.HelloWorldResult;
}
}
thanks to #felix-b answer , i did the following:
public void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
GlobalVariables.worker = sender as BackgroundWorker;
try
{
if (GlobalVariables.worker.CancellationPending == true)
{
e.Cancel = true;
return;
}
else
{
Object callResponse = CallServer(ServerName, ActionName);
if (GlobalVariables.worker.CancellationPending == true)
{
e.Cancel = true;
return;
}
else
{
e.Result = callResponse;
}
}
}
catch (Exception ee)
{
e.Cancel = true;
if (ee.GetType().IsAssignableFrom(typeof(System.ServiceModel.CommunicationObjectAbortedException)))
{
MessageBox.Show("The Request Was Cancelled");
}
else
{
GlobalFunctions.ShowError(ee);
}
}
}
public void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{}
else
{
//continue
}
}
private void button1_Click(object sender, EventArgs e)
{
if (GlobalVariables.worker.IsBusy == true)
{
server.abort(); //service reference
GlobalVariables.worker.CancelAsync();
GlobalVariables.worker.Dispose();
}
}

Object reference not set to an instance of an object when i click on RadioButton

I have two RadioButtons
private void CarRadioButton_Checked(object sender, RoutedEventArgs e)
{
try
{
// When CarRadioButton is clicked. "Leasing" and "Sale" is added
to Combobox
ContractComboBox.Items.Clear();
ContractComboBox.Items.Add("Leasing");
ContractComboBox.Items.Add("Sale");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void TruckRadioButton_Checked(object sender, RoutedEventArgs e)
{
try
{
// When TruckRadioButton is clicked. "Leasing" is added
to Combobox
ContractComboBox.Items.Clear();
ContractComboBox.Items.Add("Leasing");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
When I select an item from the ComboBox, I want to do something, like the following:
private void ContractComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ContractComboBox.SelectedItem.ToString() == "Leasing")
{
PriceMonthTextBox.IsEnabled = true;
PeriodTextBox.IsEnabled = true;
}
if (ContractComboBox.SelectedItem.ToString() == "Sale")
{
PriceMonthTextBox.IsEnabled = false;
PeriodTextBox.IsEnabled = false;
}
}
Now here is my problem. If I select an item from the ComboBox and after that click on another RadioButton I get an error: Object reference not set to an instance of an object.
Steps I did leading to the exception:
CarRadioButton Clicked.
Picked "Leasing" from ComboBox
I click on TruckRadioButton
Get error: Object reference not set to an instance of an object.
Any ideas? i think its a little problem but i cant spot it :(
Why don't you protect the code in the SelectedIndexChanged with a simple check on SelectedItem?
private void ContractComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ContractComboBox.SelectedItem != null)
{
if (ContractComboBox.SelectedItem.ToString() == "Leasing")
{
PriceMonthTextBox.IsEnabled = true;
PeriodTextBox.IsEnabled = true;
}
if (ContractComboBox.SelectedItem.ToString() == "Sale")
{
PriceMonthTextBox.IsEnabled = false;
PeriodTextBox.IsEnabled = false;
}
}
}
Try This:
private void ContractComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(ContractComboBox.SelectedIndex >= 0)
{
if (ContractComboBox.SelectedItem.ToString() == "Leasing")
{
PriceMonthTextBox.IsEnabled = true;
PeriodTextBox.IsEnabled = true;
}
if (ContractComboBox.SelectedItem.ToString() == "Sale")
{
PriceMonthTextBox.IsEnabled = false;
PeriodTextBox.IsEnabled = false;
}
}
}

Is there any way to replay a video in Windows Media Control?

How I can replay a video in Windows Media control? I try to do it by this way? but it doesn't work
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, System.EventArgs e)
{
//mediaPlayer.currentPlaylist = mediaPlayer.mediaCollection.getByName("Dastan");
//mediaPlayer.URL = #"C:\Documents and Settings\Администратор\Мои документы\Моя музыка\Мои списки воспроизведения\Dastan.wpl";
//mediaPlayer.uiMode = "none";
PlayFile(#"C:\Documents and Settings\Администратор\Мои документы\Моя музыка\Мои списки воспроизведения\Dastan.wpl");
}
private void mediaPlayer_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if ((WMPLib.WMPPlayState)e.newState == WMPPlayState.wmppsPlaying)
{
mediaPlayer.fullScreen = true;
mediaPlayer.Ctlenabled = false;
}
else if ((WMPLib.WMPPlayState)e.newState == WMPPlayState.wmppsMediaEnded)
{
Form1_Load(null, null);
}
}
private void PlayFile(String url)
{
mediaPlayer.URL = url;
mediaPlayer.Ctlcontrols.play();
}
Any ideas?
mediaPlayer.settings.setMode("loop", true);
This code snippet does all job. Thanks for answers..
Try setting mediaPlayer.Ctlcontrols.currentPosition to 0.
private void mediaPlayer_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if ((WMPLib.WMPPlayState)e.newState == WMPPlayState.wmppsPlaying)
{
mediaPlayer.fullScreen = true;
mediaPlayer.Ctlenabled = false;
}
else if ((WMPLib.WMPPlayState)e.newState == WMPPlayState.wmppsMediaEnded)
{
mediaPlayer.Ctlcontrols.currentPosition = 0;
mediaPlayer.Ctlcontrols.play();
}
}

Categories