I am learning vb.net and I'm having issues searching for what I need. I want to create a button that is "re-usable" throughout my application without needing to write code for each instance. So, what I would like to start with is take a variable in a form, example, public integer value and when this value changes I want to write to the text of a button. I know I can easily do this by writing code in the form btn_xxx.text = variable, but what if I have several buttons and each button looks at the same variable? Currently what I do is create a component which inherits a button and have a timer that on tick will look at the variable and write to the text. I'm sure there is a better way. Can anyone point me in the right direction? I know part of my problem is I don't know the nomenclature on what things are called, so hopefully I asked my question without too much confusion.
I saw this, https://www.daniweb.com/programming/software-development/threads/124842/detect-variable-change, but I don't see how to adapt that to my situation.
Here is what I have:
Private WithEvents Active_Alarm As New Nav_Active_Alarm
Then inside of a sub that calculates the count:
Active_Alarm.Count = CInt(dt_Active_Alarms.Rows.Count)
The user control:
Public Class Nav_Active_Alarm
Private mActive_Alarm_Count As Integer
Public Event Active_Alarm_Count_Changed(ByVal mvalue As Integer)
Public Property Count() As Integer
Get
Count = mActive_Alarm_Count
End Get
Set(ByVal value As Integer)
mActive_Alarm_Count = value
If Not Me.DesignMode Then
RaiseEvent Active_Alarm_Count_Changed(mActive_Alarm_Count)
test()
End If
End Set
End Property
Private Sub test()
If Not Me.DesignMode Then
If mActive_Alarm_Count = 0 Then
Me.btn_Goto_Active_Alarm.Image = My.Resources.Alarm_Clock_Static
'console or msgbox will work but updating the image will not
Else
Me.btn_Goto_Active_Alarm.Image = My.Resources.Alarm_Clock_Animation
'console or msgbox will work but updating the image will not
End If
End If
End Sub
End Class
If I write to console or add a msgbox I will see the event working. But, the image will not change. If I call the test sub from a timer it will work. Why won't the button update (by the way, I did try refresh and update in the code)?
Observer pattern is what you probably looking for.
This is quick and dirty.
Create a class to hold the variable value. Add a method that adds a button instance to a list.
Then a button that needs to know about the variable calls the register method.
When the value of the variable changes, it iterates through the list of buttons and sets the Text property of each one.
You might have jumped in a bit too deep too quick here. Google Custom data binding in .net, there's loads of built in stuff you can use. Though do it yourself is a good exercise.
A simple method to do this might be:
Create a form level list to hold the buttons you are interested in
Add the buttons you are interested in, into the list (maybe in form load or some other place where you have initialization code)
Create a private property in your form with a backing variable to hold the value you want to have applied to the buttons. In the setter portion spin through the list and set each buttons text.
Dim lstButtons As New List(Of Button)
Sub SetupButtons()
'call from form load or other init code
lstButtons.Add(btnPopulate)
lstButtons.Add(btnPopulate)
End Sub
Private _buttonText As String
Private Property ButtonText As String
Get
Return _buttonText
End Get
Set(value As String)
_buttonText = value
For Each b As Button In lstButtons
b.Text = value
Next
End Set
End Property
When you set the property - which now acts as your variable - it will update all of your textboxes for you.
I realize you mentioned without having to write code - but something has to tie things together. Even if you used the observer pattern (which is an elegant solution for this - so props to those who suggested it) you'd probably end up creating a class to hold the property and have that class implement the INotifyPropertyChanged from System.ComponentModel, and then you'd also have to have each button have a databinding for its text property to the property in the object of your class. There isn't really a way (that I can think of) to get around having to write some code for each form you do this in (though the class part you'd only have to write once of course).
Related
I have a problem and I don't understand how to solve it, if this is possible. I have a datagridview and I want to have a event then into one cell text I entry new line (keyboard enter), I now there are a event "grid_keydown" or "grid_keypress", but this two work only then I select the datagridview full row and not for each datagridview cell. There are a solution to config datagridview properties to work this 2 event also for each datagridview cell editing? Thank you a lot
So I finally found a solution to achieve what you are looking for. The bad news: It´s more tricky than I thought (and should be imho). Anyway you need the following steps:
Create a custom DataGridViewTextBoxEditingControl class.
It is to override the default behavior that the Enter key accepts the changes in the Cell and switches to the next row. It also attaches a NewLine to the current cell text.
Public Class MyDatagridTextBoxEditControl
Inherits DataGridViewTextBoxEditingControl
Public Overrides Function EditingControlWantsInputKey(keyData As Keys, dataGridViewWantsInputKey As Boolean) As Boolean
If dataGridViewWantsInputKey AndAlso keyData = Keys.Enter Then
AppendText(Environment.NewLine)
Return MyBase.EditingControlWantsInputKey(keyData, False)
End If
Return MyBase.EditingControlWantsInputKey(keyData, dataGridViewWantsInputKey)
End Function
End Class
Create a custom DataGridViewTextBoxCell class.
It is to set and use the previously created DataGridViewTextBoxEditingControl type.
Public Class MyDatagridTextBoxCell
Inherits DataGridViewTextBoxCell
Public Overrides ReadOnly Property EditType As Type
Get
Return GetType(MyDatagridTextBoxEditControl)
End Get
End Property
End Class
Bind the data together inr the DataGridView.
For my example I put the code into the Form´s constructor (after
InitializeComponent()), but you can also put it into the Load
event
'Assume [Beschreibung] is column no. 5
DataGridView1.Columns.Item(4).CellTemplate = New MyDatagridTextBoxCell()
(Depending on where you put this code you might need to call DataGridVie1.Refresh()).
Remarks: This code will lead to that you are not able to accept the changed value in the text cell with the Enter key anymore. It will however accept the input when you left mouseclick outside the cell. A Esc keypress will still revert the text changes.
You might add code into EditingControlWantsInputKey which treats another key like Enter, e.g. Insert or similar.
Without this code you can achieve the same behavior with the key combination Shift + Enter
I know, that there are many questions related to this, but still I cannot find a workable solution.
Usually, it would work like this: A form creates an instance of another form in it's container like this:
Dim PolInstIn As New SubForm1
Private Sub LoadDetail()
PolInstIn.TopLevel = False
PolInstIn.Name = "Sub From"
PolInstIn.FormBorderStyle = Windows.Forms.FormBorderStyle.None
PolInstIn.Dock = DockStyle.Fill
Me.GroupBox6.Controls.Add(PolInstIn)
PolInstIn.Show()
End Sub
Then it's easy to call a Public Sub from the sub form like this:
Call PolInstIn.MyPublicSubInSubForm1()
However, this doesn't work for me in this case. When I run MyPublicSubInSubForm1() it doesn't throw any error, but does no action. If I write a value to SubForm1 textbox and read it back, it reads, but I don't see it on the screen, so I suspect it is written to some other accidental instance.
I suspect it is because my parent form is also an instance of an form created in very similar way like SubForm1. Basically the ParentForm is a form loaded into tabPage and SubForm1 is a module loaded into ParentForm. It can exist in many copies (tabs).
Could you point to any simple solutions?
Regards,
Libor
I see this question got a lot of views, so here is an answer.
1) No visual response of child form (only results) - this could have happened if I created more then 1 instances of the form. The example is just an example, but if one use it (accidentally) this way, it may result in new definition of a child form every time (and consequent symptoms like the ones described). In practice, I split form loading from loading data into to the form (done by a public sub in that form).
2) If you want also a back reference (to i.e. parent grid form), define a Public ParentFormGrid as GridName (note ParentForm is a reserved name) and on loading a child form, set
PollInstIn.ParentFormGrid = Me
This way you can alway asccess the parent form, i.e. reload the grid when you save changes on a line edited in child form.
make Private Sub LoadDetail() to a public :
Public Sub LoadDetail()
It work on my project. Hopely its what you want
Dim PolInstIn As New SubForm1
Private Sub LoadDetail()
PolInstIn.Name = "Sub From"
PolInstIn.Show()
PolInstIn.TopLevel = False
PolInstIn.FormBorderStyle = Windows.Forms.FormBorderStyle.None
PolInstIn.Dock = DockStyle.Fill
PolInstIn.Update()
PolInstIn.Refresh()
Me.GroupBox6.Controls.Add(PolInstIn)
End Sub
So I am working with Coded UI to run some automated tests and one of the tests takes an extremely long time to complete.
I used breakpoints and found that after End Get in the bottom block of code, nothing happens for minutes and then finally the Assert.AreEqual completes and the test passes.
I tried changing a few playback settings but nothing changed. Does anyone know what is going on, and how I can fix it?
-The first block of code is a generated Assert method that gets called by the test
-The second block of code is called in the Assert parameters
'''<summary>
'''assert_clicked_columnhead_requestor - Use 'assert_clicked_columnhead_requestorExpectedValues' to pass parameters into this method.
'''</summary>
Public Sub assert_clicked_columnhead_requestor()
Dim uINameCell As HtmlCell = Me.UIAppWindow1.UIAppDocument.UIX978532666mkrdataTblTable1.UINameCell
'Verify that 'Name' cell's property 'InnerText' equals 'Name'
Assert.AreEqual(Me.assert_clicked_columnhead_requestorExpectedValues.UINameCellInnerText, uINameCell.InnerText)
End Sub
Public Overridable ReadOnly Property assert_clicked_columnhead_requestorExpectedValues() As assert_clicked_columnhead_requestorExpectedValues
Get
If (Me.massert_clicked_columnhead_requestorExpectedValues Is Nothing) Then
Me.massert_clicked_columnhead_requestorExpectedValues = New assert_clicked_columnhead_requestorExpectedValues()
End If
Return Me.massert_clicked_columnhead_requestorExpectedValues 'PROBLEM HERE
End Get
End Property
As requested
'''<summary>
'''click_columnhead_requestor
'''</summary>
Public Sub click_columnhead_requestor()
Dim uIRequestorCell As HtmlHeaderCell = Me.UIAppWindow1.UIAppDocument.UIRequestorCell
'Click 'Requestor' cell
Mouse.Click(uIRequestorCell, New Point(51, 23))
End Sub
Public ReadOnly Property UIAppWindow1() As UIAppWindow1
Get
If (Me.mUIAppWindow1 Is Nothing) Then
Me.mUIAppWindow1 = New UIAppWindow1()
End If
Return Me.mUIAppWindow1
End Get
End Property
Public ReadOnly Property UIX978532666mkrdataTblTable1() As UIX978532666mkrdataTblTable1
Get
If (Me.mUIX978532666mkrdataTblTable1 Is Nothing) Then
Me.mUIX978532666mkrdataTblTable1 = New UIX978532666mkrdataTblTable1(Me)
End If
Return Me.mUIX978532666mkrdataTblTable1
End Get
End Property
Coded UI does searches for precisely what is given in the search and filter properties, that operates quite fast. However, if that search fails then Coded UI does a smart match trying to find something similar and that can take a long time. It is necessary because titles etc can change a little from run to run. Changing the tests to avoid needing a smart match can dramatically improve the search speed. The general approach is to modify the search from equality to contains and to remove the part of the string that changes. A Microsoft blog explains in more detail, see http://blogs.msdn.com/b/dave_froslie/archive/2012/08/10/why-do-my-coded-ui-tests-pause-during-playback.aspx
¿? have you tried changing the value of ..
Playback.PlaybackSettings.SearchTimeout = miliseconds;
msdn playbacksettings.searchtimeout
good luck and tell us something
I am trying to implement another form into already complete routine. Basically all the code is there, all I need to do is manipulate the data in a different manner.
I have a routine that looks like this for instance.
This is a paraphrase example:
Private Sub getReportValues(ByRef fr As frmCustomReport, ByRef ReportInfo As ReportValues)
ReportInfo.eHeaderColor = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\FE Jupiter\MSSMonitor").GetValue("Report Equipment Header Color", "DCDCDC") 'Gainsboro
ReportInfo.mHeaderColor = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\FE Jupiter\MSSMonitor").GetValue("Report Monitor Header Color", "FFF8DC") 'Cornsilk
fr.btnEquipColor.PickedColor = System.Drawing.ColorTranslator.FromHtml("#" & Microsoft.VisualBasic.Conversion.Hex("&HFF" & ReportInfo.eHeaderColor))
The problem lays with the (fr as frmCustomReport) I want to make it a system.windows.forms.form but then I would lose the ability to use its objects. I should also mention that fr is a modal dialog and that I don't want a really hacky controlcollection work around for this. Does anyone have a good direction on this?
Note Also!!! The controls I want to access on both forms are almost identical. The only diffrence is layout and some added functionality.
Without a little more information,it is a little hard to give a concrete example. This will work depending on the amount of interaction you need to do. Create a subroutine that accepts the Base Class as an argument, take a look at the Name value and base your conditional logic off of that using CType to cast the Form to the proper type.
Private Sub clickOtherFormsButton(Value As Form)
If Value.Name = "Form3" Then
Dim formObject As Form3 = CType(Value, Form3)
formObject.Button1.PerformClick()
ElseIf Value.Name = "Form2" Then
Dim formObject As Form2 = CType(Value, Form2)
formObject.Button1.PerformClick()
End If
End Sub
make my own UserControl and I can aggregate new TabPages to a TabControl and then, inside of then TabPage, I add my own UserControl using the following code.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim TabX As New Windows.Forms.TabPage("Tab " & TabCount.ToString) '(ConfiguracionTabPage)
Dim MyControl As New ClientesEmpresa
MyControl.Name = "Control" & TabCount.ToString
If ClientesTabControl.TabPages.Count = 10 Then
ClientesTabControl.TabPages.RemoveAt(9)
End If
TabX.Controls.Add(MyControl)
TabX.Name = "Tab" & TabCount.ToString
TabX.Text = "Tab" & TabCount.ToString
MyControl.TitularLbl.Text = "Coca Cola"
Me.ClientesTabControl.TabPages.Insert(0, TabX)
Me.ClientesTabControl.SelectedIndex = 0
TabCount += 1
End Sub
My user control have several Labels, TextBox and TabPages(inside of a TabControl).
Now I want to change some properties dynamically from the source code, but I don't know how to access them.
The most similar theme that I found is this How to Acces of an User control in c#, but, as the title says, is in C#, how I can do it in VB.NET?
Sorry, I just notice that the Enter key post the comment. :(
Thanks for your feedback, I understand what are you saying but I missing something in the middle.
When I create the control in running time in the above code I can access easily to the properties of the created object, in this case my UserControl, but I don't understand how to reach the properties of a particular instance of that control from outside of Button_Click; ie. another button_click event(second button)
I was thinking to use something like
Dim ControlList As Windows.Forms.Control() = Me.ClientesTabControl.TabPages(0).Controls.Find("ModeloLbl", True)
or
ClientesTabControl.TabPages(0).Controls.OfType(Of AlarmasVehiculo)()
But I'm stuck here.
------------------------------------- 3th post ---------------
Thanks Steve, I was resolved using "Control.Find" and a For Each but your solution is easier.
There's any way to get the name of the selected tab or I must to create an Array when I create the New TabPage?, the idea is to update the text of the controls inside of the selected tab only when is selected by the user or every 5 seconds but just the in selected one.
Thanks.
To borrow M4N's answer from the C# question, and translate it to VB:
Cleanest way is to expose the desired properties as properties of your usercontrol, e.g:
Public Class MyUserControl
' expose the Text of the richtext control (read-only)
Public ReadOnly Property TextOfRichTextBox As String
Get
Return richTextBox.Text
End Get
End Property
' expose the Checked Property of a checkbox (read/write)
Public Property CheckBoxProperty As Boolean
Get
Return checkBox.Checked
End Get
Set (value As Boolean)
checkBox.Checked = value
End Set
End Property
'...
End Class
In this way you can control which properties you want to expose and whether they should be read/write or read-only. (of course you should use better names for the properties, depending on their meaning).
Another advantage of this approach is that it hides the internal implementation of your user control. Should you ever want to exchange your richtext control with a different one, you won't break the callers/users of your control.
To answer your second question, if you need to access your dynamically created controls, you can do so easily using their names, for instance:
Dim c As ClientesEmpresa= CType(Me.ClientesTabControl.TabPages("Tab1").Controls("Control1"), ClientesEmpresa)
c.CheckBoxProperty = True