- 題名: コンボボックスで選択したら他のところが動作するようにしたい
- 日時: 2010/03/26 0:06:43
- ID: 26616
- この記事の返信元:
- (なし)
- この記事への返信:
- [26617] Re[1]: コンボボックスで選択したら他のところが動作するようにしたい2010/03/26 2:33:14
- [26621] Re[1]: コンボボックスで選択したら他のところが動作するようにしたい2010/03/26 10:48:07
- ツリーを表示
2010/03/26(Fri) 07:44:33 編集(投稿者)
2010/03/26(Fri) 07:44:09 編集(投稿者)
2010/03/26(Fri) 07:42:53 編集(投稿者)
以下の用に修正したのですが
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.Text = "日本料理" Then
ListBox1.DataSource.Nothing
mesi.Add("すし")
mesi.Add("そば")
ElseIf ComboBox1.Text = "中華料理" Then
mesi.clear()
mesi.Add("中華そば")
mesi.Add("餃子")
End If
ListBox1.DataSource = mesi
End Sub
Nothingの設定がいまいちわからないのですが....
ListBox1.DataSource.Nothingを追加すると以下のエラーがでてしまいます。
-------
System.MissingMemberException はハンドルされませんでした。
Message="公開メンバ 'Nothing' は型 'List(Of String)' に見つかりませんでした。"
Source="Microsoft.VisualBasic"
StackTrace:
場所 Microsoft.VisualBasic.CompilerServices.Symbols.Container.GetMembers(String& MemberName, Boolean ReportErrors)
場所 Microsoft.VisualBasic.CompilerServices.NewLateBinding.CallMethod(Container BaseReference, String MethodName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, BindingFlags InvocationFlags, Boolean ReportErrors, ResolutionFailure& Failure)
場所 Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn)
場所 mesi.Form1.ComboBox1_SelectedIndexChanged(Object sender, EventArgs e) 場所 C:\Documents and Settings\★\デスクトップ\sample\sample\mesi\mesi.vb:行 817
場所 System.Windows.Forms.ComboBox.OnSelectedIndexChanged(EventArgs e)
場所 System.Windows.Forms.ComboBox.WmReflectCommand(Message& m)
場所 System.Windows.Forms.ComboBox.WndProc(Message& m)
場所 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
場所 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
場所 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
場所 System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
場所 System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)
場所 System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m)
場所 System.Windows.Forms.Control.WmCommand(Message& m)
場所 System.Windows.Forms.Control.WndProc(Message& m)
場所 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
場所 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
場所 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
場所 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
場所 System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
場所 System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
場所 System.Windows.Forms.Control.DefWndProc(Message& m)
場所 System.Windows.Forms.Control.WmCommand(Message& m)
場所 System.Windows.Forms.Control.WndProc(Message& m)
場所 System.Windows.Forms.ComboBox.WndProc(Message& m)
場所 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
場所 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
場所 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
場所 System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
場所 System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
場所 System.Windows.Forms.Control.DefWndProc(Message& m)
場所 System.Windows.Forms.Control.WmKeyChar(Message& m)
場所 System.Windows.Forms.Control.WndProc(Message& m)
場所 System.Windows.Forms.ComboBox.WndProc(Message& m)
場所 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
場所 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
場所 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
場所 System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
場所 System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
場所 System.Windows.Forms.Control.DefWndProc(Message& m)
場所 System.Windows.Forms.Control.WmMouseWheel(Message& m)
場所 System.Windows.Forms.Control.WndProc(Message& m)
場所 System.Windows.Forms.ComboBox.WndProc(Message& m)
場所 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
場所 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
場所 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
場所 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
場所 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
場所 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
場所 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
場所 System.Windows.Forms.Application.Run(ApplicationContext context)
場所 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
場所 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
場所 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
場所 mesi.My.MyApplication.Main(String[] Args) 場所 17d14f5c-a337-4978-8281-53493378c1071.vb:行 81
場所 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
場所 System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
場所 System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
場所 System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
場所 System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
場所 System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
場所 System.Activator.CreateInstance(ActivationContext activationContext)
場所 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
場所 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
場所 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
場所 System.Threading.ThreadHelper.ThreadStart()
InnerException:
■No26616に返信(オプーさんの記事)
> ※コンボボックスの日本料理、中華料理、イタリア料理はVB側で追加しました
> ※タイマーは有効、時間は『1』にしています
Timer_Tick ではなく、ComboBox 側の
SelectionChangeCommitted あるいは SelectedIndexChanged イベントで
制御しましょう。
> ListBox1.DataSource = mesi
データの入れ替えのたびに DataSource を差し替えるのが面倒であれば
BindingSource を中継させると便利です。
'--- Form の Load イベント ---
Me.BindingSource1.DataSource = mesi
Me.ListBox1.DataSource = Me.BindingSource1
--- ComboBox の SelectedIndexChanged イベント ---
If ComboBox1.Text = "日本料理" Then
Me.BindingSource1.Clear()
Me.BindingSource1.Add("すし")
Me.BindingSource1.Add("そば")
ElseIf ComboBox1.Text = "中華料理" Then
Me.BindingSource1.Clear()
Me.BindingSource1.Add("中華そば")
Me.BindingSource1.Add("餃子")
End If
List(Of String) には、内容が変更されたことを通知する機能が無いため、
その内容を変更しても、ListBox の内容は変化しません。
一方、BindingSource は変更通知イベントを持っており、BindingSource 経由で
変更した結果は、ListBox と List(Of String) の両方に直接反映されるようになります。
■No26618に返信(オプーさんの記事)
> ListBox1.DataSource.Nothing
そのコードだと、DataSource が返すオブジェクトすなわち、List(Of String) の
『Nothing という名前のメソッド』を呼び出すことになってしまいます。
当然、そのような名前のメソッドはありませんから、実行時に
> Message="公開メンバ 'Nothing' は型 'List(Of String)' に見つかりませんでした。"
という例外が表示されることになるわけです。
No26617 で Hongliang さんが書かれた
>> 一旦 ListBox1.DataSource に Nothing を代入してから改めて mesi を代入する
というのは、
ListBox1.DataSource = Nothing
ListBox1.DataSource = mesi
という事です。
分類:[.NET]
コンボボックスが以下のようになっています。
-------
日本料理
中華料理
イタリア料理
-------
たとえば日本料理を選択すると
リストボックスに以下のように表示されるようにしました
------
すし
そば
------
その状態から(コンボボックスの)中華料理を選択してもなんの変化もありません。
コードは以下のとおりです。
Public Class Form1
Private mesi As New List(Of String)
ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If ComboBox1.Text = "日本料理" Then
mesi.clear ()
mesi.Add("すし")
mesi.Add("そば")
ElseIf ComboBox1.Text = "中華料理" Then
mesi.clear ()
mesi.Add("中華そば")
mesi.Add("餃子")
End If
ListBox1.DataSource = mesi
End Sub
-------------
※コンボボックスの日本料理、中華料理、イタリア料理はVB側で追加しました
※タイマーは有効、時間は『1』にしています
※試しに「ElseIf ComboBox1.Text = "中華料理" Then」の下にENDと
入力したらちゃんとENDの動作がしました
VB2008です。