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

Adding a textbox to the menu

1 Answer 311 Views
Menu
This is a migrated thread and some comments may be shown as answers.
William
Top achievements
Rank 1
William asked on 24 Nov 2008, 05:14 PM
I'm using the following straight from the help file to load a textboxelement to a RadMenu of:
(VS2008 VB.NET)

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
 Dim textItem As New RadMenuContentItem()
 Dim textBox As New RadTextBoxElement()
 textBox.Text = "Enter text here"
 textBox.MinSize = New Size(100, 0)
 textItem.ContentElement = textBox
 textItem.ContentElement.AutoSizeMode = RadAutoSizeMode.WrapAroundChildren
 radMenu1.Items.Add(textItem)
 Dim buttonItem As New RadMenuContentItem()
 Dim button As New RadButtonElement()
 button.Text = "OK"
 AddHandler button.Click, AddressOf button_Click
 buttonItem.ContentElement = button
 radMenu1.Items.Add(buttonItem)
End Sub
Sub button_Click(ByVal sender As Object, ByVal e As EventArgs)
 Dim contentItem As RadMenuContentItem = TryCast(radMenu1.Items(0), RadMenuContentItem)
 Dim textBox As RadTextBoxElement = TryCast(contentItem.ContentElement, RadTextBoxElement)
 MessageBox.Show("Text is: " + textBox.Text)
End Sub

However I get the following error:
at radMenu1.Items.Add(textItem)

What do I need to do differently?
Thanks


System.NullReferenceException was unhandled
  Message="Object reference not set to an instance of an object."
  Source="Telerik.WinControls.UI"
  StackTrace:
       at Telerik.WinControls.UI.RadMenuContentItem.ArrangeOverride(SizeF finalSize)
       at Telerik.WinControls.RadElement.ArrangeCore(RectangleF finalRect)
       at Telerik.WinControls.RadElement.Arrange(RectangleF finalRect)
       at Telerik.WinControls.Layout.WrapLayoutPanel.arrangeLine(Single v, Single lineV, Int32 start, Int32 end, Boolean useItemU, Single itemU)
       at Telerik.WinControls.Layout.WrapLayoutPanel.ArrangeOverride(SizeF finalSize)
       at Telerik.WinControls.RadElement.ArrangeCore(RectangleF finalRect)
       at Telerik.WinControls.RadElement.Arrange(RectangleF finalRect)
       at Telerik.WinControls.RadElement.ArrangeOverride(SizeF finalSize)
       at Telerik.WinControls.RadElement.ArrangeCore(RectangleF finalRect)
       at Telerik.WinControls.RadElement.Arrange(RectangleF finalRect)
       at Telerik.WinControls.RootRadElement.ArrangeOverride(SizeF finalSize)
       at Telerik.WinControls.RootRadElement.ArrangeCore(RectangleF finalRect)
       at Telerik.WinControls.RadElement.Arrange(RectangleF finalRect)
       at Telerik.WinControls.Layouts.ContextLayoutManager.UpdateLayout()
       at Telerik.WinControls.RadElement.UpdateLayout()
       at Telerik.WinControls.ComponentLayoutElementTree.GetPreferredSize(Size sizeConstraints)
       at Telerik.WinControls.RadControl.GetPreferredSize(Size proposedSize)
       at System.Windows.Forms.Layout.DefaultLayout.xGetDockedSize(IArrangedElement element, Size remainingSize, Size constraints, Boolean measureOnly)
       at System.Windows.Forms.Layout.DefaultLayout.GetVerticalDockedSize(IArrangedElement element, Size remainingSize, Boolean measureOnly)
       at System.Windows.Forms.Layout.DefaultLayout.LayoutDockedControls(IArrangedElement container, Boolean measureOnly)
       at System.Windows.Forms.Layout.DefaultLayout.xLayout(IArrangedElement container, Boolean measureOnly, Size& preferredSize)
       at System.Windows.Forms.Layout.DefaultLayout.LayoutCore(IArrangedElement container, LayoutEventArgs args)
       at System.Windows.Forms.Layout.LayoutEngine.Layout(Object container, LayoutEventArgs layoutEventArgs)
       at System.Windows.Forms.Control.OnLayout(LayoutEventArgs levent)
       at System.Windows.Forms.ScrollableControl.OnLayout(LayoutEventArgs levent)
       at System.Windows.Forms.ContainerControl.OnLayout(LayoutEventArgs e)
       at System.Windows.Forms.Control.PerformLayout(LayoutEventArgs args)
       at System.Windows.Forms.Control.System.Windows.Forms.Layout.IArrangedElement.PerformLayout(IArrangedElement affectedElement, String affectedProperty)
       at System.Windows.Forms.Layout.LayoutTransaction.DoLayout(IArrangedElement elementToLayout, IArrangedElement elementCausingLayout, String property)
       at System.Windows.Forms.Control.PerformLayout(LayoutEventArgs args)
       at System.Windows.Forms.Control.System.Windows.Forms.Layout.IArrangedElement.PerformLayout(IArrangedElement affectedElement, String affectedProperty)
       at System.Windows.Forms.Layout.LayoutTransaction.DoLayout(IArrangedElement elementToLayout, IArrangedElement elementCausingLayout, String property)
       at System.Windows.Forms.Control.ControlCollection.Add(Control value)
       at Telerik.WinControls.RadControl.RegisterHostedControl(RadHostItem hostElement)
       at Telerik.WinControls.RadHostItem.PerformRoutedEventAction(RadElement sender, RoutedEventArgs args)
       at Telerik.WinControls.RadHostItem.OnTunnelEvent(RadElement sender, RoutedEventArgs args)
       at Telerik.WinControls.UI.RadTextBoxItem.OnTunnelEvent(RadElement sender, RoutedEventArgs args)
       at Telerik.WinControls.RadElement.RaiseTunnelEvent(RadElement sender, RoutedEventArgs args)
       at Telerik.WinControls.RadElement.RaiseTunnelEvent(RadElement sender, RoutedEventArgs args)
       at Telerik.WinControls.RadElement.RaiseTunnelEvent(RadElement sender, RoutedEventArgs args)
       at Telerik.WinControls.RadElement.SetParent(RadElement parent)
       at Telerik.WinControls.RadElement.ChangeCollection(RadElement child, ItemsChangeOperation changeOperation)
       at Telerik.WinControls.RadElementCollection.OnInsertComplete(Int32 index, Object value)
       at System.Collections.CollectionBase.System.Collections.IList.Insert(Int32 index, Object value)
       at Telerik.WinControls.RadItemOwnerCollection.OnInsertComplete(Int32 index, Object value)
       at System.Collections.CollectionBase.System.Collections.IList.Add(Object value)
       at Telerik.WinControls.RadItemCollection.Add(RadItem value)
       at Biosketch_2.PubMedGrid.PubMedGrid_Load(Object sender, EventArgs e) in C:\vs2008\Biosketch_2\Biosketch_2\PubMedGrid.vb:line 1729
       at System.Windows.Forms.UserControl.OnLoad(EventArgs e)
       at System.Windows.Forms.UserControl.OnCreateControl()
       at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       at System.Windows.Forms.Control.CreateControl()
       at System.Windows.Forms.Control.ControlCollection.Add(Control value)
       at Biosketch_2.MainForm.MainForm_Load(Object sender, EventArgs e) in C:\vs2008\Biosketch_2\Biosketch_2\MainForm.vb:line 77
       at System.EventHandler.Invoke(Object sender, EventArgs e)
       at System.Windows.Forms.Form.OnLoad(EventArgs e)
       at System.Windows.Forms.Form.OnCreateControl()
       at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       at System.Windows.Forms.Control.CreateControl()
       at System.Windows.Forms.Control.WmShowWindow(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.ContainerControl.WndProc(Message& m)
       at System.Windows.Forms.Form.WmShowWindow(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at Telerik.WinControls.RadFormBase.WndProc(Message& msg)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
       at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
       at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
       at System.Windows.Forms.Control.set_Visible(Boolean value)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at Biosketch_2.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:

1 Answer, 1 is accepted

Sort by
0
Accepted
Nikolay
Telerik team
answered on 26 Nov 2008, 05:37 PM
Hello William,

Thank you for the question.

The reason for the experienced behavior is that the help article refers to a version of RadMenu older than the latest Q3 2008. This article will be updated as soon as possible. The correct code snippet is:
Private Sub Form1_Load(ByVal sender As ObjectByVal e As EventArgs)  
    Dim textBox As RadTextBoxElement = New RadTextBoxElement()  
    textBox.Text = "Enter text here" 
    textBox.MinSize = New Size(100, 0)  
    radMenu1.Items.Add(textBox)  
    Dim button As RadButtonElement = New RadButtonElement()  
    button.Text = "OK" 
    button.MaxSize = New Size(0, 20)  
    AddHandler button.Click, AddressOf button_Click  
    radMenu1.Items.Add(button)  
End Sub 
Private Sub button_Click(ByVal sender As ObjectByVal e As EventArgs)  
    Dim textBox As RadTextBoxElement = TryCast(radMenu1.Items(1), RadTextBoxElement)  
    MessageBox.Show("Text is: " & textBox.Text)  
End Sub 

I have updated your Telerik points account for this report. If you have additional questions, feel free to contact me.

Best wishes,
Nikolay
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Menu
Asked by
William
Top achievements
Rank 1
Answers by
Nikolay
Telerik team
Share this question
or