So i am trying to recode this because its not working correctly :
Private Sub StartTrafficExchange()
Dim downloadstring As New StreamReader(Response2.GetResponseStream())
Dim filterstring As String() = downloadstring.ReadToEnd.Split("|")
For Each stirngman As String In filterstring
linklist.Items.Add(stirngman)
Next
Do Until linklist.Items.Count = 0
Dim rand As New Random
linklist.SelectedIndex = rand.Next(0, linklist.Items.Count - 1)
Dim strimgna As String = linklist.SelectedItem
Dim newlinkstring As String() = strimgna.Split("``")
For Each stringma As String In newlinkstring
If stringma.Length < 8 Then
GoTo a
Else
LabelX1.Text = "Navigating To " + stringma
stringma = stringma.Replace("[TIER4]", "")
Debug.WriteLine(stringma)
WebKitBrowser1.Navigate(strimgna)
Thread.Sleep(15000)
End If
LabelX1.Text = "Navigating To " + stringma
ProgressBarX1.Value += 1 a:
Next
linklist.Items.Remove(linklist.SelectedItem)
Loop
ProgressBarX1.Maximum = linklist.Items.Count
ProgressBarX1.Value = 0
StartTrafficExchange()
End Sub
So what does the code do ? Well it fetches a webrequest to a link where the source will be like
http://www.asdfd.com``[TIER4]|http://aesde.com``[TIER4]|http://www.excaedf.com``[TIER4]|
As you can see the above code first splits "|" and adds them each to a list.
Then we split other things to make it as a perfect url and then navigate to it by webkitbrowser... and waits 15 sec by using thread.sleep (Idk if it works.) then removes it from linklist in a foreach and do until loop. So whats the problem ? Well it doesn't work correctly the webkitbrowser is just hanging .
Note : The sub StartTrafficExchange() is called when the form loads.
Can someone tell me whats wrong with the above code and is there anyother way to make this work ?
-Thanks-
Assuming that linklist is not an actual LinkedList but is really a List(Of T) or the list of a List control, you can replace this chunk of code:
Dim filterstring As String() = downloadstring.ReadToEnd.Split("|")
For Each stirngman As String In filterstring
linklist.Items.Add(stirngman)
Next
with this:
Dim filter As String() = downloadstring.ReadToEnd.Split("|")
linklist.AddRange(filter)
which does the same thing, only faster, and with less memory being consumed.
Next, move this way up (like immediately after the AddRange):
ProgressBarX1.Maximum = linklist.Items.Count
I was going to type out why to make the various changes, but here is the whole routine as I would refactor it:
Private Sub StartTrafficExchange()
' Performing one split here that removes everything that needs
' to go will be faster, take less memory, won't have to be
' touched as many times
Dim filter As String() = downloadstring.ReadToEnd.Split("``[TIER4]|")
linklist.AddRange(filter)
' Set your maximum value so it knows when it is full
ProgressBarX1.Maximum = linklist.Items.Count
' Create a counter to let us know how many items
' have been processed
Dim counter As Int = 0
Dim currentURL As String = String.Empty
Do While linklist.Items.Count > 0
' Get the current URL from our list
currentURL = linklist.Items(counter)
LabelX1.Text = String.Format("Navigating To [TIER4] {0}", currentURL)
LabelX1.Refresh() ' Allow the label to update
' This is only good if you have a debugger turned on
' or are running from the IDE
Debug.WriteLine(currentURL)
WebkitBrowser1.Navigate(currentURL)
' Not a good idea as it will block the UI
' from responding. If you need a delay that
' doesn't appear to lock up the UI, implement
' a timer in a loop
Thread.Sleep(15000)
' Increment the counter, then update the progressbar
counter += 1
ProgressBarX1.Value = counter
ProgressBarX1.Refresh() ' Allow the progressbar to update
Loop
' When you are through getting all of the URLs,
' clear the list just one time
linklist.Items.Clear()
' Make recursive call to this sub
' You should limit the number of recursions
' somehow so you don't run out of stack space
StartTrafficExchange
End Sub
If you want to pause the process with blocking the UI, use this:
' Create a new timer object that will run for 1/10 of second
Dim timr As New Timer(100)
' Run this for 150 times at .1 seconds will
' give you a 15 second pause and still leave
' the UI responsive
For iLoop As Integer = 0 To 150
' Start the timer for .1 seconds
timr.Start()
' This tells everything on the form to process updates
DoEvents()
Next
First of all, this is not very important but the code is inconsistent, you are using C# practices but you are in VB.NET, then remove all those + operator on strings appends and replace them with an &.
Second, if your purpose is to wait for the webpage to be fully loaded then using Sleep method is not the way, you need to suscribe to like an WebBrowserDocumentCompleted event of the browser control (I don't know the exact event name for a WebKitBrowser)
You can simplify things by writting a method called NavigateAndWait and use it instead of using WebKitBrowser.Navigate method.
I give you an example for a default WebBrowser control:
Private WebPageLoaded As Boolean = False
''' <summary>
''' Navigates to an url and waits the page to be loaded.
''' </summary>
''' <param name="url">Indicates the url to navigate.</param>
''' <param name="newWindow">Indicates whether the url should open into a new browser window.</param>
Private Sub NavigateAndWait(ByVal Browser As WebBrowser,
ByVal url As String,
Optional newWindow As Boolean = False)
Me.WebPageLoaded = False
AddHandler Browser.DocumentCompleted, AddressOf WebBrowserDocumentCompleted
Browser.Navigate(url, newWindow)
Do Until Me.WebPageLoaded
Application.DoEvents()
Loop
RemoveHandler Browser.DocumentCompleted, AddressOf WebBrowserDocumentCompleted
End Sub
' WebBrowser [DocumentCompleted]
Private Sub WebBrowserDocumentCompleted(ByVal sender As Object, e As WebBrowserDocumentCompletedEventArgs)
Me.WebPageLoaded = True
End Sub
And other for a GeckoFX webbrowser:
Private WebPageLoaded As Boolean = False
''' <summary>
''' Navigates to an url and waits the page to be loaded.
''' </summary>
''' <param name="url">Indicates the url to navigate.</param>
Private Sub NavigateAndWait(Byval Browser as Gecko.GeckoWebBrowser,
Byval url As String,
Optional loadFlags As Gecko.GeckoLoadFlags = Gecko.GeckoLoadFlags.None,
Optional referrer As String = Nothing,
Optional postData As Gecko.GeckoMIMEInputStream = Nothing)
Me.WebPageLoaded = False
AddHandler Browser.DocumentCompleted, AddressOf GeckoWebBrowserDocumentCompleted
Browser.Navigate(url, loadFlags, referrer, postData)
Do Until Me.WebPageLoaded
Application.DoEvents()
Loop
RemoveHandler Browser.DocumentCompleted, AddressOf GeckoWebBrowserDocumentCompleted
End Sub
' GeckoWebBrowser [DocumentCompleted]
Private Sub GeckoWebBrowserDocumentCompleted(ByVal sender As Object, e As EventArgs)
Me.WebPageLoaded = True
End Sub
Related
A DataGridView displays hostnames at Column index 0, computer / printer names on the network.
pc1
pc2
print3
pc5
print
....
There are more than 500 such names.
I know how to ping them:
For i = 0 To DataGridView1.Rows.Count - 1
Try
If My.Computer.Network.Ping(DataGridView1.Item(0, i).Value) = True Then
DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.Lime
Else
DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.Red
End If
Catch ex As Exception
DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.Red
End Try
Next
The problem is that the Ping takes a very long time and the application freezes.
How can you speed up this procedure?
And let's say if the node is not available, then simply remove it from the list.
An example to Ping multiple addresses at the same time, using the async version of provided by the Ping class, Ping.SendPingAsync().
This version is await-able, not the same as the Ping.SendAsync() method, still asynchronous but event-driven.
Since you're using a DataGridView to both store the IpAddress/HostName and to present the PingReply results, you need to determine a way to match the Ping result to correct Cell of the DataGridView from which the Ip/Host address was taken.
Here, I'm passing to the method the Row's Index, so when the Ping result comes back, asynchronously, we can match the response to a specific Cell in the DataGridView.
To make the initialization method more generic, I'm passing also the index of the Column where the Ip/Host address is stored and the index of the Column that will show the result (you could also just pass all indexes, not a DataGridView Control reference to the method and handle the results in a different way).
A loop extracts the addresses from the the DataGridView and creates a List(Of Task), adding a PingAsync() Task for each address found.
When the collection is completed, the List(Of Task) is passed to the Task.WhenAll() method, which is then awaited.
This method starts all the Task in the list and returns when all Task have a result.
► Note that the Ping procedure sets a TimeOut, to 5000ms here, so all the Tasks will return before or within that interval, successful or not.
You can then decide if you want to reschedule the failed Pings or not.
The UI update is handled using a Progress delegate. It's just a method (Action delegate) that is called when the Ping procedure has a result to show.
It can also be used when the method that updates the UI runs in a different Thread: the Report() method will call the Progress object delegate in the Thread that created the delegate: the UI Thread, here (in the example, we're not actually ever leaving it, though).
This is how it works:
Assume you're starting the ping sequence from Button.Click event handler.
Note that the handler is declared async.
Private Async Sub btnMassPing_Click(sender As Object, e As EventArgs) Handles btnMassPing.Click
Await MassPing(DataGridView1, 1, 2)
End Sub
Initialization method and IProgress<T> report handler:
Imports System.Drawing
Imports System.Net.NetworkInformation
Imports System.Net.Sockets
Imports System.Threading.Tasks
Private Async Function MassPing(dgv As DataGridView, statusColumn As Integer, addressColumn As Integer) As Task
Dim obj = New Object()
Dim tasks = New List(Of Task)()
Dim progress = New Progress(Of (sequence As Integer, reply As Object))(
Sub(report)
SyncLock obj
Dim status = IPStatus.Unknown
If TypeOf report.reply Is PingReply Then
status = DirectCast(report.reply, PingReply).Status
ElseIf TypeOf report.reply Is SocketError Then
Dim socErr = DirectCast(report.reply, SocketError)
status = If(socErr = SocketError.HostNotFound,
IPStatus.DestinationHostUnreachable,
IPStatus.Unknown)
End If
Dim color As Color = If(status = IPStatus.Success, Color.Green, Color.Red)
Dim cell = dgv(statusColumn, report.sequence)
cell.Style.BackColor = color
cell.Value = If(status = IPStatus.Success, "Online", status.ToString())
End SyncLock
End Sub)
For row As Integer = 0 To dgv.Rows.Count - 1
If row = dgv.NewRowIndex Then Continue For
Dim ipAddr = dgv(addressColumn, row).Value.ToString()
tasks.Add(PingAsync(ipAddr, 5000, row, progress))
Next
Try
Await Task.WhenAll(tasks)
Catch ex As Exception
' Log / report the exception
Console.WriteLine(ex.Message)
End Try
End Function
PingAsync worker method:
Private Async Function PingAsync(ipAddress As String, timeOut As Integer, sequence As Integer, progress As IProgress(Of (seq As Integer, reply As Object))) As Task
Dim buffer As Byte() = New Byte(32) {}
Dim ping = New Ping()
Try
Dim options = New PingOptions(64, True)
Dim reply = Await ping.SendPingAsync(ipAddress, timeOut, buffer, options)
progress.Report((sequence, reply))
Catch pex As PingException
If TypeOf pex.InnerException Is SocketException Then
Dim socEx = DirectCast(pex.InnerException, SocketException)
progress.Report((sequence, socEx.SocketErrorCode))
End If
Finally
ping.Dispose()
End Try
End Function
I'm writing a program that has two forms. One form gets the user to enter multiple values, and then does some calculations. Then it passes that information to another form However I can't figure out how to do it. Here is a relevant part of my code. To head some confusion, I am trying to pass 11 values, also initially, form 2 is not shown, and then when the values are passed from form 1 to form 2, then form 1 goes away and form 2 is the only one that shown
NOTE: This is not all my code, I don't believe all my code is required (I have 1000 lines right now) However this is the code with the information I want to be passed to the other form.
A lot of people are apparently saying that this is a duplicate of another question, however that question, he seems to already know how to pass the variables, but is just having issues with it (and even with looking at his, i cant figure it out)
Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
'declarations
Dim intNormal As Integer
Dim intChildren As Integer
Dim intBonanza As Integer
Dim intDiamond As Integer
Dim intPictureFrame As Integer
Dim intKite As Integer
Dim intCrazyT As Integer
Dim intLetterX As Integer
Dim int2PostageStamp As Integer
Dim intPick7 As Integer
Dim intJackpot As Integer
Validate()
If txtNormal1.Enabled = False Then
intNormal = intNormInput
Else
intNormal = CalcNormalBooks()
End If
If txtChildren1.Enabled = False Then
intChildren = intChildInput
Else
intChildren = calcChildrensBooks()
End If
If txtBonanza1.Enabled = False Then
intBonanza = intBonInput
Else
intBonanza = calcBonanza()
End If
If txtSpecial1.Enabled = False Then
intSpecial = intSpeInput
Else
intSpecial = calcSpecialBooks(intSpecial)
End If
If txtDiamond1.Enabled = False Then
intDiamond = intDiaInput
Else
intDiamond = calcDiamond(intSpecial)
End If
If txtPictureFrame1.Enabled = False Then
intPictureFrame = intPicInput
Else
intPictureFrame = calcPictureFrame(intSpecial)
End If
If txtKite1.Enabled = False Then
intKite = intKiteInput
Else
intKite = calcKite(intSpecial)
End If
If txtCrazyT1.Enabled = False Then
intCrazyT = intCrazyInput
Else
intCrazyT = calcCrazyT(intSpecial)
End If
If txtLetterX1.Enabled = False Then
intLetterX = intLettInput
Else
intLetterX = calcLetterX(intSpecial)
End If
If txt2PostageStamp1.Enabled = False Then
int2PostageStamp = intPostInput
Else
int2PostageStamp = CalcPostageStamp(intSpecial)
End If
If txtPick71.Enabled = False Then
intPick7 = intPickInput
Else
intPick7 = calcPick7(intSpecial)
End If
If txtJackpot1.Enabled = False Then
intJackpot = intJackInput
Else
intJackpot = calcJackpot()
End If
End Sub
Since I had almost the same requiremnt lately here is my solution:
Custom Event which fires when your 2nd Form is closing
Public Event HotKeyFormClosed As EventHandler(Of HotKeyFormClosedEventArgs)
Custom EventArgs class where you store your values you want to pass to Main Form
Public Class HotKeyFormClosedEventArgs
Inherits EventArgs
'Your properties here
Public Sub New(...) 'your params here
MyBase.New()
'set your properties here
End Sub
End Class
On 2nd Form handle FormClosed event and pass your values to EventArgs
Private Sub HotKey_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs)
RaiseEvent HotKeyFormClosed(Me, New HotKeyFormClosedEventArgs(...)) 'your params here
End Sub
On Main Form handle your custom event (here HotKeyFormClosed) and extract its values
AddHandler frmHotKey.HotKeyFormClosed, AddressOf HotKey_FormClosed;
...
Private Sub HotKey_FormClosed(sender As Object, e As HotKeyFormClosedEventArgs)
'Do stuff with values from e
End If
I have chosen the Event approach since it decouples the two forms from another.
One could easily duplicate the information on both forms, make them public and access it directly thru an object instance.
But I like the observable approach from the events more due to it gives mor flexibility (additonal forms using the same events etc.)
P.S.: I wrote my code in c# and blind entered the VB code here so be gracious.
The values/variables that a method expects to receive (specified in the method's signature) are called Parameters.
The values sent to a method when the method is called are called Arguments.
As long as the arguments used when calling a method match the parameters for that method, those values can be passed.
For example (and I'll try to apply this to your context), if you want to create an instance of a form that takes certain values, you can specify those parameters in the form's New event, like so:
Public Sub New(someInt As Integer)
'do something with someInt here
End Sub
Then when you call this method you'd pass it the arguments, like so:
Dim myInt As Integer = 10
Dim newForm As myForm = New myForm(myInt)
When I say the arguments need to match the parameters, that means the number of values, the order of those values, and the value types must be the same (or in the case of numbers the parameter's type must be the same or larger than the argument's type).
As long as that is true, then it shouldn't really matter how you pass these - you could pass 11 individual arguments, you just have to make sure you are matching the argument to the parameter.
Hope that helps!
i am using vb.net with microsoft sql
i want to retrieve data where the checkbox is selected how could i perform it?
this the code i currently have
Dim chk As New DataGridViewCheckBoxColumn()
DataGridView1.Columns.Add(chk)
chk.HeaderText = "Select"
chk.Name = "lol"
sql.RunQuery("Select ID,JobPosition,requiredQualification,Salary, description,JobExpYear from vacancy where JobPosition IN (Select JobPosition from employer where EID = '" & ID & "')")
If sql.SQLDS.Tables.Count > 0 Then
DataGridView1.DataSource = sql.SQLDS.Tables(0)
End If
i will have checkbox within my data grid, how do i call it?
I'm not sure if this is what you need, question not clear enough for me. But see if this helps:
Dim condition As String = String.Empty
For Each row As DataGridViewRow In DataGridView1.Rows
condition += DirectCast(row.Cells(0), DataGridViewCheckBoxCell).Selected.ToString + ", "
Next
If Not String.IsNullOrEmpty(condition) Then
'Remove last splitter
condition = condition.Substring(0, condition.Length - 2)
End If
Your question: "i am using vb.net with microsoft sql i want to retrieve data where the checkbox is selected how could i perform it?"
There are a few different ways that this can be accomplished. You can either one, create a function that returns if it's checked or not or create a method that does all of your logic for you, it depends on how your going to use it. According to your query your not getting any information from the DataGridView, you are saying you want to retrieve data if it's checked or not. Another question is what is the purpose of running the same query if you have multiple rows, are you going to be running different queries based on the CheckBox for that row if it's checked?
Example 1 -Create a function to see what the value is
''' <summary>
''' Returns if given column is checked or not
''' </summary>
''' <param name="dGrid"></param>
''' <param name="dRow"></param>
''' <param name="intIndex"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function IsColumnChecked(ByVal dGrid As DataGridView, ByVal dRow As DataGridViewRow, ByVal intIndex As Integer) As Boolean
Dim blnIsChecked As Boolean = False
Try
'What column is the checkboxcolumn and let's make sure its the correct type
If TypeOf (dGrid.Columns(intIndex)) Is DataGridViewCheckBoxColumn Then
If dGrid.RowCount <> 0 Then
If CBool(DirectCast(dRow.Cells(intIndex), DataGridViewCheckBoxCell).Value) Then
blnIsChecked = True
End If
Else
Return blnIsChecked
End If
Else
Return blnIsChecked
End If
Catch ex As Exception
Return blnIsChecked
End Try
Return blnIsChecked
End Function
Example 1 Useage - Check to see if given row is checked or not-
'Example ONLY on a button click...
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If IsColumnChecked(DataGridView1, DataGridView1.CurrentRow, 0) Then
MessageBox.Show("It's Checked!")
Else
MessageBox.Show("Not Checked!")
End If
End Sub
You can also put this in a method to do what you want with other logic, not sure how you are going to use it. This function is good to determine if you want to continue with other things or not and can be used in other areas of your program as well.
Good Luck!
I have an UltraGrid in which I have many columns of which 2 columns are DateTime style. Now when I use the filter of that columns it shows all the DateTime values as a text in a dropdown. But I need that as a calendar in order to make the filter easy. It is enough to show just a calendar when clicking the filter.
I have tried some code but it doesn't work.
//Code:
Private Sub grdResult_BeforeRowFilterDropDown(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.BeforeRowFilterDropDownEventArgs) Handles grdResult.BeforeRowFilterDropDown
e.Cancel = True
UltraCalendarCombo1.Visible = True
UltraCalendarCombo1.Location = New Point(grdResult.Rows.FilterRow.Cells(e.Column).GetUIElement().Rect.Location.X, grdResult.Rows.FilterRow.Cells(e.Column).GetUIElement().Rect.Location.Y - 2)
UltraCalendarCombo1.Size = New System.Drawing.Size(grdResult.Rows.FilterRow.Cells(e.Column).GetUIElement().Rect.Size.Width, grdResult.Rows.FilterRow.Cells(e.Column).GetUIElement().Rect.Size.Height)
' UltraCalendarCombo1.DroppedDown = True
End Sub
The above event will fire when the filter dropdown is clicked.
private sub applyCustomeViewSettings(byval gridFormat as GridFormat)
....
...
For Each ColumnFormat In gridFormat.ColumnFormats
For Each column In Me.grdResult.DisplayLayout.Bands(0).Columns
If column.Key.ToUpper = ColumnFormat.ColumnKey.ToUpper Then
If column.Key.ToUpper = "PCSSTDT" Then
column.Header.Caption = IIf(ColumnFormat.Caption = "", ColumnFormat.ColumnKey, ColumnFormat.Caption)
column.Hidden = ColumnFormat.Hidden
'column.AllowRowFiltering = IIf(ColumnFormat.AllowRowFiltering = False, ColumnFormat.AllowRowFiltering, DefaultableBoolean.True) 'CType(ColumnFormat.AllowRowFiltering, DefaultableBoolean)
column.Width = ColumnFormat.Width
column.Header.VisiblePosition = ColumnFormat.VisiblePosition
column.Format = ColumnFormat.Format
column.SortIndicator = ColumnFormat.SortIndicator
' column.Style = ColumnStyle.Date
'column.EditorComponent = UltraCalendarCombo1
column.FilterOperandStyle = FilterOperandStyle.Default
Else
column.Header.Caption = IIf(ColumnFormat.Caption = "", ColumnFormat.ColumnKey, ColumnFormat.Caption)
column.Hidden = ColumnFormat.Hidden
column.AllowRowFiltering = IIf(ColumnFormat.AllowRowFiltering = False, ColumnFormat.AllowRowFiltering, DefaultableBoolean.True) 'CType(ColumnFormat.AllowRowFiltering, DefaultableBoolean)
column.Width = ColumnFormat.Width
column.Header.VisiblePosition = ColumnFormat.VisiblePosition
column.Format = ColumnFormat.Format
column.SortIndicator = ColumnFormat.SortIndicator
column.Style = ColumnFormat.Style
End If
End If
Next
....
...
End Sub
The above method makes the grid changes(apply settings) to show the filter as calendar.
But this doesn't work and showing me the same normal grid.
How can I achieve this?
I have managed to show a MonthCalendar when you press the icon to filter a DateTime column in an UltraWinGrid.
When the MonthCalendar is dispayed you could select a specific date using the familiar interface provided by this standard WinForm control. After selecting the date you could use the value to apply programmatically a filter condition to the UltraWinGrid column.
To reach this result you first need to add a reference to the Infragistics4.Win.SupportsDialog.v11.2 assembly where you can find the UltraGridFilterUIProvider class
Now, in your form where you need the filtering to appear, add this code: (it is just an example because I haven't your datasource and thus I have a prebuilt one with just one datetime column)
Imports Infragistics.Win.UltraWinGrid
Imports Infragistics.Win.SupportDialogs.FilterUIProvider
Public Class Form1
' This is the key object that let us customize '
' the Filter for the UltraWinGrid'
Dim _filterUIProvider as UltraGridFilterUIProvider
' In the InitializeLayout event we substitute the normal
' filter handler with the custom filter'
Private Sub UltraGrid1_InitializeLayout(sender As Object, e As Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs) Handles UltraGrid1.InitializeLayout
e.Layout.Override.AllowRowFiltering = Infragistics.Win.DefaultableBoolean.True
_filterUIProvider = New UltraGridFilterUIProvider()
' Comment out the following line to test the default
' **Excel Filter Style Interface** '
AddHandler _filterUIProvider.BeforeMenuPopulate, AddressOf _filterUIProvider_BeforeMenuPopulate
e.Layout.Override.FilterUIProvider = _filterUIProvider
End Sub
' Before the UltraGridFilterUIProvider shows its standard form interface
' we start a custom form used to apply our filtering logic '
' and block the display of the UltraGridFilterUIProvider interface '
Private Sub _filterUIProvider_BeforeMenuPopulate(sender As Object, e As Infragistics.Win.SupportDialogs.FilterUIProvider.BeforeMenuPopulateEventArgs)
' A custom form with the MonthCalendar and 3 buttons '
' to handle the filter logic '
Using fDate = new FormDate()
' Open our custom form with the monthcalendar
if (DialogResult.OK = fDate.ShowDialog())
' We need a nullable date to allow the removing of the filter'
Dim dtFilter As DateTime? = fDate.SelectedDate
if (dtFilter.HasValue)
' Apply programmatically a filtercondition to the column
' In this case I have only one column. so I use the index 0
' in your case this should change to reflect your column index
Dim fc = new FilterCondition(FilterComparisionOperator.Equals, dtFilter.Value)
ultraGrid1.DisplayLayout.Bands(0).ColumnFilters(0).FilterConditions.Add(fc)
Else
ultraGrid1.DisplayLayout.Bands(0).ColumnFilters.ClearAllFilters()
End If
End If
End Using
e.Handled = true ' Stop the standard interface'
End Sub
End Class
Now we need only a simple form called FormDate that contains a MonthCalendar and three buttons (Set, Clear, Cancel) that return (Set) a Date to set the filter, (Clear) a null value to remove previous filter and a cancel button to abort the processing
Here the code for the form, the design is trivial
Public Class FormDate
Public Property SelectedDate As DateTime?
Private Sub FormDate_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.SelectedDate = Nothing
End Sub
Private Sub cmdSet_Click(sender As Object, e As EventArgs) Handles cmdSet.Click
'This button has DialogResult=OK'
Me.SelectedDate = monthCalendar1.SelectionStart
End Sub
Private Sub cmdClear_Click(sender As Object, e As EventArgs) Handles cmdClear.Click
'This button has DialogResult=OK'
Me.SelectedDate = Nothing
End Sub
End Class
This could resolve your problem, however, I have discoverd what seems to be a bug in UltraGridFilterUIProvider. When I call the e.Handled=True my expected result is the filter to not show anything but, instead a small window still appears and I have to press Escape to hide it. I have not found any way to automatically hide it.
It seems to be a problem to signal to the Infragistics team.
I suggest you also to test the Excel Style Filter Interface provided automatically by the UltraGridFilterUIProvider. This interface has a lot of options and is much more preferable to the standard UI filter. To test this interface you should only comment out the AddHandler line above
EDIT Following the comment from #Alhalama I have tried to use the BeforeRowFilterDropDown event and the result are better (well it is perfect now). So I have commented out the line with the AddHandler, removed the code for the BeforeMenuPopulate and added the code for the BeforeRowFilterDropDown
Private Sub UltraGrid1_BeforeRowFilterDropDown(sender As Object, e As BeforeRowFilterDropDownEventArgs) Handles UltraGrid1.BeforeRowFilterDropDown
If e.Column.Key = "DateRequest" Then
Using fDate = New FormDate()
If DialogResult.OK = fDate.ShowDialog() Then
Dim dtFilter As DateTime? = fDate.SelectedDate
If (dtFilter.HasValue) Then
Dim fc As FilterCondition = New FilterCondition(FilterComparisionOperator.Equals, dtFilter.Value)
UltraGrid1.DisplayLayout.Bands(0).ColumnFilters(0).FilterConditions.Add(fc)
Else
UltraGrid1.DisplayLayout.Bands(0).ColumnFilters.ClearAllFilters()
End If
End If
End Using
e.Cancel = True
End If
End Sub
Now, when I try to open the filter for the column named DateRequest I open immediately the FormDate and at the end I set the Cancel property of the BeforeRowFilterDropDownEventArgs to true to avoid further processing of the filter dialog. This seems to be perfect...... Great credit for this to Mr. Alhalama. If you wish I think you should post your own answer because your suggestion really makes the difference.
I am trying to get a function for a class assignment to work however as the program hits the specific line in question it dies off and nothing after this line will execute. The program does not lock up, just the current execution path dies.
I have tried running debugging but much the same happens. Once I hit the link that should call a function from the object stored in the Arraylist element the break point at the actual function that should be called is not hit and nothing further happens.
Public Structure Appliances
' Create New Appliance object
Public Sub New(name As String, pusage As Double)
aName = name
aPUsage = pusage
End Sub
' Create New Washer Object
Public Sub New(name As String, pusage As Double, wusage As Double)
aName = name
aPUsage = pusage
aWUsage = wusage
End Sub
' Functions
Public Function getAName()
Return aName
End Function
Public Function getAPUsage()
Return aPUsage
End Function
Public Function getAWUsage()
Return aWUsage
End Function
Dim aName As String ' Appliance Name
Dim aPUsage As Double ' Appliane Power Usage
Dim aWUsage As Double ' Appliance Water Usage
End Structure
...
Public Class Form1
...
Dim appList As New ArrayList() ' Create an arraylist appliance objects
Public appTemp As Appliances ' To store appliance objects until they can be added to the arraylist
...
Private Function getAppInfo()
getAppInfo = Nothing
Do While fInStream.Peek() <> -1
s = fInStream.ReadLine() ' Get a line from the file and set s to it
Dim words As String() = s.Split(New Char() {","c}) ' Split the line contents along commas and set those parts into words
words(0) = words(0).Replace("_", " ") ' Reaplce underscores with spaces
If (words.Count = 3) Then ' If words contains the washer appliance
appTemp = New Appliances(words(0), Double.Parse(words(1)), Double.Parse(words(2)))
appList.Add(appTemp)
Else ' For all other appliances
appTemp = New Appliances(words(0), Double.Parse(words(1)))
appList.Add(appTemp)
End If
Loop
End Function
Private Function setUsage(name As String)
setUsage = Nothing
' Find appliance
For i = 0 To appList.Count
If (name = appList(i).getAName()) Then
If (name = "Washer") Then
s = appList(i).getWUsage() ' !!!This is the line where the execution dies at, nothing after this line is processed and the function call is not completed
txtbGPH.Text = s
End If
MsgBox("Test 1")
Exit For
ElseIf (i = appList.Count) Then
MsgBox("Appliance could not be found")
End If
Next
End Function
End Class
Use a List(Of X) instead of ArrayList if you are going to insert only one type:
Dim appList As New List(Of Appliances)
And I recommend you to declare your temp var inside the methods unless is necessary. Anyway, in this case you don't need it, you can add your var in this way:
appList.Add(New Appliances(words(0), Double.Parse(words(1))))
With this use (using lists) you won't need to use arraylistObj.Item(i).Method() and you can simply use the common way:
s = appList(i).getWUsage()
Nevermind, I figured it out just now. I did not know that arraylists are not "arraylists" but a collection. I thought maybe it would act like other collection oriented objects and that you have to use a .item(i) to access the elements, which turns out to be the case.
txtbGPH.text = appList.item(i).getAWusage()
produces the proper behavior and the rest of the code after the problematic line indicated in the OP executes as does the break point set at the called function.