How to add existing winform to multiple tabpages - vb.net

I want to add the same Form in project to five tabpages in TabControl.
This is what i have tried
For i As Integer = 0 To 4
'XtraTabPage instance (DevXpress)
Dim page As New XtraTabPage()
'ExamsTab is the existing form that i want to add tho the page
Dim fm As New ExamsTab
fm.TopLevel = False
fm.Dock = DockStyle.Fill
page.Controls.Add(fm)
fm.BringToFront()
fm.Show()
'Adding the page to the TabControl in Exams_Class
Exams_Class.ExamsTabControl.TabPages.Add(page)
Next
This only adds the form to the last tabpage but doesn't add to others.

If that didn't work I'd try page.Controls.AddRange(fm.Controls) but I'm not in a position to test it.
In the past when I've wanted to keep my code better organized into one form per tab I've:
made a new form
put a tabcontrol on it with a single page, call the control TheTabControl for example
put all the controls into the tab page, put all the events etc in the form code
put a line of code that retrieves the tab:
Public Function GetTab() As TabPage
Return Me.TheTabControl.TabPages(0)
End Function
on the main form, with the one tabcontrol that should contain all the other tab pages, have a code like:
MainTabCobtrol.TabPages.Add(New SomeOtherForm().GetTab())
This way all the controls and their event handlers stay in one form (a good thing) but you can combine them together into one Tab Control on another form. Putting them inside a tab means you can get an idea of how to size it, and you can specify in the designer other things like the tab name etc
From a code organising perspective, it's something the designer can more easily cope with than having all the TabPages in one. Control and all the event handlers etc (makes for a fairly monster form code behind and it doesn't respond well to being split into partial classes)
If you want to add a tab control to the existing form the Document Outline window might be helpful for moving the controls into it with drag and drop. You could also cut all the controls, add a tab and paste, but check the eventbhabdlers are all still wired up afterwards- I find that sometimes this makes them go missing

Related

Changing a control's value in a particular Tab of a TabStrip

Basically, I have a multipage for different departments of information called mpageTabsMain.
Inside each page of the multipage is a tabstrip, that contains tabs that represents different people.
What i'm doing for testing right now is using a button click to fill in a textbox's text on my 2nd page of the multipage.
mpageTabsMain.Pages(1).TextBox1.Text = "test"
However, what this does is it fills in the TextBox1's text for every Tab (person) in the TabStrip.
How do I refer to a particular tab (person)'s control in a TabStrip so that only that one is filled?
Thanks.
1) You can't have the same control names within a UserForm.
2) If you want to handle different controls for each page (e.g. TextBox1 on page1, TextBox2 on page2) you should use a Multipage control and maybe this would be the better choice for your requirements.
3) A Tabstrip control makes sense for pages with a similar or identical layout, but uses ONE set of controls only. So if you assign a value to your TextBox1.Text property without further conditions
(i.e. distinguishing between the different kinds of pages) you'll get the same text for the same TextBox1 control independantly of which page you are addressing. Though a Tabstrip can possibly
facilitate to write comparable code for only a few controls (distinguishing at which page you are), it could be the harder part to code.
Addendum (refers to comment below)
OK, you are using a TabStrip within a Multipage and apparently you want to display a tabstrip tab for every person. (BTW, I wouldn't do that, you could just change the tab name dynamically, but that isn't the question).
There is no more than one unique TextBox1 control in the whole UserForm. As TextBox1 is a unique Name, and instead your long code line it's sufficient to assign a string value via Me.TextBox1.Text = "test content for Person A" referring to a current tab view of Person A or TextBox1.Text = "test content for Person B" according to another tab view and another tab value. But it's the same TextBox1 in any case. Me stands for the UserForm itself inside the Userform code module.
You only 'fake' the view to this text (control) depending on the tab situation and are responsible to write it back properly or save it according to the actual tab view :-;

How can a ContextMenuToolStripItem be displayed, without doing it programmatically?

I've taken over a project, which contains some UltraWinGrid controls on some of the forms.
On one of the original forms which has been there since before I took on the project, there is a ContextMenuStrip, which has just one ContextMenuToolstripItem on it, captioned 'Default for Column'.
This ContextMenuStrip is hidden by default, however, when the user right-clicks on the UltraGrid, the ContextMenuStripItem is displayed on the grid (rather than at the top of them form where the ContextMenuStrip is).
I'm trying to replicate this behaviour in a new form that I've added myself, however, after adding the ContextMenuToolstripItem in, I cannot get it to display when right-clicking my grid.
I've put a breakpoint on every subroutine in the in the older forms class, and none of them are triggered when right-clicking it.
Additionally I have also searched the class for DefaultForColumnToolstripMenuItem, and the only place it appears is in the Click event of the ContextMenuToolStripItem itself.
So, how else is it possible to show it when right-clicking the grid? I've copy and pasted the ContextMenuStrip from the old form onto the new form and still nothing occurred.
I'm assuming it's in a property somewhere that I'm missing?
Check for a ContextMenuStrip property on the UltraWinGrid. You can set it in the designer and your context menu will appear on right click without having to deal with the MouseClick handler, checking the mousebutton used, etc.

Loading new form within a navigation tab in ms Access 2010

I have created the following navigation structure in a Microsoft access 2010 database:
You can see that there are two tabs, and the data for the tabs is populated with information linked to the ClientID, which is stored in an un-editable textbox at the top of the form. When the Communications Forms tab is selected, a list of communications forms that have been completed for the specific ClientID is shown. And there is a button to create a new form. My question is how do I write the macro so that clicking on the Create New Form button will cause a blank new form to be loaded in the space that is currently occupied by the List of Forms?
Below is what I have so far. It sends the user to a new form instead of embedding the new form underneath the Communication Forms tab in the current form. How can I change the below so that the blank new form is loaded under the Communication Forms tab in the current form, so that all the navigation controls remain visible/usable?
EDIT:
To address HK1's assumptions below, I am adding the following description of the steps I took to create the form in the screenshot above:
1.) I created a blank form in design view.
2.) I added a listbox to list client fullname and id, and a textbox to filter the listbox.
3.) I added the clientid and fullname textboxes to the form, and set them to change based on
what the user selects from the listbox
4.) I dragged a navigation control onto the form next to the listbox
5.) I dragged a form called "ListOfForms" onto a new tab in the navigation control to create the tab
6.) I added the CreateNewForm command button to the ListOfForms form while embedded in the main form
Here is the result of HK1's suggested code:
While I appreciate it, it does not do what I need. You can see that it just adds an additional row to the list in ListOfForms. Instead, I need the code to place a blank MyForm in the place of MainForm where ListOfForms is currently located. Thus, under the CommunicationsForms tab, all the user would see would be a blank MyForm object, which is a different form than ListOfForms.
When I click on the place where ListOfForms is located in Layout View, I see that it is called NavigationSubForm in the Property Sheet. Thus, NavigationSubForm would swap in MyForm in place of ListOfForms when the user clicks on the Create New Form command button. But if the user clicks on CommunicationForms tab again, ListOfForms would again be placed in NavigationSubForm.
I tried the following:
Private Sub cmdCreateNewForm_Click()
Forms!MainForm!NavigationSubform = MyForm
End Sub
But it generates a Runtime Error '438': Object doesn't support this property or method.
Next, I tried:
Private Sub cmdCreateNewForm_Click()
Forms!MainForm.NavigationSubform.SourceObject = MyForm
End Sub
This causes the NavigationSubForm to go blank, so that ListOfForms disappears. This seems like I am on the correct track, but what do I need to do to get it to put a blank MyForm in the NavigationSubForm instead of just an empty space?
The form you have labeled as "List of Forms" appears to be what's called a continuous form, and I'm guessing it's inside a subform control located on a another form. I'm also guessing that your Create New Form button is probably located on the header section of your continuous form. I'm also making the assumption that your continuous form/subform is bound to an editable recordset/recordsource but there's no way I can tell this by looking at the screenshot.
With these assumptions in place, the code for your Create New Form button would probably be something like this:
Private Sub cmdCreateNewForm_Click()
Me.AllowAdditions = True
DoCmd.GoToRecord, , acNewRec
End Sub
If any of my assumptions above are incorrect than it's likely this code won't work as expected.
Be aware that New Records in continuous forms (and datasheet forms) always appear at the bottom of the list. I'm not aware of any easy way of making them appear anywhere else.

Making multiple forms appear as one in VB.NET

I am writing a Windows Forms application in VB.NET. I have three forms: the main form, which shows a list of accounts, the account form which allows the user to view/edit the information for a specific account, and the policy form which allows the user to view/edit the information on a specific policy for that account. I want the forms to appear as if they are all the same window. Example: when the application starts, the user clicks an account name in the list box on the main form and clicks "edit". What I want to happen is that the window stays in the exact same place and stays the same exact size, only the content of the main form appears to be replaced with the content of the account form. Same thing if the user then chooses to edit a policy from the account form. When the user finishes and clicks "save", the main form comes back up. Through this entire use case, it would appear to the user as if they were viewing the same window the entire time, with the content of that window changing.
How can I do this? I have tried something like:
Dim newForm as New AcctForm
newForm.Location = Me.Location
newForm.Show()
Me.Close()
The problem is that if the user moves the original window, the new window appears where the parent form originally appeared, not where it ended up.
I see this is already in the comments, but what I have done in this case in the past is build each "form" in the application as a custom control. Then I have one actual form, and navigation works by changing which custom control is currently loaded on the parent form. To move from one screen/view to another, you remove the current custom control from the form's controls collection and add the new custom control.
I believe this is superior to manually setting the startup position and size, because you can use the form's .SuspendLayout()/.ResumeLayout() methods to hide the interim state, where there is no control loaded, from the user. This is harder to do when you want one form to be completely replaced by another.
This also makes it easy to set certain form properties in one place and have them be consistent for the application. You can even have an area on the form with controls that will now show in every view.
When using this pattern, I typically have each of my custom controls inherit from a common base. You may not have anything specific you will do with that base at the outset, but it almost always comes in handy later.
Finally, switching to use this scheme is easier than you think. Just go to the code for the each of your current forms, and you will find that each class currently inherits from System.Windows.Forms.Form. Most of the time, all you really need to do is change them to inherit from System.Windows.Forms.Panel and you're most of the way there.
As others have said, it may be better to redesign your application using custom controls or panels etc.
However, to answer your question regarding the seemingly random location of your forms, the first thing to check is that each form has it's StartPosition property set to Manual.
If your main form is resizable, then I would also add code to adjust newForm to the same size too.
I hope that helps with your immediate issues; so that you can move on to redesigning the application!
good morning there is another way . set property for second form to (top most) and use also
from2.show();
that make you switch between forms and keep form2 top other
Thanks
try using ShowDialog()
Dim newForm as New AcctForm
newForm.Location = Me.Location
newForm.ShowDialog()
Me.Close() <-- removed this

Option Group frame: can I add text boxes that are part of the frame instead of rad button options?

Ok so this maybe a simple/silly question but I don't know so here goes:
In access let's say I want to have a frame control, so I click the option group button and add it to the desgin surface. However, I am not wanting to use this as a option group with radio button selection, instead I would like to add text boxes instead the frame, so that when I reference the frame, it references every control instead of it, hence the text boxes, cbo boxes, etc.....just as it would if they were radio option selections.
So can you do this?
I want whatever controls I add inside the frame to be easily referenced (i.e. make all controls visible just by using frameExample.visible = true) so that I can build my own tab control groupings.....
can this be done?
Thanks!
EDIT:
What I am trying to accomplish is having a form that includes a collection of controls (input controls - cbo boxes, text boxes, etc), that serve as the Main record information. These are saved to a table via an INSERT statement on button_click because this form is unbound.
Next I have 8 categories that are relative per each main record (and data that goes along with it). Each of these categories could have a sub form area and a button click that bring it's relative form into the sub form area. These sub forms would be unbound as well as I would just save data via SQL statement. So i know I could accomplish this by running the insert statement from the parent form, on the main collection control's data that would create the KeyID number, then run a SQL statement that would turn around and load that KeyID number right back onto the page in a hidden text box.
Then when I click one of the sub forms and load its relative collection of controls, I could then save that data along with KeyID for each of these sub-forms/tables.
SO......
I was wondering if instead you could define these controls as a collection so that you could hide and make visible all the ones you need on button clicks and avoid the need for additional forms (subs). I know that if a user enters data into a text box, and then somewhere along the way that box becomes hidden, the data still exists in it and still ends up in the SQL statement....
So I want all these controls to exist on the same form, but I thought what is I could encapsulate them into a frame like an option group, then I could call the frame and all the relative controls would be called up (made visible) as needed.
Sorry for the long explanation but I thought it would help.
I do not think you can do it with an Option Group, but what you are describing is pretty much a subform, yesno?
Some examples of hiding the tab control from an app that went live in March 1998:
Tab driven by transparent command buttons over labels styled to look like colored command buttons:
Same approach, more buttons:
In this case, fake colored command buttons don't drive the tab, but insted show/hide the tab and a subform. In this case, the tab is actually driven by the listbox:
A view of when the tab is hidden and the subform revealed. The listbox drives navigation within the subform, which has a visible tab on it:
So, there's a lot that can be done without showing the tab control.