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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

combobox.selectedindex = -1 error

Status
Not open for further replies.

techsmith

MIS
Jun 27, 2003
114
GB
Has anyone come across an Object reference not set to an instance of an object error that only occurs for 'regular users' when setting a combobox.selectedindex = -1[/color blue]?

I'm hoping this rings a bell without needing to provide all the context as it's a bit quirky - for instance the error only occurs every 3rd time the procedure is called.

By the way is this the .Net way to clear the selectedindex for a combobox?
 
"every 3rd time the procedure is called."
What procedure?

There is no ".NET way". -1 means only nothing selected.
 
The procedure:-
Code:
Public Sub ClearControl()
	SelectedIndex = -1
End Sub
The lookup control is cleared when a new grid row is selected and refilled if the newly selected row meets a validation check.

The pattern of behaviour looks like:
1.form load... 1st grid row selected..ClearControl() OK, set control to new val ok
2.new row selected in grid... ClearControl() fails exception passed to user as message
3.new row selected ...ClearControl() ok, set control to new val ok
4.new row selected ...ClearControl() ok, set control to new val ok
5.new row selected in grid... ClearControl() fails exception passed to user as message

I've checked the value of SelectedIndex when stepping through and it throws an exception whether SelectedIndex = -1 or a +ve value.

Thanks for looking into this
 
Wait, you missuandestood something here.
Try the next which clears a combo or a textbox:

Code:
public sub ClearControl(byref ctl as control)
  select case typeof ctl
    case is combobox
      ctype(ctl,combobox).selectedindex=-1
    case is textbox
      ctype(ctl,textbox).text=string.empty
  end select
end sub
 
If you set the text value of the combo box manually to a value that isn't in the drop down (say to like string.empty), the selected intex will be set to -1 ;)

As for the issue here though, do you happen to have the combo box bound to a data source? Or a datagrid linked to the combo box? Or is there an handled event for selection or text changed on the combo box?

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top