form2 combobox

Hi,i am new to programming

i am having trouble with the following.

i have a form with a splitcontainer

i have added 2 usercontrols "usercontrol1" and "usercontrol2"

i have set the usercontrols to fill splitcontainer panel2

i have a second form "form2"

i have a combobox on form2 with collection items "conttrol 1" and "control 2"

i am saving the selected text of the combobox to a user setting "startupcontrol"

if i put the combobox on form1 on splitcontainer panel1 i get the results i want.

But if i move it to form2 where belongs it does nothing.

Please see the code below.

The first part is working with the combobox on form1 splitcontainer panel1

The second part is not working with the combobox on form2.

i have my.settings.save() on form2 closing event.

PublicClass Form1

Private MyControl1As UserControl1

Private MyControl2As UserControl2

PrivateSub Form1_Load(ByVal senderAs System.Object,ByVal eAs System.EventArgs)HandlesMyBase.Load

MyControl1 =New UserControl1

MyControl2 =New UserControl2

MyControl1.Parent = SplitContainer1.Panel2

MyControl2.Parent = SplitContainer1.Panel2

MyControl1.Dock = DockStyle.Fill

MyControl2.Dock = DockStyle.Fill

MyControl1.Visible =False

MyControl2.Visible =False

IfMy.Settings.StartUpControl <>NothingThen

ComboBox1.SelectedText =My.Settings.StartUpControl

EndIf

If ComboBox1.Text ="control 1"Then

MyControl1.Visible =True

Else

MyControl1.Visible =False

EndIf

If ComboBox1.Text ="control 2"Then

MyControl2.Visible =True

Else

MyControl2.Visible =False

EndIf

EndSub

PrivateSub Form1_FormClosing(ByVal senderAs System.Object,ByVal eAs System.Windows.Forms.FormClosingEventArgs)HandlesMyBase.FormClosing

My.Settings.StartUpControl = ComboBox1.SelectedText

My.Settings.Save()

EndSub

EndClass

'the code below is not giving me the correct results.

PublicClass Form1

Private MyControl1As UserControl1

Private MyControl2As UserControl2

PrivateSub Form1_Load(ByVal senderAs System.Object,ByVal eAs System.EventArgs)HandlesMyBase.Load

MyControl1 =New UserControl1

MyControl2 =New UserControl2

MyControl1.Parent = SplitContainer1.Panel2

MyControl2.Parent = SplitContainer1.Panel2

MyControl1.Dock = DockStyle.Fill

MyControl2.Dock = DockStyle.Fill

MyControl1.Visible =False

MyControl2.Visible =False

IfMy.Settings.StartUpControl <>NothingThen

My.Forms.Form2.ComboBox1.SelectedText =My.Settings.StartUpControl

EndIf

IfMy.Forms.Form2.ComboBox1.Text ="control 1"Then

MyControl1.Visible =True

Else

MyControl1.Visible =False

EndIf

IfMy.Forms.Form2.ComboBox1.Text ="control 2"Then

MyControl2.Visible =True

Else

MyControl2.Visible =False

EndIf

EndSub

PrivateSub Form1_FormClosing(ByVal senderAs System.Object,ByVal eAs System.Windows.Forms.FormClosingEventArgs)HandlesMyBase.FormClosing

My.Settings.StartUpControl =My.Forms.Form2.ComboBox1.SelectedText <i have this line of code in the formclosing event on form2

My.Settings.Save()<i have this line of code in the formclosing event on form2

EndSub

PrivateSub OptionsToolStripMenuItem_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles OptionsToolStripMenuItem.Click

My.Forms.Form2.Show()

EndSub

EndClass

Thanks

[11052 byte] By [rod_r] at [2007-12-25]
# 1

For the code shown below, please add what I have

added and comment out what I have commented out.

Do not just cut and paste the whole thing.

Public Class Form1

Private MyControl1 As tester.UserControl1

Private MyControl2 As tester.UserControl2

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

MyControl1 = New tester.UserControl1

MyControl2 = New tester.UserControl2

MyControl1.Parent = SplitContainer1.Panel2

MyControl2.Parent = SplitContainer1.Panel2

MyControl1.Dock = DockStyle.Fill

MyControl2.Dock = DockStyle.Fill

MyControl1.Visible = False

MyControl2.Visible = False

'If My.Settings.StartUpControl <> Nothing Then

'My.Forms.Form2.ComboBox1.SelectedText = My.Settings.StartUpControl

'End If

'If My.Forms.Form2.ComboBox1.Text = "control 1" Then

'MyControl1.Visible = True

'Else

'MyControl1.Visible = False

'End If

'If My.Forms.Form2.ComboBox1.Text = "control 2" Then

'MyControl2.Visible = True

'Else

'MyControl2.Visible = False

'End If

If My.Settings.StartUpControl <> Nothing Then

If My.Settings.StartUpControl = "control 1" Then

MyControl1.Visible = True

Else

MyControl1.Visible = False

End If

If My.Settings.StartUpControl = "control 2" Then

MyControl2.Visible = True

Else

MyControl2.Visible = False

End If

End If

End Sub

Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing

' My.Settings.StartUpControl = My.Forms.Form2.ComboBox1.SelectedText '<i have this line of code in the formclosing event on form2

'My.Settings.Save() '<i have this line of code in the formclosing event on form2

End Sub

--

‘ The rest of your code in Form1 is okay

TallDude at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 2

BTW, make sure your setting has a default

value of control 1

or the thing won't work the first time through.

TallDude at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 3

Working example of this code at:

http://users.adelphia.net/~gcumbia/tester.zip

TallDude at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 4
Tall Dude's code needs a bit of work to get the Form1 instance to respond to events in Form2. Add this code to Form2:

Public Class Form2
Public Event ControlSelected()
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
RaiseEvent ControlSelected()
End Sub
...
End Class

I've added a ControlSelected event, it is fired when the user selects an entry in the dropdown list of the combobox. We now need to make Form1 aware of the events raised by the Form2 instance. Add and modify the code like this:

Public Class Form1
Private MyControl1 As tester.UserControl1
Private MyControl2 As tester.UserControl2
Private WithEvents mForm2 As Form2

Private Sub mForm2_ControlSelected() Handles mForm2.ControlSelected
MyControl1.Visible = mForm2.ComboBox1.Text = "control 1"
MyControl2.Visible = mForm2.ComboBox1.Text = "control 2"
End Sub
Private Sub mForm2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles mForm2.FormClosing
mForm2 = Nothing
End Sub
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
If mForm2 Is Nothing Then
mForm2 = New Form2
mForm2.Show()
End If
mForm2.Focus()
End Sub
...
End Class

Note the use of the WithEvents keyword and how I'm no longer using My.Forms.Form2 to reference the Form2 instance. Instead, I keep my own reference to the instance in the mForm2 field. Also note that I'm responding to the FormClosing event so I know that Form2 is no longer displayed and needs to be recreated by the ToolStripButton1_Click event handler.

nobugz at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 5

Thanks Tall Dude, This is exactly what i was trying to do.

In your opinion is this the best way to do something like this.

i will be adding about 20 usercontrols.

Thanks

rod_r at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 6

NoBugz,

I appreciate you reviewing this and your code may prove

quite valuable as this program progresses.

However, the code I provided, first, deleted the dependancy

of form1 looking at a form2 control, and secondly met the

total requirements specified in the user specification.

That is, that form2 would set a user setting. That's all.

According to the specification it does not try to immediately

change the visibilty of any control.

You did make me notice a secondary issue that may be

necessary as the program progresses.

I don't see any 'friend withevents' that may be needed

for the user controls. I'm not sure why they were not added

through the IDE.

You are the expert, so additional comments and corrections

from you are always welcome. (I've been wrong before)

TallDude at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 7

If you can sketch out the details a little bit more.

I, and hopefully Hans (Nobugz), can make some

recommendations.

Will they all go in the same spot as the first two?

Will they be in pairs like the first two, each requiring

a ComboBox?

Was NoBugz correct in thinking that the combobox

should immediately change some visibility as well

as set a setting?

In response to your last post, did you notice how the

combobox was dynamically loaded? Depending

on your answer and additional info, that technique

may be useful.

TallDude at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 8

Tall Dude,in response to your questions.

1 yes they all will go in the same spot

2 no they will not be in pairs they will all use the same combobox and usersetting

3 i have not yet tried nobugs method

What i'm doing is having form2 be my user settings form.

as my program progresses more usercontrols will be added.

not every user will use every usercontrol so iam giving them an option to select the startup control they will be using most of the time.

what i am planning on doing is just adding the "if" at the form1 load event for every usercontrol

just like the 2 in the example.

Thanks

rod_r at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 9

I would recommend:

Public Class Form1

' Add usercontrols to splitcontainer, panel2 through the IDE

' In this example, they are automatically named like:

' Usercontrol11 for the first usercontrol1

' Usercontrol21 for the first usercontrol2 etc.

' I changed the settings default value to

' UserControl11

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

If My.Settings.StartUpControl <> Nothing Then

Dim c As Control

For Each c In SplitContainer1.Panel2.Controls

If c.Name <> My.Settings.StartUpControl Then

c.Visible = False

Else

c.Visible = True

End If

Next

Else

UserControl11.Visible = True

End If

End Sub

' My toolstrip button is different than yours.

' Use your menucode here

Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click

My.Forms.Form2.Show()

End Sub

End Class

Public Class Form2

Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

My.Settings.StartUpControl = Me.ComboBox1.SelectedText

My.Settings.Save()

End Sub

Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

' Populate the ComboBox through code

' not one line at a time as before

Dim c As Control

For Each c In Form1.SplitContainer1.Panel2.Controls

ComboBox1.Items.Add(c.Name)

Next

End Sub

End Class

TallDude at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 10

I have added a close button to form2 but when i close the form it does not update the startup control.

i have the following code for the button click event

My.Settings.StartUpControl = Me.ComboBox1.SelectedText

My.Settings.Save()

Me.Close.()

if i use the x on the top right corner of the form to close the form it works fine.

i have the same code for the formclosing event of the form.

Thanks

rod_r at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 11

Let's use 'SelectedItem', and perhaps change it to the

'SelectionChangeCommitted' event.

Private Sub ComboBox1_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectionChangeCommitted

My.Settings.StartUpControl = CStr(Me.ComboBox1.SelectedItem)

My.Settings.Save()

End Sub

TallDude at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 12

Rod,

Sorry I don't have a lot of time today to look closer

at this. But we need to handle the situation if the

user selects nothing, and also, 'SelectedItem' as

in my post a few minutes ago is more reliable

than 'SelectedText'.

TallDude at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 13

I tried the selectionchangedcommitted event but nothing happened.

for some reason it is only working with the form closing event.

Thanks

rod_r at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 14

Here is what my form2.designer.vb and form2.vb look like.

If you still have problems, download tester.zip again from the

link I gave you, it is updated.

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _

Partial Class Form2

Inherits System.Windows.Forms.Form

'Form overrides dispose to clean up the component list.

<System.Diagnostics.DebuggerNonUserCode()> _

Protected Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing AndAlso components IsNot Nothing Then

components.Dispose()

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

<System.Diagnostics.DebuggerStepThrough()> _

Private Sub InitializeComponent()

Me.ComboBox1 = New System.Windows.Forms.ComboBox

Me.SuspendLayout()

'

'ComboBox1

'

Me.ComboBox1.FormattingEnabled = True

Me.ComboBox1.Location = New System.Drawing.Point(0, 0)

Me.ComboBox1.Name = "ComboBox1"

Me.ComboBox1.Size = New System.Drawing.Size(121, 21)

Me.ComboBox1.TabIndex = 0

'

'Form2

'

Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)

Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font

Me.ClientSize = New System.Drawing.Size(292, 266)

Me.Controls.Add(Me.ComboBox1)

Me.Name = "Form2"

Me.Text = "Form2"

Me.ResumeLayout(False)

End Sub

Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox

End Class

Public Class Form2

Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

' Populate the ComboBox through code

' not one line at a time as before

Dim c As Control

For Each c In Form1.SplitContainer1.Panel2.Controls

ComboBox1.Items.Add(c.Name)

Next

End Sub

Private Sub ComboBox1_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectionChangeCommitted

My.Settings.StartUpControl = CStr(Me.ComboBox1.SelectedItem)

My.Settings.Save()

End Sub

End Class

TallDude at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...