This is a migrated thread and some comments may be shown as answers.

Which DDL fired the Selected Value Changed?

13 Answers 160 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Matthew Link
Top achievements
Rank 1
Matthew Link asked on 12 Oct 2010, 08:13 PM
Hello,

I'm trying to implement the following code

Private Sub combo_SelectedValueChanged(ByVal sender As System.Object, ByVal e As Telerik.WinControls.UI.Data.PositionChangedEventArgs) Handles cmbManager.SelectedIndexChanged, cmbYear.SelectedIndexChanged, cmbStatus.SelectedIndexChanged, cmbProductFamily.SelectedIndexChanged, cmbCommmodity.SelectedIndexChanged
    Try
            dtData.Rows(0).Item(sender.Tag) = sender.SelectedValue
            bEdited = True
    Catch ex As Exception
        Stop
    End Try
End Sub

However the sender in this event is NOT the RadDropDownList, it is a RadListElement and I cannot find a way to figure out which dropdown the element came from.  

Can anyone assist me?  I really hope I'm just overlooking something obvious.

Thanks!
Matthew 

13 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 12 Oct 2010, 09:13 PM
hi Matthew,

Im not quite sure what you mean.
If you just want to know which RadDropDownList is firing this event, then you should split the method and create a separate event hander for each of your drop down lists. E.g.

Private Sub cmbManager_SelectedValueChanged(ByVal sender As System.Object, ByVal e As Telerik.WinControls.UI.Data.PositionChangedEventArgs) Handles cmbManager.SelectedIndexChanged, cmbYear.SelectedIndexChanged
    Try
       
    Catch ex As Exception
          
    End Try
End Sub
  
Private Sub cmbYear_SelectedValueChanged(ByVal sender As System.Object, ByVal e As Telerik.WinControls.UI.Data.PositionChangedEventArgs) Handles cmbYear.SelectedIndexChanged
    Try

    
Catch ex As Exception
       
    End Try
End Sub
Is that what you mean?
Hope that helps
Richard
0
Matthew Link
Top achievements
Rank 1
answered on 13 Oct 2010, 12:55 AM
Hi Richard,

Thanks for you input.  I am aware that I can do a method for every drop down, however I have a lot of them on the form and would prefer to have an event handler with just a couple lines of code which handles all that event for all of them, rather than have 10 or 15 events handlers that are nearly identical.

If it's not possible to link the RadListElement back to the RadDropDownList that it came from then multiple event handlers is what I will have to do.  I was just trying to avoid it :)

Matthew
0
Emanuel Varga
Top achievements
Rank 1
answered on 13 Oct 2010, 05:03 AM
Hello Mathew,

Although i have to agree with Richard on this one, if you really want to use one event handler for multiple dropdowns the simplest way would be to just check to which dropdown does the current item belong to, something like this:
private void radDropDownList1_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
{
    if (radDropDownList1.Items.Contains(((RadListElement)sender).ActiveItem))
    {
        MessageBox.Show("DropDown1");
    }
    else
    {
        MessageBox.Show("DropDown2");
    }
}

I would like to also suggest taking a look at this help article, related to RadControl Spy, to be able to see the internal structure of the controls, for easier and better understanding of the internals.

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
0
Richard Slade
Top achievements
Rank 2
answered on 13 Oct 2010, 07:22 AM
Hi Again,

this may also help http://msdn.microsoft.com/en-us/library/aa984308(VS.71).aspx
But just to try and put you off, I used to have to maintain an old .NET 1.0 program that was littered with single event handlers that handled multiple control events and it was awful to maintain. The methods had become bloated and unreadable. Much better in my opionion  to separate and make the code easier to read.

Hope that helps
Richard
0
Emanuel Varga
Top achievements
Rank 1
answered on 13 Oct 2010, 07:29 AM
Hello again,

Richard, the problem is not that, the problem is getting up the hierarchy chain from the ListElement to the RadDropDownList control, mostly because the ListElement does not expose a property like DropDownControl, which in some cases might be a good thing.
For instance let's say you are creating a RadPageView with a variable number of pages and you have to create controls dynamically based on some conditions.
On each of these pages you have a dropdown, that has similar functionality, would you link each dropdown to a different handler? (how?)

There are always cases and cases, sometimes the cleaner approach is preferred, sometimes it's not an option.

Best Regards,
Emanuel Varga
0
Richard Slade
Top achievements
Rank 2
answered on 13 Oct 2010, 07:56 AM
Hi Emanuel, :o)

I agree with you entirely regarding the issue with getting back up the hierarchy chain, but from my experiences with handling multiple controls events with one handler, I would always separate for future maintainability. Also, if someone sends you a stack trace after the system crashes later on, it's going to be harder to get to the root of the issue with this system.

I agree too that sometimes the cleaner approach is not an option, but in this case there doesn't seem to be (from what I know of this) a justification. Separate the body of the event handler (if possible) into a different method and call it from each event handler is, imo the way it should be done in general.
Cheers
Richard
0
Emanuel Varga
Top achievements
Rank 1
answered on 13 Oct 2010, 08:06 AM
Hello Richard,

I totally agree, but i'm just trying to help other people achieve their goals, i can just them offer an oppinion but withouth knowing their design and what else is happening besides the question, it's pretty hard to say that is the way to go, at best we can say that "usually this is the way to go".

//'Very off topic: In my opinion, in programming as in life the possibilities are endless.

Best Regards,
Emanuel Varga
0
Matthew Link
Top achievements
Rank 1
answered on 13 Oct 2010, 10:30 AM
Thanks for getting to the heart of the issue, Emanuel.

" the problem is getting up the hierarchy chain from the ListElement to the RadDropDownList control, mostly because the ListElement does not expose a property like DropDownControl,"

It sounds like I'm out of luck on this one, but if anyone from Telerik is watching, this would be a useful property to have.


Thanks again both of you for your feedback.

Matthew
0
Emanuel Varga
Top achievements
Rank 1
answered on 13 Oct 2010, 10:38 AM
Hello again Matthew,

It's almost the same case with the CellElement and the Grid, but there, there is a GridControl property if I'm not mistaken, anyway i'm glad you found your answer.

If you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
0
Jack
Telerik team
answered on 15 Oct 2010, 07:42 AM
Hi guys, 

This is an interesting discussion. I agree that it is a bit confusing that the sender argument of RadDropDownList.SelectedValueChanged and SelectedIndexChanged events is not the RadDropDownList itself. So, we decided to change this in our upcoming release. 

Regarding RadGridView, it uses virtualization and the cell element is not a constant. This often confused our customers and that is why we decided to remove this property. Instead, you can use the GridTableElement.GetCellElement property. Find more information in this blog article.

If you have further questions, I will be glad to answer.

Sincerely yours,
Jack
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Deborah
Top achievements
Rank 1
answered on 31 May 2011, 09:39 PM
I am using the current release and this does not appear to be changed.

In my scenario, I have a set of questions that are coming from a database and a combobox for each question's answers, which are also coming from the database.

Since I don't have any idea how many questions there will be, I am creating the dropdownlist controls in code. So I *have* to hook them up to one event handler. I am surprised that this would be considered "bad programming practice."

I am really stuck because I cannot figure out which one the user has just selected. And it does NOT appear that this was changed from the RadDropDownListElement to the RadDropDownList (which would be expected) as per the prior post in this thread.

Not sure how to proceed. Any suggestions would be welcome.
0
Jack
Telerik team
answered on 03 Jun 2011, 08:35 AM
Hi Deborah,

Thank you for pointing this issue to us. We will change the event sender in our upcoming release - Q2 2011. You can solve the issue by using the ElementTree.Control property of RadDropDownListElement. Please consider the following code snippet:
void list_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
{
    RadDropDownList dropDownList = (RadDropDownList)((RadDropDownListElement)sender).ElementTree.Control;
    //...
}
 
void list_SelectedValueChanged(object sender, EventArgs e)
{
    RadDropDownList dropDownList = (RadDropDownList)((RadDropDownListElement)sender).ElementTree.Control;
    //...
}

I updated your Telerik points. If you have further questions, do not hesitate to ask.

Greetings, Jack
the Telerik team
Q1’11 SP1 of RadControls for WinForms is available for download; also available is the Q2'11 Roadmap for Telerik Windows Forms controls.
0
Deborah
Top achievements
Rank 1
answered on 03 Jun 2011, 03:25 PM
Thanks. I will give this a try.
Tags
DropDownList
Asked by
Matthew Link
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Matthew Link
Top achievements
Rank 1
Emanuel Varga
Top achievements
Rank 1
Jack
Telerik team
Deborah
Top achievements
Rank 1
Share this question
or