Jump to content

Stringregexp return always 0


Recommended Posts

Problem with Stringinstr. Script always return zero...

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <GuiRichEdit.au3>
#include <Sound.au3>
#Include <GuiEdit.au3>
#Include <Array.au3>
#Include <String.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 623, 449, 192, 114)
$Here = GUICtrlCreateEdit("", 88, 48, 449, 201)
GUICtrlSetData(-1, "Here write. One per line!")
GUICtrlSetResizing(-1, $GUI_DOCKHCENTER+$GUI_DOCKVCENTER)
$Check = GUICtrlCreateButton("Check again!", 96, 296, 153, 57)
$Clipboard = GUICtrlCreateButton("Copy to clipboard", 312, 296, 169, 65)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

$hWnd = GUICtrlGetHandle($Here)
$sound = _SoundOpen(@WindowsDir & "\media\tada.wav")

HotKeySet("{ENTER}", "enter")

While 1
   $nMsg = GUIGetMsg()
   
   Switch $nMsg
      Case $GUI_EVENT_CLOSE
         Exit
      Case $Here

      Case $Form1
         _SoundClose($sound)
         Exit
      Case $Form1
         WinSetState("Form1", '', @SW_MINIMIZE)
      Case $Form1
         WinSetState("Form1", '', @SW_RESTORE)
      Case $Check
         ;checking()
      Case $Clipboard
         
   EndSwitch
WEnd

Func enter()

For $i=0 To 1 Step +1
$nr_linii = _GUICtrlRichEdit_GetLineCount($hWnd)
$dl_ost_linii = _GUICtrlEdit_LineLength($hWnd, $nr_linii)
$nr_linii2 = $nr_linii - 1
   If $nr_linii > 1 Then
      $txt_sprawdzam = _GUICtrlRichEdit_GetTextInLine($hWnd, $nr_linii)
      MsgBox(0, '', "Text to check: " & $txt_sprawdzam)
      $caly_edit = _GUICtrlEdit_GetText($hWnd)
      MsgBox(0, '', "Whole edit without split: " & @CRLF & $caly_edit)
      $edit_1_linia = StringRegExpReplace($caly_edit, @CRLF, " ")
      msgbox(0, '', "Whole edit in 1 line: " & @CRLF & $edit_1_linia)
      $string_skrocony = StringTrimRight($edit_1_linia, $dl_ost_linii + 1)
      msgbox(0, '', "String trimmed from last string from edit: " & $string_skrocony)
      $Hex = _StringToHex($string_skrocony)
      msgbox(0, '', $Hex)
      $check = StringInStr($txt_sprawdzam, $string_skrocony, 1, 1, 0)
      msgbox(0, '', $check)
      If $check > 0 Then
            _SoundPlay($sound, 0)
            MsgBox(0, '', "Found duplicated word " & '"' & $txt_sprawdzam & '"' & " in " & $nr_linii2 & " and " & $nr_linii & " line! Delete last line to go on!")
            $i=0
            ExitLoop
         Else
            znak_enter()
            $i=0
            ExitLoop
         EndIf
   Else
   znak_enter()
   ExitLoop
EndIf
ExitLoop
Next
   ;Endif
EndFunc

Func znak_enter()
   Send(@LF)
   $suma_linii = _GUICtrlRichEdit_GetLineCount($hWnd)
   If $suma_linii = 3 And _GUICtrlRichEdit_GetTextInLine($hWnd, 2) = '' Then
      Send("{BS}")
   Else
   EndIf
EndFunc

Script is checking last writed word with all before, if found should play sound and show msgbox. Here are a lot of msgboxes to check what is going on with strings after each step. I don't understand why I always get 0. A tried Stringregexp also - this same situation.

Any ideas?

Link to comment
Share on other sites

  • Moderators

Smigacznr1,

You are mixing functions from the GUIEdit and GUIRichEdit UDFs - hardly surprising that the code fails at some point. ;)

I think you are overcomplicating the code. As you only want one item per line, why not use a list and an input to do what you want? I came up with this as a quick example: ;)

#include <GUIConstantsEx.au3>
#Include <GuiListBox.au3>

; Create GUI
$hGUI = GUICreate("Test", 500, 500)

; Create list and input
$hList = GUICtrlCreateList("", 10, 10, 200, 200)
$hInput = GUICtrlCreateInput("", 10, 210, 200, 20)

; Create a dummy for the accelerator key
$hDummy = GUICtrlCreateDummy()

GUISetState()

; Set teh accelerator key - works like a hotkey, but only in your GUI
Local $aAccelKeys[1][2] = [["{ENTER}", $hDummy]]
GUISetAccelerators($aAccelKeys)

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hDummy
            If _GUICtrlListBox_FindInText($hList, GUICtrlRead($hInput)) = -1 Then
                ; If we do not find the text in the list - add it
                GUICtrlSetData($hList, GUICtrlRead($hInput))
            Else
                ; If we do - say so
                ConsoleWrite("Double" & @CRLF)
            EndIf
            ; Clear the input
            GUICtrlSetData($hInput, "")
    EndSwitch

WEnd

Please ask if anything is unclear. Some more errorchecking might be useful, but see if it is required first. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Please ask if anything is unclear. Some more errorchecking might be useful, but see if it is required first. :)

M23

Works great! How to add case sensitive checking?

And have question about:

; Create a dummy for the accelerator key
$hDummy = GUICtrlCreateDummy()

; Set teh accelerator key - works like a hotkey, but only in your GUI
Local $aAccelKeys[1][2] = [["{ENTER}", $hDummy]]
GUISetAccelerators($aAccelKeys)

Dummy accelerator what is this and what is for? I don't get it :/

$aAccelKeys[1][2]

From help I know that first number is number of accelerator key and second number is?

Link to comment
Share on other sites

If _GUICtrlListBox_FindInText($hList, GUICtrlRead($hInput)) = -1 Then
                ; If we do not find the text in the list - add it

In this case when I wrote aaaDDD and next word will be DDD script will alarm me that DDD word is on the list. So this function is searching match for whole or part word. I changed to:

If _GUICtrlListBox_FindString($hList, GUICtrlRead($hInput), True) = -1 Then
                ; If we do not find the text in the list - add it

Now it's searching only for whole word. How to make it case sensitive? Hmmm....

Link to comment
Share on other sites

  • Moderators

Smigacznr1,

when I wrote aaaDDD and next word will be DDD script will alarm me that DDD word is on the list

That was reason I mentioned errorchecking - good for you for finding the other function! :)

Dummy accelerator what is this and what is for? I don't get it

Accelerator keys work like HotKeys, but only when your GUI is active. They operate by firing an existing control within the GUI - so you need the 2 elements in the array to define the key itself (just as for a HotKey) and the control it is to activate. Does that explain it better? ;)

How to make it case sensitive?

How about forcing the input content into either upper or lower case - that way you only get upper or lower case additions to the list and you will get the match:

#include <GUIConstantsEx.au3>
#Include <GuiListBox.au3>

; Create GUI
$hGUI = GUICreate("Test", 500, 500)

; Create list and input
$hList = GUICtrlCreateList("", 10, 10, 200, 200)
$hInput = GUICtrlCreateInput("", 10, 210, 200, 20)

; Create a dummy for the accelerator key
$hDummy = GUICtrlCreateDummy()

GUISetState()

; Set the accelerator key - works like a hotkey, but only in your GUI
Local $aAccelKeys[1][2] = [["{ENTER}", $hDummy]]
GUISetAccelerators($aAccelKeys)

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hDummy
            ; Force input content into lower case
            $sText = StringLower(GUICtrlRead($hInput))
            If _GUICtrlListBox_FindString($hList, $sText, True) = -1 Then
                ; If we do not find the text in the list - add it
                GUICtrlSetData($hList, $sText)
            Else
                ; If we do - say so
                ConsoleWrite("Double" & @CRLF)
            EndIf
            ; Clear the input
            GUICtrlSetData($hInput, "")
    EndSwitch

WEnd

How is that? ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Accelerator keys work like HotKeys, but only when your GUI is active. They operate by firing an existing control within the GUI - so you need the 2 elements in the array to define the key itself (just as for a HotKey) and the control it is to activate. Does that explain it better? :)

$hList = GUICtrlCreateList("", 10, 10, 200, 200, BitOR($LBS_NOTIFY,$WS_VSCROLL,$WS_BORDER, $LBS_MULTIPLESEL))

$hInput = GUICtrlCreateInput("", 10, 210, 200, 20)

Second control in code is hInput so it is this like connection? First control will be higher GUICtrl writed in code?

How about forcing the input content into either upper or lower case - that way you only get upper or lower case additions to the list and you will get the match:

No I need to check words with case sensitive. I can't force input for only lower or other chars. Chars in list have to be this same like in paper from user get it (ex. barcode or ID of something).
Link to comment
Share on other sites

  • Moderators

Smigacznr1,

Second control in code is hInput so it is this like connection? First control will be higher GUICtrl writed in code?

I do not understand what this has to do with Accelerator keys. ;)

Certainly the control to which you link an Accelerator key can be an existing control (e.g. you can operate a button with it) but in this case we did not have a suitable control so I created a Dummy to do the job. :D

No I need to check words with case sensitive

Then you need to compare the strings with the == operator to force a case-sensitive comparison. ;)

But if (as I understand) you want to accept different versions of the same text (i.e you want to have both FrEd and fReD) we run into the problem of how we get the data into the list because Windows will not accept them as different if we add them separately - this script shows the problem: :D

#include <GUIConstantsEx.au3>

; Create GUI
$hGUI = GUICreate("Test", 500, 500)

$hButton_1 = GUICtrlCreateButton("Add 'FrEd'", 10, 300, 80, 30)
$hButton_2 = GUICtrlCreateButton("Add 'fReD'", 100, 300, 80, 30)

; Create list and input
$hList = GUICtrlCreateList("", 10, 10, 200, 200)
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hButton_1
            GUICtrlSetData($hList, "FrEd")
        Case $hButton_2
            GUICtrlSetData($hList, "fReD")
    EndSwitch
WEnd

To get over this you need to add the data in one large chunk like this: :D

#include <GUIConstantsEx.au3>

; Create GUI
$hGUI = GUICreate("Test", 500, 500)

$hButton_1 = GUICtrlCreateButton("Add Both", 10, 300, 80, 30)

; Create list and input
$hList = GUICtrlCreateList("", 10, 10, 200, 200)
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hButton_1
            GUICtrlSetData($hList, "|FrEd|fReD")
    EndSwitch
WEnd

To solve theis, I suggest you check every item in the list and create a single variable to refill the list after each check: :)

#include <GUIConstantsEx.au3>
#include <GuiListBox.au3>

; Create GUI
$hGUI = GUICreate("Test", 500, 500)

; Create list and input
$hList = GUICtrlCreateList("", 10, 10, 200, 200)
$hInput = GUICtrlCreateInput("", 10, 210, 200, 20)

; Create a dummy for the accelerator key
$hDummy = GUICtrlCreateDummy()

GUISetState()

; Set the accelerator key - works like a hotkey, but only in your GUI
Local $aAccelKeys[1][2] = [["{ENTER}", $hDummy]]
GUISetAccelerators($aAccelKeys)

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hDummy
            ; Read the input
            $sInputText = GUICtrlRead($hInput)
            ; Count the list elements
            $iCount = _GUICtrlListBox_GetCount($hList)
            ; Start the data with the separtor character to force a refill
            $sData = "|"
            ; Clear a flag
            $fMatch = False
            ; Go through the existing list 
            For $iIndex = 0 To $iCount - 1
                $sItemText = _GUICtrlListBox_GetText($hList, $iIndex)
                ; Add the text to the data string
                $sData &= $sItemText & "|"
                ; Now do a case sensitive comparison
                If $sItemText == $sInputText Then
                    ; Set the flag if we find a match
                    $fMatch = True
                    ConsoleWrite("Double" & @CRLF)
                EndIf
            Next
            ; If we do not find the text in the list - add it to the data and refill the list
            If Not $fMatch Then
                $sData &= $sInputText
                ; Set the data in the list
                GUICtrlSetData($hList, $sData)
            EndIf
            ; Clear the input
            GUICtrlSetData($hInput, "")
    EndSwitch

WEnd

Happy now? :D

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...