Check with adding / removing objects in VB6 - button

I have a query, I have this interface:
The ComboBox are inside a UserControl, pressing the Add this UserControl button with the ComboBox adds the UserControl / ComboBox in a PictureBox:
What I want is that, for example, when the user selects values ​​of the two ComboBox and press the button add these selected values ​​go to the PictureBox (below) and the values ​​that are selected in the UserControl are cleaned.I leave a picture of what I say, suppose the user selected the values ​​of the combos:
Once you have selected them, the add these button is enabled, they pass below and those above are cleaned:
But, if I press the remove button you must remove the last loaded object and move to the top. I leave a descriptive image, press the remove button:
The bottom disappears and goes up:
Currently this is the code I use to add:
Option Explicit
Dim indice As Integer
Public Property Let AddType(ByVal Value As String)
cmbAddExample.Text = Value
End Property
Private Sub btnAñadir_Click()
indice = indice + 1
Picture1.Visible = True
Load uc1(indice)
Set uc1(indice).Container = Picture1
uc1(indice).Visible = True
uc1(indice).Top = IIf(indice = 1, 0, uc1(indice - 1).Top + uc1(indice - 1).Height + 20)
Load cmbAddExample(indice)
Set cmbAddExample(indice).Container = uc1(indice)
cmbAddExample(indice).Visible = True
cmbAddExample(indice).Top = cmbAddExample(indice - 1).Top
CargaIDTipoNumero
uc1(indice).AddType = uc1(0).AddType
uc1(indice).AddType = ""
If indice = 3 Then
Me.btnAñadir.Enabled = False
End If
End Sub
So, does anyone have any idea how I can do what I am looking for? Or yes, is it possible?
Button Remove:
Private Sub btnQuitar_Click()
indice = cmbAddExample().Count - 1
If indice > 0 Then
Unload cmbAddExample(indice)
End If
End Sub

Using this answer as the starting point, you can implement these requirements fairly easily. I have copied relevant code from the other answer and modified it:
Private Sub btnAdd_Click()
index = index + 1
Load uc1(index)
Set uc1(index).Container = Picture1
uc1(index).Visible = True
uc1(index).Top = IIf(index = 1, 0, uc1(index - 1).Top + uc1(index - 1).Height + 20)
'copy the information down
uc1(index).AddType = uc1(0).AddType
uc1(0).AddType = ""
Picture1.Visible = True
End Sub
Private Sub btnRemove_Click()
If index = 0 Then Exit Sub
'copy the information back up and remove the control
uc1(0).AddType = uc1(index).AddType
Unload uc1(index)
index = index - 1
If index = 0 Then Picture1.Visible = False
End Sub
I have only coded for one control to illustrate the concept. You can add other controls as needed.

Related

Copy objects with a button vb6

Project structure and UserControl:
Formularios = Form = Form1
Controles de Usuario = User Controls = uc1
All UserControl Code:
Option Explicit
Public Property Get AddType() As String
AddType = cmbAddExample.Text
End Property
Public Property Let AddType(ByVal Value As String)
cmbAddExample.Text = Value
End Property
Public Property Get AddNumber() As String
AddNumber = Text1.Text
End Property
Public Property Let AddNumber(ByVal Value As String)
Text1.Text = Value
End Property
Button Add(Añadir) Code:
Option Explicit
Dim indice As Integer
Private Sub btnAñadir_Click()
indice = indice + 1
Load uc1(indice)
Set uc1(indice).Container = Picture1
uc1(indice).Visible = True
uc1(indice).Top = IIf(indice = 1, 0, uc1(indice - 1).Top + uc1(indice - 1).Height + 20)
Load cmbAddExample(indice)
Set cmbAddExample(indice).Container = uc1(indice)
cmbAddExample(indice).Visible = True
cmbAddExample(indice).Top = cmbAddExample(indice - 1).Top
CargaIDTipoNumero
Load Text1(indice)
Set Text1(indice).Container = uc1(indice)
Text1(indice).Visible = True
Text1(indice).Top = Text1(indice - 1).Top
uc1(indice).AddType = uc1(0).AddType
uc1(indice).AddType = ""
Picture1.Visible = True
If indice = 3 Then
Me.btnAñadir.Enabled = False
End If
End Sub
Black Circle is a UserControl with a TextBox and a ComboBox inside. Red Circle is a PictureBox.
So, the problem is that i need copy values from uc1 in PictureBox when i press the button add. But when i press the button, it shows me the next error:
Compilation Error: Method or data member not found.
In this line:
uc1(index).AddType = uc1(0).AddType
So, any sugerence?
In your post, you say the object circled in black is a UserControl when in fact it is a PictureBox named uc1(0). Since a PictureBox does not have an AddType property, you receive the error.
Replacing this PictureBox with a UserControl will resolve the error.

Checkbox for the VB6 DataGrid

How to add Checkbox for Vb6 data grid like as image.
For an MSFlexGrid control, I use a checked and unchecked image in the column.
You add two PictureBoxcontrols, one for each image, and make the Visible property false. When loading the data for your grid you can set each pic according to whether it needs to be checked or not:
With MSFlexGrid1
.Col = 1
If myCol1Bool Then
Set .CellPicture = picChecked.Picture
Else
Set .CellPicture = picUnChecked.Picture
End If
.Col = 2
If myCol2Bool Then
Set .CellPicture = picChecked.Picture
Else
Set .CellPicture = picUnChecked.Picture
End If
End With
You can toggle the check state on click if it's editable:
Private Sub MSFlexGrid1_Click()
If (MSFlexGrid1.Col <> 1 And MSFlexGrid1.Col <> 2) Or MSFlexGrid1.Row < 1 Then Exit Sub
If MSFlexGrid1.CellPicture = picChecked Then
Set MSFlexGrid1.CellPicture = picUnchecked
Else
Set MSFlexGrid1.CellPicture = picChecked
End If
End Sub
For a full example, check out vb-helper.com.

ASP.Net - Reducing repetitive code for validation - VB

I have a form with many drop down list boxes on. Each of which I am showing or hiding a row of a table based on its value then adding a requiredfieldvalidator to the text box contained in that row. I am doing this on the selectedindexchanged event of each drop down list, the code for which can be seen below:
Protected Sub cbOffCover_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbOffCover.SelectedIndexChanged
If cbOffCover.SelectedValue = "N" Then
OffCoverRow.Visible = True
Dim rfOffcover As RequiredFieldValidator = New RequiredFieldValidator
With rfOffcover
.ControlToValidate = txtOffCover.ID
.SetFocusOnError = True
.ErrorMessage = "*"
.ForeColor = System.Drawing.Color.Red
End With
OffCoverCell.Controls.Add(rfOffcover)
Else
OffCoverRow.Visible = False
Dim c As Control
For Each c In OffCoverCell.Controls
If c.ID = "rfOffCover" Then
OffCoverCell.Controls.Remove(c)
End If
Next c
End If
End Sub
I then reuse this code for each drop down list to show/hide a differently named row and apply validation to a different text box.
My question being is there a better way of doing this? I don't know exactly how but I can't help but think I don't have to write this much code (or copy/paste) over and over again for each drop down list. Is it possible to write a function/class that will do the work and I can just call that instead? Might seem basic but i'm new to asp/vb. Many thanks
You can put it in a function that returns a boolean. When you call the function, pass it the combobox itself and whatever values you want to validate against. If it matches, return true. Try something like this:
Public Function ValidateComboBox(someComboBox as ComboBox, expectedValue as String)
Dim result as Boolean = False
If someComboBox.SelectedValue = expectedValue Then
result = True
OffCoverRow.Visible = True
Dim rfOffcover As RequiredFieldValidator = New RequiredFieldValidator
With rfOffcover
.ControlToValidate = txtOffCover.ID
.SetFocusOnError = True
.ErrorMessage = "*"
.ForeColor = System.Drawing.Color.Red
End With
OffCoverCell.Controls.Add(rfOffcover)
Else
OffCoverRow.Visible = False
Dim c As Control
For Each c In OffCoverCell.Controls
If c.ID = "rfOffCover" Then
OffCoverCell.Controls.Remove(c)
End If
Next c
End If
Return result
End Function
Of course, modify it to fit your needs. Maybe you only return the value, and do the other stuff inside the control's SelectedIndexChanged method.

How to check if button was click in page load

I have a button I don't want causing this if command in my page load.
If Page.IsPostBack Then
ViewState(2) = TreeView1.SelectedNode.ValuePath
ViewState(5) = TextBox1.Text
''Where I would like to put another if command or whatever else is possible to check if the buttong was clicked''
If ViewState(2) = ViewState(1) And ViewState(5) = ViewState(4) Then
nodecount = ViewState(3)
nodecount = nodecount + 1
ViewState(3) = nodecount
If nodecount > 0 Then
MsgBox("Please select another option or different number of data points")
End If
Else
nodecount = 0
ViewState(3) = nodecount
End If
End If
I tried setting a public property to change if the button was clicked but page.ispostback gets called first then the sub does.
I'm not sure if I fully understand your question. If you are trying to run a block of code, why not do it at the btnSomeButton.Click event, why do it at the Page_Load event.

Attaching Changed Event to Dynamically Created Radiobuttons

I'm dynamically creating a load of radiobuttons in code behind (vb.net on load) and am wondering how I should create the events on them. I have a gridview with two columns and each row has a radiobutton in both columns that are in a group together (fyi group means check one, the other is unchecked). I want to change the background colour to yellow on whichever one is checked. How best would I do this? Javascript (catch all radio button change events), JQuery (catch all radio button change events) or Vb.net (attach method on creation)? Here is how I'm creating them;
Dim r As RadioButton
Dim l As Label
Dim radioBtnNumber As Integer = 0
For row As Integer = 0 To GridView1.Rows.Count - 1
If GridView1.Rows(row).RowType = DataControlRowType.DataRow Then
Dim x As Integer = 0
For Each tc As TableCell In GridView1.Rows(row).Cells
l = New Label()
l.Text = tc.Text
If (x = 1) Or (x = 2) Then
r = New RadioButton()
r.GroupName = String.Format("RdBtnGroup{0}", row)
r.ID = radioBtnNumber.ToString
//Could add in event here. Tried this but didn't work
//r.Attributes.Add("OnCheckedChanged", "radiobuttonChecked()")
tc.Controls.Add(r)
radioBtnNumber = radioBtnNumber + 1
End If
tc.Controls.Add(l)
x = x + 1
Next
End If
Next
That code is actually more confusing that it should be because the gridview is actually flipped (veritical as opposed horizontal). So I have radiobuttons named 0, 1, 2, 3, 4 etc with 0 and 1 in a group, 2 and 3 in a group etc and I just want to catch which one has been changed. Bear in mind though that I need the name of the RB or it's position in my gridView as I'll have to change the background colour of the RB it's paired with also!
Add line (code is C#, translate to VB):
r.CheckedChanged += new System.EventHandler(this.radiobuttonChecked);
when you create the RadioButton. radiobuttonChecked should be method of your webpage:
protected void radiobuttonChecked(Object sender, EventArgs e)
{
...
}
where you will be able to cast sender to RadioButton and analyze its properties. This will cause a postback to the server, where you can modify the radio buttons.
If you want to do it on the client, then you can first assign CssClass (e.g. "rbClass") to the same string for all of them and also use
r.Attributes.Add("pairName", "<name for the pair of radiobuttons>")
Then add javascript to your page:
$(document).ready(function(){
$(".rbClass").change(function(){
// here you look at $(this).attr and
// adjust its and its pair's css accordingly
})
});

Resources