Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert a string to a condition 1

Status
Not open for further replies.

cruise95

Programmer
Oct 7, 2003
56
US
How can I convert a string into a condition?

For example, I accept a condition from the user as a string. I then apply this condition.

ie.
Dim strCondition = "3 < 4"
If (CType(strCondition, Object) = True) Then
' Perform some actions
End If


Of course this doesn't work, but where am I going wrong?

Thanx
 
Dim strCondition = CInt(3 < 4)

________________________________________________________
Zameer Abdulla
Help to find Missing people
Sharp acids corrode their own containers.
 
I guess it depends on what the user will input though. Will they always be Integers they pass in?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Thanx ZmrAbdulla...but that will only work for ints

I've even thought about a select statement...but that's out of the question

Here are som examples that the user can enter:
strCondition = "regionNumber < 3"
strCondition = "reportName.substring(1,0) = 'F'"
strCondition = "reportName.IndexOf('[UN-DEFINED]') = 0"
strCondition = "regionNumber - 6 = 0"

Any suggestions?
 
Your users will actually be typing .NET code as a condition?! I'd re-think that. How about having a drop-down list with the type of criteria (e.g. greater than, exists in, starts with etc) and write the code based on the actual input?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
ca8msm is right.. I don't think it is a good idea to ask user to enter the criteria as .net code.
There is only one chance I can assume that you are creating a .net training application.

________________________________________________________
Zameer Abdulla
Help to find Missing people
Sharp acids corrode their own containers.
 
I am creating a customized datagrid that will color a row (or cell) a certain color. So if that cell contains a Null or is somehow not valid, then that cell is colored red.

This customized datagrid is to be used by all programmers in my company...thus a customized datagrid adds consistency.

That is why I wanted to add an error condition. This condition will be different depending on what the application will do, what data it will contain, and what the programmer specifies.

Thus I do not want to use a drop down box of enumerations since this will add less flexibility. I might end up going this route in a later version though. Now I'm thinking about letting the programmer code the error himself and just specifying what the color should be. This method adds a nice learning curve/knowledge base for the coder. However, doing it for them will take a lot of processing and I will have to make a mini-compiler to evaluate the filed names (reportName, regionNumber, etc...).

Thanx for all of the input...if there is anyway to do this I would like to hear about it since I hope to explore this issue later.

What fun programming is!?!?

thanx again
 
Surely if you are customizing a grid and other programmers are involved...the correct way to go about this is to raise an Event in your customized datagrid, which passes in the datarow of the underlying datasource, and or passes in other values by reference which could indicate the row fore and backcolor

The programmer can then build in his/her own logic, and set the ByRef Variables to the correct values, so that the underlying class takes care of all the row colouring, when the custom event ends and the class code recommences.




Sweep
...if it works dont f*** with it
curse.gif
 
Good idea Sweep

I went down that road a little...but didn't get too far. I'm still too new/ignorant to get my code to work.

my custom control has three classes (but only two are important...the other is some enumeration)

The first inherits from System.Windows.Forms.DataGrid and overrides various methods. I've already added one event to this class and the event works like expected

The second class inherits from DataGridTextBoxColumn. However, I raise a CellColorEvent event in the (Protected Overloads Overrides) Paint sub. But I cannot seem to find this event in my Main Form.

Can you help me with this?
 
it is possible to execute code another user has given as a property but for that to work the code needs to be good vb.net or C#. If that is what you really want then you can have it. But perhaps there is another way. Make a class with 3 properties. 2 objects and one being the operator and then make a function that returns true or false. If you want to be more complex then you should go the first way.


Christiaan Baes
Belgium

"My new site" - Me
 
I would raise 2 events from your datagrid only. One could do a checkingrowvalue event and the other could do a checkingcellvalue event. You may need to chain fire events from your gridtextboxcolumns up to your custom datagrid. This way you will be able to expose the events you want from the custom datagrid on your form.



Sweep
...if it works, you know the rest..
curse.gif
 
Christiaan

That idea sounds intersting...but I'm still not exactly sure what you mean. Would you mind elaborating? Thanx
 
Sweep,

I'm still looking at your solution and trying to get the events so they see each other.


Here's some relevant code...I hope this helps:

UPSDatagrid

Code:
Namespace UPSControls
  Public Class UPSDataGrid
    Inherits System.Windows.Forms.DataGrid

    Private Sub formatDataGridColumn(...)
      Dim textBoxColumn As UPSControls.CustomDataGridTextBoxColumn

      Dim tmpDataTable As Data.DataTable = Me.DataSource
      Dim numCols = tmpDataTable.Columns.Count
      For i = 0 To numCols - 1
        textBoxColumn = New UPSControls.CustomDataGridTextBoxColumn()
        textBoxColumn.Alignment = HorizontalAlignment.Center
      Next
    End Sub

    ....

  End Class
End Namespace



DataGridTextBoxColumn
[/color upsdatagrid]
Code:
Namespace UPSControls

    Public Class CustomDataGridTextBoxColumn
        Inherits DataGridTextBoxColumn

        Public Event CellColorEvent()
        ...

        Protected Overloads Overrides Sub Paint(...)
        ' This event causes no error...but I cannot find it listed in the MainForm
        ' Class or the UPSDataGrid Class
            RaiseEvent CellColorEvent()

            MyBase.Paint(...)
        End Sub
    End Class
End Namespace



MainForm
[/color upsdatagrid]
Code:
Public Class MainForm
    Inherits System.Windows.Forms.Form
   ...
End Class

Thanx
 
Instead of (in the datagrid code class)
Code:
Dim textBoxColumn As UPSControls.CustomDataGridTextBoxColumn
use 
Dim WithEvents textBoxColumn As UPSControls.CustomDataGridTextBoxColumn
Then you will be able to expose the cellcolor event to the datagrid. If you then add an event to the datagrid which is exposed to the form, then that is the event into which you add custom code. You will probably need to pass a reference to the CustomdataTextboxColumn up the events.
Hope this helps.

Sweep
...if it works, you know the rest..
curse.gif
 
Thanx Sweep for your help

I have another problem that I thought I would share with you. Whenever I add the DLL to the toolbox I get an ugly default image of some gear. Do you know how I can change that image?


As far as the initial problem goes, I finaly got it so I can manipulate the cell Color from the main form. I can't put it in my other classes since they are just going to be turned into a DLL and other programmers using this data grid will not have access to those classes.

Of course they can just override those classes, but by offering them a cellColr event in the main form then they don't have to worry about it and their job is made a lot easier.


Here's what I ended up doing:
I tried the WithEvents, and I finally ended up chain fireing events from my gridtextboxcolumns to my custom datagrid, and then to the main form. I would think that their are more efficient ways to do this...but this way works.


Here's some code showing the relevant portions of what I did if you're interested:


UPSDataGrid

Code:
Namespace UPSControls
  Public Class UPSDataGrid
    Inherits System.Windows.Forms.DataGrid

    Private _errorForeColor As System.Drawing.Color = System.Drawing.Color.White
    Public ReadOnly Property ErrorForeColor() As System.Drawing.Color
      Get
        Return _errorForeColor
      End Get
    End Property

    Private _errorBackColor As System.Drawing.Color = System.Drawing.Color.Red
    Public ReadOnly Property ErrorBackColor() As System.Drawing.Color
      Get
        Return _errorBackColor
      End Get
    End Property

    Public Event CellColorEvent(ByVal obj As Object, ByVal Name As String, ByVal [source] As CurrencyManager, ByVal rowNum As Integer, ByRef BackBrush As Brush, ByRef ForeBrush As Brush)

    Private Sub formatDataGridColumn(...)
      Dim textBoxColumn As UPSControls.CustomDataGridTextBoxColumn

      Dim tmpDataTable As Data.DataTable = Me.DataSource
      Dim numCols = tmpDataTable.Columns.Count
      For i = 0 To numCols - 1
        textBoxColumn = New UPSControls.CustomDataGridTextBoxColumn()

        AddHandler textBoxColumn.CellColorEvent, AddressOf DataGridCellEventHandler
      Next
    End Sub

    Public Sub DataGridCellEventHandler(ByVal obj As Object, ByVal Name As String, ByVal [source] As CurrencyManager, ByVal rowNum As Integer, ByRef BackBrush As Brush, ByRef ForeBrush As Brush)

      RaiseEvent CellColorEvent(obj, Name, source, rowNum, BackBrush, ForeBrush)

     End Sub

    ....

  End Class
End Namespace


UPSDataGridTextCollumn

Code:
Namespace UPSControls

    Public Class CustomDataGridTextBoxColumn
      Inherits DataGridTextBoxColumn

      Public Event CellColorEvent(ByVal obj As Object, ByVal Name As String, ByVal [source] As CurrencyManager, ByVal rowNum As Integer, ByRef back As Brush, ByRef Fore As Brush)
        ...

      Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal [source] As CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean)
        Dim obj As Object
        obj = Me.GetColumnValueAtRow(source, rowNum)

        RaiseEvent CellColorEvent(obj, Me.MappingName, source, rowNum, backBrush, foreBrush)

        MyBase.Paint(...)
      End Sub
    End Class
End Namespace


MainForm

Code:
Public Class MainForm
  Inherits System.Windows.Forms.Form
    

  Public Sub CellColor(ByVal obj As Object, ByVal Name As String, ByVal [source] As CurrencyManager, ByVal rowNum As Integer, ByRef BackBrush As Brush, ByRef ForeBrush As Brush) Handles UpsDataGrid1.CellColorEvent
    If (Not (obj) Is Nothing) Then
      Dim thisTxt As String
      thisTxt = CType(obj, String).Substring(0)

      If (thisTxt = "[UN-DEFINED]") Then
        BackBrush = New System.Drawing.SolidBrush(Me.UpsDataGrid1.ErrorBackColor)
        ForeBrush = New System.Drawing.SolidBrush(Me.UpsDataGrid1.ErrorForeColor)
      End If
    End If
  End Sub
End Class
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top