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

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

Related

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 to create click event for dynamically created custom control?

Let say that I'm creating thousands of custom controls, each control has own data assigned to it (like name, id, etc.), now I would like to add
click event for each of these custom controls
access from software any of these dynamically created controls
So I would like to show MsgBox if user click control #452, which will be returning values assigned to object in custom control class (passed during creation of new control on user form)
Second thing is that I would like to access at any time any of these objects and read values assigned to object in control....
How to do it ? Each control has name assigned like "Example1", "Example2", "Example3" but I don't know how to access it from code as any of these exists during writing...
Also I were trying to find how to create and rise events but it seems that all solutions are for statically created controls and I need to rise events for dynamically crated custom controls....
Thanks in advance for any tips/help how to solve it.
I'm using VB.NET
Ok I figured it out !
It is very simple actually.
If any one will be looking for an answer :
To access dynamically created control first we are going to name it :
Dim Control as new CustomControl
Control.Name = "Name" & Counter
Next after we've created new controls we are going to access it using:
Dim _Control as new CustomControl
_Control = Ctype(Me.Panel1.Controls("Name1"), CustomControl)
MsgBox(_Control.Name.ToString)
Viola
I still not sure about custom events for all controls on custom control (so where ever user click, there should be override event which returns something back, now I can only either click panel container (not labels/pictureboxes) from code inside user class, or event per control on custom control (but issue is that I want to pass data from custom control to user class).

Opening winforms in the same screen

I have an application with multiple win forms. I have noticed is that if the user has multiple screen displays and changes the application to one screen, the other forms that are called by other buttons, will open in the main display and not where my main application or form is.
How can I change this?
You can use the Form.CenterToParent Method on your Forms, they will then open up centered on your the creating Form, Or you can use the Screen Class to get the Bounds of the Display that your Main application is running on, then pass it to your created forms.
Edit:
On second thought assigning the Owner might just be enough, I don't have a dual monitor computer booted up at this time to test though
Dim frm As Form1 = New Form1()
frm.Owner = Me
frm.CenterToParent()
frm.Show()
Edit
Just had a chance to check it out. It was as I thought assigning the Owner to the new Form or using the Form.Show(IWin32Window) will open the new Form on the same screen as the originating Form.
frm.Show(Me)
Looks like the CenterToParent property is protected now.
According to the MSDN link
Do not call the CenterToParent method directly from your code. Instead, set the StartPosition property to CenterParent.
If the form or dialog is top-level, then CenterToParent centers the form with respect to the screen or desktop

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

TabControl.SelectedIndex being changed, but SelectedIndexChanged even not firing

All,
I have a TabControl in an application that started behaving strangely. Some background...
This program was converted from VB6 to VB .NET 2008, and used to refer to forms using their class names. In other words, I might have a form class called frmFoo. In the code for the program you might see:
frmFoo.Show()
or
frmFoo.UserDefinedProperty = True
During some recent changes, I created variables to represent instances of my forms much like these:
Public MyForm as frmFoo
MyForm = New frmFoo
MyForm.Show()
In doing so, I also removed code from the form's Load event handler and put it in the form's constructor.
When the form loads, or when a document is loaded and should influence the TabControl's selected index, something like the following will not necessarily fire the SelectedIndexChanged event.
MyForm.tbsForm.SelectedIndex = ValueReadFromFile
...or...
MyForm.tbsForm.Tabs(ValueReadFromFile).Select
Sorry to be so wordy, but there's more. If I open the form and look at the TabControl to verify that it's been set properly, everything works like it's supposed to. The misbehaving TabControl is contained within another TabControl, so I have to click the parent TabControl to see it. If I can see it, and run a test, the test always works. If I can't see it, and run a test, the first test I run will not fire the event. ...paging Dr. Heisenberg...
It's almost as if the control has to be initialized first by changing the value or making it visible onscreen...I'm totally lost on this one. It's the most unusual behavior I've ever seen. And everything worked perfectly before I began using variables to represent forms and placed the Load event code into the form constructors.
Can anyone help, or at least put me out of my misery?
SH
-------------------------------------------------------------- Edit #2
I just performed a test after having attempted to eliminate some of the variability in the behavior. But I wanted to confirm the previously-stated behavior.
I opened the program and read a file. This file contained a value that should have triggered the event handler. Without making the control visible, I can change the SelectedIndex property of the tab control without the event firing.
I closed the program down again, and reopened it. This time, selected the parent tab that allowed the child tab (the one whose event I'm concerned with) to become visible. I then selected a different tab in the parent control, meaning that the child control was no longer visible. When I opened the same file as before, it fired the event.
I'm tempted to implement a flag that confirms that the control has been repainted or whether the parent tab has been displayed. I may have to fire the event in code if the flag isn't set.
I want to reiterate that everything worked when the program referred to the forms by their class names and much of the arrangement of controls on the forms was done in the load event. Now the program creates variables and the arrangement of the controls is done in the form's constructor. I'm sure this has something to do with the problem I'm having, but I can't understand how. Any wisdom to share?
MyForm.tbsForm.SelectedIndexChanged = ValueReadFromFile
doesn't make a lot of sense. Is tha trying to assign a handler to the SelectedIndexChanged event? or is ValueReadFromFile the name of the tab?
What you're saying is that you have two tab controls, say, A and B. Tab control B is contained within a tab of A, and unless A has the tab page selected that contains the tab control B, the SelectedIndexChanged event of B will not fire if you change its tab programatically?
In which different ways have you tried to select a tab within the child tab control, and when is this code being executed?