vb.net 2008/2010 tabcontrol and code - vb.net

I am looking for a way to be able to use the tab control on my main form and transfer all 3 other forms onto that main form that has the tab control. Problem being is that some functions are named the same in each of the septate forms i want to bring into just one for the tab control.
Is there a way to separate the code for each form ON the main form of the tab control so i don't have to rename/recode each of the 3 forms i want to place on the main form of the tab control?
As an example:
Form1 has a function called upDateMe()
form2 has a function called upDateMe()
So now the main form would look like this:
Friend Class frmPW
Function sub upDateMe()
blah blah
End Function
Function sub upDateMe()
blah blah
End Function
so when i put both form1 and form2 code on the main form where the tab control is, its going to tell me that i have a duplicate function name.
Thanks for any help!
David

Why not add the what as a parameter to the update-sub?
I don't know what the functions are doing, but assuming that they load some data and bind Controls to it, you could provide a parameter that you can use to separate the logic/datasource.
Another possibility would be to simply rename the subs so that you can distinguish their purpose.
Last but not least you could encapsulate the whole business logic and databinding in one class/UserControl and instantiate it for every ex-form.

Consider User Controls.
There are plenty of examples available. You might want to start with this walkthrough on MSDN:
Walkthrough: Authoring a Composite Control with Visual Basic
One thing to watch for: The project type you'll create is actually 'Windows Forms Control Library', not 'Windows Control Library' like the 'Creating the Project' section, step 2 says.
You would create a User Control for each form in your project. Each of these UCs can be added to a tab on your main form and can be treated like any other standard Windows control.
For example - you can create a UC for your form1 called ctlForm1 and add it to Visual Studio's Toolbar. You can use the WinForm designer to drop an instance of your control in a tab page on your new main form and call it, say, ucForm1. To call the UC's upDateMe method you would simply call:
ucForm1.upDateMe()
You can thne create a UC for your form2, add it to your Toolbar and drop an instance of it on a different tab page on your main form (calling that instance ucForm2). You can call that control's upDateMe method like this:
ucForm2.upDateMe()
You'll want to learn about User Controls; they're a great way to encapsulate logic, events and UI elements into descrete, reusable components.

Related

Save Separate Settings for Copies of the same Form

I have a form, when I right click it, you can select properties, which opens a settings Form that interacts directly with the previous form, ie, it can change the color, size and other properties of the the original form. I want Several Copies of the original form running.
As it Stands, all the new forms take their settings from the same My.settings.
1st How Can I Save all the settings from each copy of the original form, Separately?
right now im using Form1.show(), and dim newform as form1 = new form1.
2nd, how can I make sure the settings form is interacting only with their respective original form?
right now, all the new settings Forms are only interacting with the same original form.
I have a jumble of data types saved, about 10 settings, and 2 of them are Specialized.string to save matrixes
Firstly, I recommend not using My.Settings in this case, since it only serves the purpose of storing global settings.
You'll need to store the settings in the Form1 class for each instance seperately, these could then be saved to a file or in the registry.
To your second question, in the settings form class, add a parameter to the Form.Show method, which is used to pass the instance of a main form object to the settings form.
This will tie a newly opened settings form to a particular main form. This could look like this:
Class SettingsForm
Shadows Sub Show(parentForm As Form1)
'The parentForm paramter will be the Form1 instance which will be controlled by this settings window
End Sub
End Class
You then just call the SettingsForm.Show method and pass the instance of the Form1 object you want to control to the method.

Can't show user control in form from another user control in vb.net

I currently have a windows form, this contains various user controls that can be shown in a click of a button, only showing the user control which has been selected, normally this works fine with the code
Checkworkcontrol1.Show()
Gradescontrol1.Hide()
Submissioncontrol1.Hide()
The code above is used in my windows form to display the specific user control, but i have one exception that i'm trying to implement, inside one of the user control i have a button that will show the another user control in the windows form but when i try to implement the same method it does nothing.
Mainpage.Submissioncontrol1.Show()
Mainpage.Gradescontrol1.Hide()
Mainpage.Checkworkcontrol1.Hide()
"Mainpage" being the windows form
In the user control you will need a reference to the form. From any control on the form you can call FindForm() to retrieve the form that the control is on. Also cast it to the right form type (form name) to be able to access specific members of it.
Dim frm = DirectCast(FindForm(), Mainpage) ' Where Mainpage is the name of the form.
frm.Submissioncontrol1.Show()
frm.Gradescontrol1.Hide()
frm.Checkworkcontrol1.Hide()
On the main form these controls must be Public (or Friend) to be accessible from the user control.
Also I strongly recommend to use Option Strict On and Option Explicit On as this will reveal a lot of errors at compile time. See: What do Option Strict and Option Explicit do?.
If Mainpage is the name of the form then your code cannot work, because your form is a Class, i.e. Mainpage is a type and not the form object.

How can I hide the inherited menu strip?

I've got a form that needs to inherit a base form. The problem is that the base form has a menu strip. The form I'm working on should not have any kind of menu on it.
How can I hide the inherited menu strip? Alternatively, how can I exchange the inherited menu for something I construct myself in the new form?
In the base form class, extract the menu strip creation code from InitializeComponent() into a virtual method, and then override that method in the new form. The drawback of this is you lose visual designer support for the menu in the base form.
I found another way, which I actually ended up using.
If you don't need to show the menu again in that particular form, you can initialize all the components as usual. Then simply make a public method like HideMainMenu() in your base class, that you call after all Init's are run, which only does this:
Public Sub HideMainMenu()
Me.Menu = Nothing
End Sub
You could probably show the menu again by doing
Public Sub ShowMainMenu()
Me.Menu = Me.myPrivateMainMenu
End Sub
But I haven't tried that...

How do I create a "wizard" style ui using vb.net?

The idea is that I would have a set of forms, users would click through a "forward" and "back" button, and the current form would change to a different one. My issue is that I can write code that just pops up a new form, but im not sure how to do a "replacement" of my current form. How is this usually done?
What I did recently was to create a form with buttons already in place and a large panel to contain each step. The dialog would accept an initial step in the form of a IWizStep instance, and the things would roll from there.
Each step was a class exposing a UserControl responsible for the visual aspect of the step, while the logic itself was handled by the class (it was a little more complicated that that, but that was the general idea).
The IWizStep interface, implemented by the step and accepted by the dialog, was on the lines of:
Interface IWizStep
Event StateChanged As EventHandler
ReadOnly Property Control As Control
ReadOnly Property Title As String
ReadOnly Property CanMovePrevious As Boolean
ReadOnly Property CanMoveNext As Boolean
Function MovePrevious As IWizStep
Function MoveNext As IWizStep
End Interface
To put everything together, a controller class would know how to compose the steps necessary for each given action. Therefore I had a controller for, say, "Emit Order", which needed some 10 steps, and a controller for "Emit Orders in Batch", which needed only a couple of steps.
Create a set of UserControls, and add and remove them from a Panel in a single form. (and set Dock to Fill)
You could define a user control which acts as a "wizard". It just needs the buttons you have and an array of content panels, just have it switch through the panels when the buttons are pressed assuming a certain condition is met within the controls on the panel. There's no real definitive "wizard" maker, since it's pretty easy to roll your own wizard.
You don't need to do a "replacement" of your current form really, you could just add a new one to the project. If you do need to for whatever reason, just grab the control collection with Me.Controls, copy that somewhere, and put the new controls up. When you don't need the wizard, swap them out again. It's generally best practice to make a new form however!

vb.net - problem making a call from one usercontrol to another form

I have a tabcontrol that creates tab pages from a "User Control" I created (a separate form in vb.net) using this code: (MainTab is the separate user control I created which has text boxes etc in it)
Dim tmpTab As New MainTab
myTabControl.TabPages.Add()
Dim tmpTabCount As Integer = myTabControl.TabPages.Count
myTabControl.TabPages.Item(tmpTabCount - 1).Controls.Add(tmpTab)
myTabControl.TabPages.Item(tmpTabCount - 1).Text = "Untitled"
I'm using the devexpress xtratab control so the code might look a bit different than the default vb.net tab control.
Now in my MainTab user control file file, I can't for the life of me figure out how to call a control in the form1 where the xtra tab control is placed on. "Me.Parent.Dispose" works for closing the tab when executed via the MainTab control, but that's as far as I can get for communicating with the parent from.
Does anyone know the solution? I'm not sure if I have to reference something in the MainTab user control or what in order to communicate with any objects on the default form1.
Generally speaking, I avoid making my child controls cognizant of the parent. It leads to unpleasant coupling more often than I care for.
Consider adding a custom event to your MainTab class that your form can subscribe to. When you want to pass a message to the form, your user control can invoke the method, and your form's event handler can process it accordingly. This pattern helps keep your user control pluggable into other forms by reducing its dependency on its parent.
Creating a user control event in a windows form is discussed in this MSDN article:
http://msdn.microsoft.com/en-us/library/aa302342.aspx