Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
first i work on visual studio 2008.
i have a form(frmOrdreEnCours) that call an another form(frmListe) on a button.click event .
frmListe has a ListBox where you can choose a thing and validate it,
But i was ask to add a button frmOrdreEnCours that do the exact same thing, but it had to choose a specific element and don't show the frmListe.
so i tried to simulate a click but it doesn't works without the .show() Method
and obviously my client don't want a to see a opened window even for a second.
Private Sub BtnVracSup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnVracSup.Click
Dim ArlTraduction As ArrayList = New ArrayList
ArlTraduction.Add(30163) 'Non-conformite operationnel - materiel
ArlTraduction.Add(30168) 'Non-conformite opérationnel - autres
Classes.Langues.Traduire(ArlTraduction)
Dim FrmListe As New FrmListe(FrmListe.TypeListe.OperationnelMaterielBacSupp, ArlTraduction(0).ToString, ArlTraduction(1).ToString, NumOrdre)
FrmListe.MdiParent = FrmParent
Dim HideIt As Boolean = False
FrmListe.Show() // i don't want this
FrmListe.Visible = HideIt // i even try this
FrmListe.BtnValider.PerformClick() // there's no .Click available
End Sub
PerformClick doesn't do anything, if target control's CanSelect is false.
And if control is not visible, CanSelect return false.
http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/Button.cs,845bec419ca23629
You need call event handler (like BtnValider_Click) directry, but I think that looks bad code.
If you implemented like below:
Public Class FrmListe
Private Sub BtnValider_Click(sender As Object, e As EventArgs) Handles BtnValider.Click
' Validation codes '
End Sub
End Class
It's time to extract validation codes to another method.
Public Class FrmListe
Private Sub BtnValider_Click(sender As Object, e As EventArgs) Handles BtnValider.Click
IsValid()
End Sub
Public Function IsValid() As Boolean
' Validation codes '
Return True ' or Return False it is not valid '
End Function
End Class
and you can call this like following:
FrmListe.IsValid()
If you can, I recommend that you consider implement IsValid method in another class, like following:
Public Class ListeValidator
Public Function IsValid(type As FrmListe.TypeListe, traduction1 As String, traduction2 As String, numOrdre As Object)
' I dont know type of NumOrdre '
' validation code here '
End Function
End Class
I hope this helps you.
Related
I have a form called "partmanager". On it is a button to show another form "parteditor" to allow editing details of a part. Clicking that button will show the form and pass in a variable to the parteditors "new" routine.
My problem is that when the calling form (partmanager) starts, it immediately calls new routine in the parteditor form before it (partmanager) is even initialized so the parteditor form does not get the string that is supposed to be passed in. Later, when the calling form is visible and I click the button to show the parteditor form, new has already been prematurely called and so is not called again and the form does not get the string passed in.
I hope this makes sense!
I can implement a property in the parteditor form and pass in my variable that way prior to showing the form and that will work, thereby not even requiring a "new" routine in the parteditor forms code.
So my question is, is implementing the property the proper way to pass this variable to the form being called, or am I not properly coding my forms? (I also have an intermediary module called "commands" where I have been defining command procedures, in this case just showing a form.)
any pointers would be appreciated, thanks!
here is the code for the button in the calling form:
Private Sub EditButton_Click(sender As Object, e As EventArgs) Handles EditButton.Click
Commands.EditPart(_PartNumber) 'call the editpart command
Me.Close()
Me.Dispose()
End Sub
here is the code for the form being called:
Public Class PartEditForm
Private _partNumber As String = String.Empty
Public Sub New(partNumber As String)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
_partNumber = partNumber
End Sub
Private Sub PartEditForm_Load(sender As Object, e As EventArgs) Handles Me.Load
Label1.Text = _partNumber
End Sub
End Class
and here is the code in my "commands" module for loading/showing the form:
Public PartEditForm As New PartEditForm(_partNumber)
Public Sub EditPart(partnumber As String)
If PartEditForm.IsDisposed Then
PartEditForm = New PartEditForm(partnumber)
End If
PartEditForm.Show()
End Sub
you can save the routine in a dim variable and get this on the other form and close the first form or hide but you need get in new var and contine where stop before.
I have a Window-Form 'caller' in vb.net containing a datagridview with a small overview table of certain objects, each with its own ID in the first column. Now, if a row is double clicked, i want to show a dialog 'edit', where one can edit many details of that row which i do not want in the overview table.
My approach is as follows: In the caller form i wrote this to call 'edit':
Private Sub dgdata_dbclick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dg_data.CellMouseDoubleClick
Dim f_edit As New edit
f_edit.ShowDialog(Me)
End Sub
That works fine.
However, in the called Form "edit" i need to check, which ID was selected and load this data from the database to edit it. I can access some data from the calling form 'caller' using e.g.
MsgBox(CType(Me.Owner, caller).Text)
to show the window title of 'caller'. However, i want to extract the currently selected ID in the datagridview or at least some variabhle containing it. In the caller form, this could be easily done by evaluating
dg_data.Item(0, selectedRow).Value.ToString
but i cannot access any relevant information in 'caller'. I have a public class with some global variables there but i cannot access them as well.
Probably my strategy to solve this problem is not the most clever approach? Basically, i want to open a very detailed edit window when someone clicks on a line in an overviewtable but simultaniously blocking the rest of the application as long as the edit window is open.
Thanks!
The idea is to pass the data to the second form. When you create an instance of the second form (my class is called Form2, yours is called edit) with the New keyword the Sub New is called on Form2.
Private Sub OpenEditDialog()
Dim f_edit As New Form2(32) '32 is the number you retrieve from your DataGridView
f_edit.ShowDialog(Me)
f_edit.Dispose()
End Sub
You pass the ID to Form2 and set a variable at Form level. You can then use the variable anywhere in Form2.
Public Class Form2
Private ID As Long
Public Sub New(SelectedID As Long)
InitializeComponent()
ID = SelectedID
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MessageBox.Show(ID.ToString)
End Sub
End Class
You need to call InitializeComponent() so the controls will show up.
How do you usually get data into objects? You set a property or pass an argument to a method or constructor? Why should this be any different? Decide which you want to use and then write that code in your form. If it's required data, I would suggest a constructor. Just write this code in your form:
Public Sub New
and hit Enter. That will generate a little extra code automatically. You can then add a field to store the value, a parameter to the constructor and then assign the parameter to the field inside.
Thank you for pointing me to the correct route.
I solved it like this (which works fine and which is hopefully acceptable):
In the calling form:
Private Sub dgdata_dbclick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dg_data.CellMouseDoubleClick
Dim selectedRow As Integer = dg_data.CurrentCell.RowIndex
Dim f_edit As New edit
f_edit.edit(dg_data.Item(0, selectedRow).Value.ToString)
f_edit.ShowDialog(Me)
f_edit.Dispose()
End Sub
In the called form:
Public Sub edit(ByVal id As Long) 'Handles MyBase.Load
'Enter commands to prepare your form
End Sub
Private Sub Play_Click(sender As Object, e As EventArgs) Handles Play.Click
WordGeneration()
userInput()
Once the user has clicked a button which has been implemented into my form it will then run each sub specified . However the problem is , i do not want the sub WordGeneration() being run more than once . Is there a way for me to program into my code to stop WordGeneration() from being run more than once so it doesnt keep randomly generating a word , as i only want it to generate one word.
Create a Boolean variable which you can set to True once you have called the code:
Public Class Form1
Private _codeCalled As Boolean = False
Private Sub Play_Click(sender As Object, e As EventArgs) Handles Play.Click
If Not _codeCalled Then
WordGeneration()
End If
userInput()
End Sub
End Class
So in your WordGeneration method set the variable _codeCalled = True.
This should then only call the method once. Anytime you want to call the method again look at setting _codeCalled = False. You may also want to give it a different name which better fits your structure.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an application migrated from vb6 to vb.net.
I want to access the text of textbox1 in SearchFields of module.
I do not want to pass it as a parameter because I have so many controls to pass.
Please provide some example.
My code is frmNew page have Textbox1
Private Sub Ok_Click(.....)
call SearchFields()
Me.Close()
End Sub
Inside module I have method below
Sub SearchFields()
Dim str As string
Dim frm As New frmNew
str = frm.Textbox1.Text
End Sub
frmNew.Textbox1.Text is getting nothing
To do as little recoding as possible, you can change your two methods to the following. For this to work Textbox1 must be marked public. I have personally had to "migrate" VB6 apps to .Net, as #LarsTech pointed out, this requires rethinking of your app, not just syntax changes.
Private Sub Ok_Click(.....)
call SearchFields(Me)
Me.Close()
End Sub
Sub SearchFields(Form frm)
Dim str As string
str = frm.Textbox1.Text
End Sub
You can pass a reference to the control to use for the search text. This makes your SearchFields method more general. As an example, I created a form named frmNew and a module named Searching. On the form I placed a button named Ok, a TextBox and a ComboBox.
Public Class frmNew
Private Sub Ok_Click(sender As Object, e As EventArgs) Handles Ok.Click
Searching.SearchFields(TextBox1)
Searching.SearchFields(ComboBox1)
Me.Close()
End Sub
End Class
There are two ways you could go about handling the control passed to the module (which I named Searching). First, you can check the type of the control and take actions based on that:
Module Searching
Sub SearchFields(textSource As Control)
Dim str As String = ""
' just for invesigating, show the type of the control.
Console.WriteLine(TypeName(textSource))
If TypeOf textSource Is System.Windows.Forms.TextBox Then
str = textSource.Text
ElseIf TypeOf textSource Is System.Windows.Forms.ComboBox Then
Dim src = DirectCast(textSource, ComboBox)
If src.SelectedIndex >= 0 Then
str = src.SelectedItem.ToString()
Else
' nothing was selected. Do whatever is appropriate.
str = "NOTHING SELECTED!"
End If
End If
'TODO: the searching code.
Console.WriteLine(str)
End Sub
End Module
Alternatively, you can take advantage of method overloading, where it runs the version of the method which corresponds to the argument(s) you pass to it:
Module Searching
Sub SearchFields(src As TextBox)
DoSearch(src.Text)
End Sub
Sub SearchFields(src As ComboBox)
'TODO: check an item is selected.
Dim txt = src.SelectedItem.ToString()
DoSearch(txt)
End Sub
Private Sub DoSearch(s As String)
' do the search
Console.WriteLine(s)
End Sub
End Module
So, I need to do an program for a client and he wants a search bar in it. So I made it and everything worked perfectly but I put it in my main form. Now, I want to put it in a class but when I initialize the program, it gives me the following error
An error occurred while creating the form. For more information,
see Exception.InnerException. The error is: The form is self-reference during
construction from a default instance, which led to infinite recursion. In the
constructor of the form, refer to the form using 'Me'.
I tried to put Me.Rbtn_X... but it doesn't recognize it.
Initialization
' Main form
Public Sub New()
InitializeComponent()
Initialize_search()
End Sub
Initialize_search()
' Main form
' search is initialize like this :
' Dim search as New Research
Private Sub Initialize_search()
search.generate_autocomplete()
End Sub
generate_autocomplete()
' Research class
Sub generate_autocomplete()
' Main_form = Main form
Dim field = ""
' This is the place where the program fail
If Main_form.RbtnR_avancee_contact.Checked Then
field = "personneressource"
Else
field = "beneficiaire"
End if
' ....
End Sub
Is there something I didn't understand or It's not possible to do it that way?
Edit: added Form_shown event
Public Sub New()
InitializeComponent()
' Initialize_search()
End Sub
Private Sub Form_personne_Shown(sender As Object, e As EventArgs) Handles Me.Shown
MessageBox.Show("You are in the Form.Shown event.")
End Sub
The form is not created (fully) until New completes. By adding your Initialize_search to it, it eventually leads to the statement `Main_form.RbtnR_avancee_contact.Checked'. This is wrong on two counts:
1) the form doesnt exist yet, so you cant refer to it. (this is what the error meant with 'form is self-reference during construction')
2) the ref should be Me.RbtnR (which is what it meant by 'refer to the form using 'Me'')
Move your Initialize_search to the Form_shown event. Your code should look like this (including Lar's suggestion)
' Main form
Public Sub New()
' REQUIRED
InitializeComponent()
End Sub
If there is really something that needs to be setup for this, add it to the form_shown event:
Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.Shown
' NOTE: even .NET refers to ME not MainForm etc
InitializePanel
InitializeSeach
End Sub
Then:
Private Sub Initialize_search()
search.generate_autocomplete(Me.RbtnR_avancee_contact.Checked)
End Sub
Then:
Sub generate_autocomplete(AdvContact as Boolean)
Dim field AS STRING = ""
If AdvContact Then
field = "personneressource"
Else
field = "beneficiaire"
End if
' ....
End Sub
Your search class doesn't have a reference to the instance of the form's controls.
Try passing the value instead:
Sub generate_autocomplete(advancedChecked As Boolean)
Dim field As String = ""
If advancedChecked Then
field = "personneressource"
Else
field = "beneficiaire"
End if
End Sub
Then when you call it:
search.generate_autocomplete(Me.RbtnR_avancee_contact.Checked)
Even if did work like you want it to, according to your code, it would always result in field containing the same value (whichever was set in designer).
Instead, try putting this code inside RbtnR_avancee_contact.Checked event. Or even TextChanged for the autocomplete box (and initialize it for the first time user enters anything), it would examine the checked state and populate autocomplete items.
With this approach, if your user never uses the search box, you don't need to initialize it.