focusing a textbox

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

41 Replies - 1775 Views - Last Post: 07 February 2017 - 08:42 AM Rate Topic: -----

#1 SophieneeRichards   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 102
  • Joined: 18-March 16

focusing a textbox

Posted 31 January 2017 - 05:45 AM

Hi,

I feel like what I'm trying to do should be fairly simple! I'm writing a function that makes the current textbox read only if the program user does not have clearance to enter the data.
    Private Sub ReadOnlyFunction()
        'make controls "Read Only" if user is not QA registered.
        If FrmPassword2.TxtMname.Text = "SLE" Then
            ??? = Not vbReadOnly
        Else
            ??? = vbReadOnly
        End If
    End Sub



In this example the only user who can edit the textbox is "SLE". If I put this function directly into the code for the textbox in question I believe it would be easy, however that would add many lines of code, whereas if I can get it to do it just by calling a function which is one line of extra code then that would be much neater! In actually fact there will be way more than one user in the list of users who can edit the textboxes and every time someone leaves or joins the QA department someone will have to go into multiple places to edit the list, whereas if I can call the function then there will be just one list that needs editing.

Anyway if anyone know how I can ask the program to select the current textbox then that would be great, thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: focusing a textbox

#2 PeterH   User is offline

  • D.I.C Regular

Reputation: 59
  • View blog
  • Posts: 286
  • Joined: 03-September 09

Re: focusing a textbox

Posted 31 January 2017 - 07:34 AM

You could call a function which determines if the user has 'access', the returns a Boolean, which you can assign to the textbox.readonly property.

Perhaps something like,

textbox.readonly = NameCheck(FrmPassword2.TxtMname.Text) 

Private Function NameCheck(ByVal UserName as string) as Boolean
Select Case UserName
     Case "SLE" 
         return True 
     Case Else 
         return False  
End Select
End Function

This post has been edited by PeterH: 31 January 2017 - 07:40 AM

Was This Post Helpful? 0
  • +
  • -

#3 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14240
  • View blog
  • Posts: 57,124
  • Joined: 12-June 08

Re: focusing a textbox

Posted 31 January 2017 - 07:43 AM

Why wouldn't ya'all use '.enabled'?
Was This Post Helpful? 0
  • +
  • -

#4 SophieneeRichards   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 102
  • Joined: 18-March 16

Re: focusing a textbox

Posted 31 January 2017 - 08:44 AM

Hi PeterH

Thank you for your suggestion! What you say makes sense to me, however I thought if I put the line "txtQADocSign.ReadOnly = NameCheck(FrmPassword2.TxtMname.Text)" at the top of my sub and then sign in as a a user which is not SLE then it would make the box read only, but it didnt work :( am I putting it in the wrong place?

Private Sub txtQADocSign_DoubleClick(sender As Object, e As System.EventArgs) Handles txtQADocSign.DoubleClick
        'Only editbale if on QA register
        txtQADocSign.ReadOnly = NameCheck(FrmPassword2.TxtMname.Text)
        'Only sign off section if its completely filled out
        If txtFinalTestEngSign.Text = "" Then
            MessageBox.Show("Please make sure all previous sections have been signed off before starting section 8")
        ElseIf chkQADocPCB.Checked = False OrElse
      chkQADocPneumatic.Checked = False OrElse
      chkQADocVentSerial.Checked = False Then
            MessageBox.Show("Please check all boxes in section before signing off 8.1")
        Else : txtQADocSign.Text = FrmPassword2.TxtMname.Text 'Insert user name from login
            'record activity in Activity Log
            txtActivityLogRecorder.Text = ("Added: Signature for 8.1 QA documention check")
            'Save the Activity log data to SQL Server
            SaveToDataBaseFunction1()
        End If
    End Sub



Also do I need to change the bit that says "UserName" in your snippet to something from my code, or is it an arbitrary label? Maybe that's why it didn't work?
Was This Post Helpful? 0
  • +
  • -

#5 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1505
  • View blog
  • Posts: 3,810
  • Joined: 01-February 13

Re: focusing a textbox

Posted 31 January 2017 - 09:48 AM

I see the piece of code below and figure you must be using a second form for the login called "FrmPassword2". Is that form the default instance of the form or is that a new instance of some password form?
FrmPassword2.TxtMname.Text

Is the "FrmPassword2" form opened from your main application`s window, or is it the actual startup form of the application, the first form that shows when your app is started?

If it is opened from your main application`s window, you should be using a New instance of the password form if you are not already doing so.

The information of when and where the form is encountered/accessed will help us suggest the best methods to take.

Also, try sticking a MessageBox right after these two lines like shown below. This will let you see with a quick test, if the Text from the nName textbox is giving you the name or not.
            'Only editbale if on QA register
            txtQADocSign.ReadOnly = NameCheck(FrmPassword2.TxtMname.Text)
            MessageBox.Show(FrmPassword2.TxtMname.Text)


If the Text in the MessageBox is blank, then you know that the text is not being read from the password form. That may be because you are using a default form or the wrong instance if it. Let us know what you get when you try this.
Was This Post Helpful? 0
  • +
  • -

#6 maceysoftware   User is offline

  • Foo
  • member icon

Reputation: 354
  • View blog
  • Posts: 1,543
  • Joined: 07-September 13

Re: focusing a textbox

Posted 31 January 2017 - 10:49 AM

It is also worth mentioning that there are two properties, which can achieve this, you have mentioned one while Modi has mentioned the other.

So one is Read-only while the other is Enabled.

Now what is important is how you set and use these properties, so below I have just mocked up a little chart, down the side is the properties, and along the type the two states it can be in.

          | True      | False        |
--------------------------------------
Enabled   | Enabled   | Disabled     |
--------------------------------------
Read only | Read-only | Not Readonly |
--------------------------------------


Now before we go any further if the textbox is in read-only mode or disabled the use will not be able to modify the text, while if the control is enabled or not readonly they can. have you noticed anything? the result have the flip effect from one another, to block modification using the enabled property you have to pass false, while for read-only you pass true.

Now lets have a look at example:

 txtQADocSign.ReadOnly = NameCheck(FrmPassword2.TxtMname.Text)



Ok so from this i can see that your using the readonly property, I am also taking it for granted you copied Peter's example exactly:

Private Function NameCheck(ByVal UserName as string) as Boolean
Select Case UserName
     Case "SLE" 
         return True 
     Case Else 
         return False  
End Select
End Function


So whats wrong with this? from what you said in your first post is that you wanted the user 'SLE' to be able to modify the content? however your passing back true, which if you look up at the my chart again that would make the control read-only, if you look further it, if the user isnot 'SLE' then you are passing back false which explains why the box is not read-only for anyone else.

However IronRazors comments are also spot on and this could cause your problems if your not creating a instance of the form.

This post has been edited by maceysoftware: 31 January 2017 - 03:19 PM

Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is offline

  • So Smart! S-M-R-T!
  • member icon

Reputation: 6578
  • View blog
  • Posts: 26,719
  • Joined: 12-December 12

Re: focusing a textbox

Posted 31 January 2017 - 11:13 AM

A disabled (Enabled = False) TextBox is completely inert, it also appears slightly faded.

A ReadOnly TextBox can still be focused and its text can be copied.
Was This Post Helpful? 0
  • +
  • -

#8 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1505
  • View blog
  • Posts: 3,810
  • Joined: 01-February 13

Re: focusing a textbox

Posted 31 January 2017 - 11:19 AM

Nice catch maceysoftware, i did not see that.

@ SophieneeRichards,
If there is only one or two passwords or names that can enable this textbox, you could get rid of the function all together. Just use something like this instead.
txtQADocSign.ReadOnly = (FrmPassword2.TxtMname.Text.Trim <> "SLE")

If the textbox`s text is anything other than "SLE", it will set the ReadOnly property to True which will stop the user from typing in it. If the text is "SLE", it will be set to False which allows the user to type in it.
Was This Post Helpful? 0
  • +
  • -

#9 SophieneeRichards   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 102
  • Joined: 18-March 16

Re: focusing a textbox

Posted 31 January 2017 - 11:20 AM

IronRazer and maceysoftware, Thank you for your replies,

The frmPassword2 is that start-up form. I created the message box and placed it where you suggested and using both my SLE login and Sophie login, both times it told me my username in the pop-up message window. So I guess I'm using the right instance?

maceysoftware, I think I need to take it bit longer to get my head exactly around your chart- I know its probably simple really! Thanks
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw   User is offline

  • So Smart! S-M-R-T!
  • member icon

Reputation: 6578
  • View blog
  • Posts: 26,719
  • Joined: 12-December 12

Re: focusing a textbox

Posted 31 January 2017 - 11:20 AM

It is odd, though, that you are reading "SLE" from another TextBox. Can't I just edit that text to gain access?! The property should be read from a model somewhere, not from a UI property.
Was This Post Helpful? 0
  • +
  • -

#11 SophieneeRichards   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 102
  • Joined: 18-March 16

Re: focusing a textbox

Posted 31 January 2017 - 11:27 AM

Ironrazor, unfortunately there are quite a few people in the QA dept and they have a tenancy to come and go so the list will be a lot longer than 1 or 2 users. By using a function that means the list only needs to be edited in one instance. The function will actually need to be called 38 times in the code, so if I did your code
txtQADocSign.ReadOnly = (FrmPassword2.TxtMname.Text.Trim <> "SLE")



Then it would be a lot of work each time the staff changed.

Thanks for the idea though!!

andrewsw, The user SLE also has a password, oh and the box is already read only- you can only populate it by double clicking- is thats the issue????!! I just realised, when I populate the box with "SLE" it is grey (read only) and when i populate it with "Sophie" the box goes white... If thats the issue can I solve it by using enable/disable?
Was This Post Helpful? 0
  • +
  • -

#12 PeterH   User is offline

  • D.I.C Regular

Reputation: 59
  • View blog
  • Posts: 286
  • Joined: 03-September 09

Re: focusing a textbox

Posted 31 January 2017 - 12:08 PM

Following on from all the good advice given so far, you might need to also consider how you check the user name e.g. could it be 'sle', 'Sle', SLe' etc. etc. Perhaps consider changing the incoming value to uppercase when you do the test against your hard coded values.

Private Function NameCheck(ByVal UserName as string) as Boolean 

 Select Case UserName.toupper
      Case "SLE"
          return True  

      Case Else  

          return False   

 End Select 

 End Function 

This post has been edited by PeterH: 31 January 2017 - 12:11 PM

Was This Post Helpful? 0
  • +
  • -

#13 andrewsw   User is offline

  • So Smart! S-M-R-T!
  • member icon

Reputation: 6578
  • View blog
  • Posts: 26,719
  • Joined: 12-December 12

Re: focusing a textbox

Posted 31 January 2017 - 12:12 PM

Quote

I just realised, when I populate the box with "SLE" it is grey (read only) and when i populate it with "Sophie" the box goes white...

This doesn't happen automatically, there must be some code that does this.
Was This Post Helpful? 0
  • +
  • -

#14 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1505
  • View blog
  • Posts: 3,810
  • Joined: 01-February 13

Re: focusing a textbox

Posted 31 January 2017 - 12:25 PM

Quote

Ironrazor, unfortunately there are quite a few people in the QA dept and they have a tenancy to come and go so the list will be a lot longer than 1 or 2 users.


You said that the only name that should have access to the TextBox was "SLE", so if that was the case, then it would work. If there are other users that should have access to this textbox too, then the best method would depend on where and how you are storing and accessing the names.

So where are these names stored that you compare the current name to? Are they in a database or ...???
Was This Post Helpful? 0
  • +
  • -

#15 SophieneeRichards   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 102
  • Joined: 18-March 16

Re: focusing a textbox

Posted 31 January 2017 - 01:46 PM

Ok, I've added in the bit which changed the username to upper as PeterH suggested and that seemed fine.

I've also changed it so that the textbox is either enabled or disabled instead of read only, as the textbox is already 'read only' and can only be populated by double clicking, when it automatically populates the username from the login. That is why its grey to start with and i assume it went white (not read only) when I entered the "Sophie" username- it was like a double negative. Read-only being made read only must equal not read only.

Anyway making it enabled/disabled caused it to work when I entered "SLE" but when I entered "Sophie" it disabled the box- but with the text "Sophie" in the box!! I need it to not put the text in the box if that's the case.

I thought I could solve it like this, but it didn't work.

Private Sub txtQADocSign_DoubleClick(sender As Object, e As System.EventArgs) Handles txtQADocSign.DoubleClick
        'Only editbale if on QA register
        txtQADocSign.Enabled = NameCheck(FrmPassword2.TxtMname.Text)
        If txtQADocSign.Enabled = False Then
            txtQADocSign.Text = ""
        End If
        MessageBox.Show(FrmPassword2.TxtMname.Text)
        'Only sign off section if its completely filled out
        If txtFinalTestEngSign.Text = "" Then
            MessageBox.Show("Please make sure all previous sections have been signed off before starting section 8")
        ElseIf chkQADocPCB.Checked = False OrElse
      chkQADocPneumatic.Checked = False OrElse
      chkQADocVentSerial.Checked = False Then
            MessageBox.Show("Please check all boxes in section before signing off 8.1")
        Else : txtQADocSign.Text = FrmPassword2.TxtMname.Text 'Insert user name from login
            'record activity in Activity Log
            txtActivityLogRecorder.Text = ("Added: Signature for 8.1 QA documention check")
            'Save the Activity log data to SQL Server
            SaveToDataBaseFunction1()
        End If
    End Sub



The bit i added was:

If txtQADocSign.Enabled = False Then
            txtQADocSign.Text = ""
        End If



But it still disabled the box and populated it with "Sophie". Any further suggestions would be much appreciated!

Also, Ironrazor, I was thinking that the list of users would just be held in the Private Function, and be changed manually by a programmer in future if needed. However I could look into putting the names into a database. Currently all the logins are stored in a central place on the server, and I have asked the company I'm writing for if we can access those or do I need to make my own. There is also another programmer writing other data recorders for the company and he uses his own list of users and passwords for his program. I'd personally rather use the central server database, because otherwise they have to add new users in umpteen different places every time someone new joins the company! Anyway its kind of a can of worms :( but there is less then 10 people in QA so I thought it would just be easiest to have the list in the function. Does that make sense?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3