Link to home
Start Free TrialLog in
Avatar of Todd MacPherson
Todd MacPhersonFlag for Canada

asked on

Need help updating a label name from another form

VB.Net Vs 2003

Hi I have a textbox called txtUpdate on form7. It contains the original label name of a user defined label on the selected tab of form2

I want to be able to change the label name on form2 based on the input in a textbox called txtName on form7 while at the same time making sure that txtName on form7 does not include any other control names on form2

Here is what I have thus far but it does not seem to work properly and I do not know why.

Any help appreciated.

Thanks PB

        If butAdd.Text = "Update" Then 'in edit mode

            Dim upDateTag As String
            Dim ctrl As Control

            For Each ctrl In clsGlobals.Form2.TabControl1.SelectedTab.Controls

                If UCase(txtName.Text) = UCase(ctrl.Name) And UCase(txtUpdate.Text) <> UCase(ctrl.Name) Then
                    Beep()
                    MsgBox("Duplicate attribute names are not permitted.", MsgBoxStyle.Information)
                    Exit Sub
                ElseIf UCase(txtUpdate.Text) = UCase(ctrl.Name) Then
                    upDateTag = txtName.Text & vbCrLf
                    upDateTag = upDateTag & lstFormat.SelectedItem & vbCrLf
                    upDateTag = upDateTag & lstDefault.SelectedItem & vbCrLf
                    upDateTag = upDateTag & lstEntry.SelectedItem & vbCrLf
                    upDateTag = upDateTag & lstLife.SelectedItem
                    ctrl.Name = txtName.Text
                    ctrl.Text = txtName.Text
                    ctrl.Tag = upDateTag
                End If
            Next

            Beep()
            MsgBox("Attribute updated.", MsgBoxStyle.Information)
            Me.Close()
            Exit Sub
        End If
Avatar of Todd MacPherson
Todd MacPherson
Flag of Canada image

ASKER

FYI the line

If UCase(txtName.Text) = UCase(ctrl.Name) And UCase(txtUpdate.Text) <> UCase(ctrl.Name)

Is meant to allow the user to keep the original name when they only want to change some other textbox/listbox during an uodate.

Thanks

PB
I have modified it a bit. From what I see it works in all cases but one. When it is the first control of type label in the tab and I rename it to new name that already exists the code for duplicate checking does not fire. It does for other labels

        If butAdd.Text = "Update" Then 'in edit mode

            Dim upDateTag As String
            Dim ctrl As Control
            For Each ctrl In clsGlobals.Form2.TabControl1.SelectedTab.Controls

                If UCase(txtName.Text) = UCase(ctrl.Name) And UCase(txtUpdate.Text) <> UCase(ctrl.Name) Then
                    Beep()
                    MsgBox("Duplicate attribute names are not permitted.", MsgBoxStyle.Information)
                    Exit Sub
                ElseIf TypeOf ctrl Is Label AndAlso ctrl.Name = txtUpdate.Text Then
                    upDateTag = txtName.Text & vbCrLf
                    upDateTag = upDateTag & lstFormat.SelectedItem & vbCrLf
                    upDateTag = upDateTag & lstDefault.SelectedItem & vbCrLf
                    upDateTag = upDateTag & lstEntry.SelectedItem & vbCrLf
                    upDateTag = upDateTag & lstLife.SelectedItem
                    ctrl.Name = txtName.Text
                    ctrl.Text = txtName.Text
                    ctrl.Tag = upDateTag
                    Beep()
                    MsgBox("Attribute updated.", MsgBoxStyle.Information)
                    Me.Close()
                    Exit Sub
                End If
            Next
        End If
hmmm noticed something if the label I want to change is above another label and the labels have the same name, the duplicate checking procedure is not triggered. I know it is something simple but I can not find it.

PB
Avatar of Mike Tomlinson
Hi PBLack,

Even I'm having trouble following...

Would it be possible to upload the project so we can take a look?
http://www.ee-stuff.com/login.php
Ok before I upload anything I will try and explain what I need without using code.

1) on form2 a user can create up to 10 labels maximum with a minimum of 1 label required. All must have a unique name and unique text. In this case the label name and label text are the same.

2) a user can click on a label and it loads into form7 from which the user can change the name of the label by clicking an 'Update' button

3) other than the label there are a couple of other attributes that may or may not be updated and fed back to the tag property of the clicked label.

4) if the new name for the label matches any other label name on form2 then the update process must abort because no duplicates are allowed

5) sometimes the user may just change the 'other' attributes as described in (3) thus leaving the original label name intact. This will NOT trigger the duplication check.

I hope this explains what I am attempting to do.

Thanks

PB

I solved this on my own after many hours of trial an error. I am sure there is a more elegant way but my method now works 100% of time:

        If butAdd.Text = "Update" Then 'in edit mode

            Dim upDateTag As String
            Dim ctrl As Control
            For Each ctrl In clsGlobals.Form2.TabControl1.SelectedTab.Controls
                If UCase(txtName.Text) = UCase(ctrl.Name) AndAlso UCase(txtUpdate.Text) <> UCase(ctrl.Name) Then
                    Beep()
                    MsgBox("Duplicate attribute names are not permitted.", MsgBoxStyle.Information)
                    Exit Sub
                End If
            Next
            For Each ctrl In clsGlobals.Form2.TabControl1.SelectedTab.Controls
                If TypeOf ctrl Is Label AndAlso UCase(ctrl.Name) = UCase(txtUpdate.Text) Then
                    upDateTag = txtName.Text & vbCrLf
                    upDateTag = upDateTag & lstFormat.SelectedItem & vbCrLf
                    upDateTag = upDateTag & lstDefault.SelectedItem & vbCrLf
                    upDateTag = upDateTag & lstEntry.SelectedItem & vbCrLf
                    upDateTag = upDateTag & lstLife.SelectedItem
                    ctrl.Name = txtName.Text
                    ctrl.Text = txtName.Text
                    ctrl.Tag = upDateTag
                    Beep()
                    MsgBox("Attribute updated.", MsgBoxStyle.Information)
                    Me.Close()
                    Exit Sub
                End If
            Next
        End If

So what do I do about the points in this case?

PBLack
You solved it yourself!  Ask for a PAQ/Refund:
https://www.experts-exchange.com/Community_Support/General/
ASKER CERTIFIED SOLUTION
Avatar of kodiakbear
kodiakbear

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial