Jul 28, 2010

Backgroundworker threads

Backgroundworker threads

The BackgroundWorker class is a component in Windows Forms applications that can be used for task which take some time. Task will be run in a dedicated thread (asynchr). You can drag de background wordker from the compenent tab in the toolbox on to the form.
In a triggered method, create the events if the thread has to be created. Implement 3 events. The work event, the completed event and de progress changed event. This does not trigger the thread, but only implements the functionality
private void InitializeBackgoundWorker()
{
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler( backgroundWorker1_RunWorkerCompleted);
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler( backgroundWorker1_ProgressChanged);
}
Example implementation DoWork event:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
e.Result = DoLongWork((int)e.Argument, worker, e);
}
The sender is de background worker. The eventargs contain the option for cancelling the operation. The worker object tells abut the status. The user could have choosen to cancel the operation. If the consuming time function is executing. The worker status can be questionned along the way. A user could have choosen the cancel the operation.
if (worker.CancellationPending) { e.Cancel = true; }
With the function backgroundWorker1.CancelAsync(); the thread can be cancelled.With the function backgroundWorker1.RunWorkerAsync(numberToCompute); the thread can be started.

After completing or cancelling the thread the Completed event will be triggered
private void backgroundWorker1_RunWorkerCompleted( object sender, RunWorkerCompletedEventArgs e) { }
The eventargs contains the result.
e has attribute error. The e.Error can be set in the DoWork event
e has attribute cancelled. The e.Cancelled can be set in the DoWork event
e has attribute result. Also can be set in the DoWork event.
In the DoWork event the progress can be reported. Call worker.ReportProgress(percentComplete);
In the event handler of ProgessChanged read the value:
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
textBoxCompleted.Text = e.ProgressPercentage.ToString() + "% completed";
}

Jul 19, 2010

DevExpress Win Form XGrid Set focus on editied column

how to set focus on edition column on click on add

   If GridView2.DataRowCount = 0 Then
            GridView2.FocusedColumn = GridView2.VisibleColumns(0)
            GridView2.SetFocusedValue(0)
            additem()
        End If

Jul 6, 2010

Filter ListBox using javascript

<head id="Head1" runat="server">
  <title>Demotitle>
head>
<script type="text/javascript" language="javascript">
  var myVals=new Array();
   function CacheValues()
   {
   var l =  document.getElementById('ListBox1');
 
   for (var i=0; i < l.options.length; i++)
    {
      myVals[i] = l.options[i].text;
    }
   }
 
  function SearchList()
  {
    var l document.getElementById('ListBox1');
    var tb = document.getElementById('TextBox1');
   
    l.options.length=0;
 
    if(tb.value == "")
    {
      for (var i=0; i < myVals.length; i++)
      {
        l.options[l.options.length] = new Option(myVals[i]);
      }
    }
    else{
 
      for (var i=0; i
      {
        if (myVals[i].toLowerCase().indexOf(tb.value.toLowerCase()) != -1)
        {
          l.options[l.options.length] = new Option(myVals[i]);
        }
        else
        {
          // do nothing
        }
      }
    }
  }
  function ClearSelection(lb)
  {
    lb.selectedIndex = -1;
  }
 
<body onload=CacheValues();>
  <form id="form1" runat="server">
  <input type="text" ID="TextBox1" onkeyup="return SearchList();"/><br />
  <select ID="ListBox1" Height="150px" Width="250px"  size="13" multiple="multiple">
  <option>Vincentoption>
  <option>Jenniferoption>
  <option>Shynneoption>
  <option>Christianoption>
  <option>Helenoption>
  <option>Vladioption>
  <option>Beeoption>
  <option>Jeromeoption>
  <option>Vinzoption>
  <option>Churchilloption>
  <option>Rodoption>
  <option>Markoption>
  select>
  form>
body>
html>