Call a windows form dynamically by its title in vb.net - vb.net

I'm stuck with something that I have tried to overcome before and couldn't.
Last time I worked around it, but I really need to nail this down.
I have a multi-instance form which lists details of jobs, so a user may have 6 or so instances of this form open, all with different info. Lets call that form A.
Within form A there is an option to assign that job to a user.
Clicking that link label opens a new form using showdialog where a combobox is populated with logged in users... Lets call that form B.
What I want to do is have the value selected on form B passed back to form A, however because form A has multiple instances, I'm not sure how to call it.
Form A is JobDetails.vb, and always has a form title of "Job XXXXX Details", so I was hoping there was a way I could use the title to call it.
Hope that all make sense! Thanks in advance, and apologies if its a dumb question. I feel like this should be something obvious, but I just can't achieve it.

As shown on the help page of ShowDialog, you can easily alter the main form when the dialog-form is closed. So you could do something like this:
Public Sub ShowMyDialogBox()
Dim testDialog As New Form2()
' Show testDialog as a modal dialog and determine if DialogResult = OK.
If testDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
Me.Text = String.Format("Jobs of {0}", testDialog.cmboUsers.SelectedValue)
Else
Me.Text = "Jobs"
End If
testDialog.Dispose()
End Sub

Related

Refreshing form from timer in another form

I have a problem with the snippet of the code I have got to extend and improve. It is not my original code and I cannot change the logic that much of it, just to be clear.
I have one main form called MDIServer, which has a timer set for every second. In Timer.Tick I have some other code which works fine (timer is running okay). Newly, I had to check there, if one form is Active and if so, change some stuff (labels text and tags) in that form and refresh it.
I add there this code:
If IsActiveForm("frmName") Then
frmName.ChangeSomething()
End If
The Sub ChangeSomething is, how you can see, located in the form I want to refresh and do the changes. In that function I simply change the label text and tags of few controls.
My question is: Form is not refreshing => labels are not visible changed, why?
I think I tried already almost anything with Refresh() function in the ChangeSomething() function or in the timer after I called this function. Also I tried to add there new timer (in frmName) and do the changes there, which works perfectly with
Label.Text = "something new"
Label.Refresh()
So I guess problem is somewhere with the refreshing form from Timer in different form. I also tried to do it with my own InvokeReguired() function etc...
P.S. When I am debugging the code, labels and tags are changing and every single function which has to be called, is called, but it is just not visible on the form itself.
EDIT Info
formName is not declared in MDIServer explicitely and in this case and many other cases, forms are used as default instances. Timer is from System.Windows.Forms.Timer. Also MDIServer is not a MDIParent of the formName and I cannot use Me.ActiveMdiChild Is. Lets just say, these two forms are not dependent on each other in any way.. and everything is done through name of the form (default instance, so nothing like Dim frm As Form and frm = frmName).
I would be really glad for any tip or anything :D
Thanks guys,
Vojta
So, I fixed my problem after some research and the problem was (expected) that I am not calling the subroutine ChangeSOmething() for one specific instance of the form frmName. So I had to change my code, that I will call it exactly for the instance which is active and visible.
New code looks like this:
Dim frmCollection = Windows.Forms.Application.OpenForms
Dim listfrmname = frmCollection.OfType(Of frmName).ToList()
If listfrmName.Count > 0 Then
Dim tmpFrm As frmName = listVZT15.Last()
tmpFrm.ChangeSomething()
End If
I also could not use combination of frmCollection.OfType(Of frmName).Any and frmCollection.Item("frmName"), because when I was closing the form and opening again, it created new and new instances (I dont know, why it is not closing the old one, but like I said, it is not my code). So the logic is, to list all open forms of the needed type, and then take the last instance from that list and for that instance call the subroutine. Also Me.Refresh() is placed in the subroutine ChangeSomething() itself.
Thanks everyone for help, it surely helped me to understand, how the instances works here.

vb.net Move between two form with option

at first i am sorry for my weak english but i will try my best to explain my issue
i am new in programming so i got idea but i don't know if can be happen or not
the idea is , i have 2 form separated each form have there cods , but i want to add button in form one its open the second form, to add thing to the second one , but i need to return to first form when i close the second form
can i do something like that ??
for clarification the second form i can open it from other ways and have own function and the first form too
UPDATING !!!
at first thank for all your answers and not useful vote ,second yes i am new but not that much i know how to use formname.show() and formname.close() etc..... ,the issue not about usual ways to open and close forms, for example in form one i input information about user one of the option is where he live, the country choosing from dropbox ,next to the dropbox there button named add new country the button open new form to add new country when i add new country and closed the form back to the first form , and just to be know there button in the third form (setting) to open the form add new country ,IN SHORT WORD I DON'T NEED TO OPEN THE FIRST FORM EVERY TIME I CLOSE THE SECOND ONE , ONLY RETURN TO THE FIRST FORM IF I CAME FROM THE BUTTON THAT I CLICKED IN FORM ONE
put this on action
formname.show() 'for opening a new form
formname.close() 'for opening closing the form
formname.showdialog() 'Where you cant click the main form``
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'show form 2
Form2.Show()
'move text from one form to another
Form2.TextBox1.Text = TextBox1.Text
End Sub

Send Parameter via new() or pre-set properties before calling the new form?

I would really appreciate your advice on the following:
I'm working on Windows forms using VB.NET (even though the language is irrelevant to the question at hand).
I've got a main form and wish to call out another one, however depending on a given variable I need the text on some of the new form's elements to change as well as disable some of its controls.
There are two ways I see of doing it:
Send a parameter from the main form and have some logic on the second form to deal with everything on load.
Main Form:
dim newform as new frmcalculate(byval type as string)
New Form:
public sub getexplanation(byval type as string)
select type
case "Sum"
lblexplanation.text = "this is a sum"
case "Subtraction"
lblexplanation.text = "this is a subtraction"
End sub
Set exactly what I want on the main form before calling the new form.
i.e:
dim newform as new frmcalculate()
newform.lblexplanation.text = "This is a sum"
I hope I've managed to explain it correctly.
I'm still new at this especially getting the formats right on Stackoverflow.
In the first approach the code is best managed and organized for further editing. So each form has it own code.
It is not best practice to use second approach. (Editing a form designer from another one)

Showing MsgBox() with nowait (no user input) is not the real issue

I had searched a lot how to display a Msgbox that will not wait the user input (pressing ok or cancel).
I found 3 solutiuons to this.
1- Display the MsgBox() in another thread or using BackgroundWorker()
2- Create a form that display the message, then closed the form by a timer and use it instead of Msgbox()
3- Using the API MessageBoxA()
Let say I have a loop from 1 to 100, and I want display a message for the i(counter)
When I test above 3 ways, I found that this is not the right way of doing it, I don't need a msgbox() to close by it self after showing the message, because that will display 100 dialog.
What I realy want is to display ONLY 1 MsgBox() and change the text accordingly.
I managed to do this using a a Form() as class and I did it using Application.DoEvents
I know it can be done using BackgroundWorker or Threading since alot of people advice against using Application.Doevents
Here is my code
Dim oWW As New WaitWindow With {.TopLevel = True,.TopMost = True,.StartPosition = FormStartPosition.CenterScreen}
oWW.Show(Me)
For i = 1 to 100
Threading.Thread.Sleep(500) ' Just to slowdown execution
oWW.SetMessage("Counter = " + i.ToString)
Next
oWW.Dispose()
Public Class WaitWindow
Sub SetMessage(ByVal Message As string)
lbl_message.Text = Message
Application.DoEvents
End Sub
End Class
WaitWindow is not more than a Form base class with a label (lbl_message)
That code works fine (display WaitWindowForm on center of currently displayed form only once, then I change the text)
I have 3 questions :
1- How to display the WaitWindowForm in the top right corner of my working form?
2- Is it possible to display the normal MsgBox() or MessageBox.Show() only once, then capture the text displayed and change it?
3- Which one is suitable for my issue (BackGroundWorker or Threading) and what the code in WaitWindow class I post will be if I decided to use Backgroundworker or Threading instead of Application.DoEvents (Changing the label text not showing new form with new text) ?
3 questions in one post.. humm.. who cares, I am not the one who will answer lol :)
Thanks in advance.
I think the issue that you're really encountering is that you're trying to use a message box for something it's not suited for. If you want to have text that constantly changes just add a text box in the upper right corner of your application and adjust it every time a new message needs to be shown.
You can also look up dialogu windows (search ".showdialog() vb.net" in google) might help as well.

How to refresh an access form

I am building an MS Access application in which all the forms are modal. However, after data change in a form, I want to refresh the parent form of this form with newer data. Is there any way to do it. To elaborate further :
Consider there are two forms, Form A and Form B. Both are modal form. From Form A, I initiate Form B, and now Form B has the user attention. But at the close of form B, I want to refresh the Form A. Is there a way to do it?
You can repaint and / or requery:
On the close event of form B:
Forms!FormA.Requery
Is this what you mean?
No, it is like I want to run Form_Load
of Form A,if it is possible
-- Varun Mahajan
The usual way to do this is to put the relevant code in a procedure that can be called by both forms. It is best put the code in a standard module, but you could have it on Form a:
Form B:
Sub RunFormALoad()
Forms!FormA.ToDoOnLoad
End Sub
Form A:
Public Sub Form_Load()
ToDoOnLoad
End Sub
Sub ToDoOnLoad()
txtText = "Hi"
End Sub
"Requery" is indeed what you what you want to run, but you could do that in Form A's "On Got Focus" event. If you have code in your Form_Load, perhaps you can move it to Form_Got_Focus.
I recommend that you use REQUERY the specific combo box whose data you have changed AND that you do it after the Cmd.Close statement. that way, if you were inputing data, that data is also requeried.
DoCmd.Close
Forms![Form_Name]![Combo_Box_Name].Requery
you might also want to point to the recently changed value
Dim id As Integer
id = Me.[Index_Field]
DoCmd.Close
Forms![Form_Name]![Combo_Box_Name].Requery
Forms![Form_Name]![Combo_Box_Name] = id
this example supposes that you opened a form to input data into a secondary table.
let us say you save School_Index and School_Name in a School table and refer to it in a Student table (which contains only the School_Index field). while you are editing a student, you need to associate him with a school that is not in your School table, etc etc
to refresh the form you need to type -
me.refresh
in the button event on click