Treat the two set separately - vb.net

The woes of a programming newbie... In my program.
I am calculating and displaying how many calories a person needs to maintain current weight. I have 2 sets of 2 radioButtons: 1 set is for the gender, male or female, the other set is for their activity rate, active or inactive.
With two different sets of radioButtons, one from each should be able to be selected, however only one radio button is being selected at a time, how can I tell the program to treat the two sets of radio buttons separately?

You should use GroupBoxes to put a set of radiobuttons inside a groupbox and the other set inside another Group box
In this way every set of radiobuttons is isolated from the other set and works as expected
The following code is just an example of a form build manually, working with the designer will create an equivalent of this code for you inside the InitializeComponent method. Note how the two radiobutton sets are childs of a different container (the groupbox)
' A generic form
Dim f as Form = new Form()
f.Size = new Size(300, 500)
' Create a Group box
Dim b1 as GroupBox = new GroupBox()
b1.Text = "Gender"
b1.Location = new Point(0,0)
' Create two radiobutton for Gender
Dim r1 as RadioButton = new RadioButton()
r1.Text = "Male"
r1.Location = new Point(5,15)
Dim r2 As RadioButton = new RadioButton()
r2.Text = "Female"
r2.Location = new Point(5, 40)
' Add the two radiobuttons to the GroupBox control collection
b1.Controls.AddRange(new Control() {r1, r2})
' Repeat for the second set of radiobuttons
Dim b2 as GroupBox = new GroupBox()
b2.Text = "Activity Rate"
b2.Location = new Point(0,100)
Dim r3 As RadioButton = new RadioButton()
r3.Text = "Active"
r3.Location = new Point(5,15)
Dim r4 as RadioButton = new RadioButton()
r4.Text = "Inactive"
r4.Location = new Point(5,40)
b2.Controls.AddRange(new Control() {r3, r4})
' Finally add the GroupBoxes to the Form ControlsCollection
f.Controls.AddRange(new Control() {b1, b2})
f.ShowDialog()

Radio buttons work in groups. Separate them with a container and your good.

Related

How to create multiple labels

I'm new to "coding/programming". I'm trying to make a functional program - I call it "a point to pay". It's like those supermarket programs where they register the thing you are going to buy. So i need to create some labels to register products.
The code I have:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ''When i click the button
If CantidadVer1.Text = 0 Then ''this verifies how many Labels i have created
CantidadVer1.Text = +1 ''this updates the verification
Dim lbl1 As New Label ''this creates the labels
lbl1.Size = New System.Drawing.Size(159, 23)
lbl1.Text = (Product.Text) ''product.text is a TextBox
lbl1.Location = New System.Drawing.Point(12, 80 + 20) '' i add 20 more everytime i create a label
Me.Controls.Add(lbl1)
ElseIf CantidadVer1.Text = 2 Then ''at this point it creates the label but "crashes" (It dosent work anymore)
CantidadVer1.Text = +1
Dim lbl2 As New Label
lbl2.Size = New System.Drawing.Size(159, 23)
lbl2.Text = (Product.Text)
lbl2.Location = New System.Drawing.Point(12, 80 + 40)
Me.Controls.Add(lbl2)
ElseIf CantidadVer1.Text = 2 Then
CantidadVer1.Text = +1
Dim lbl3 As New Label
lbl3.Size = New System.Drawing.Size(159, 23)
lbl3.Text = (Product.Text)
lbl3.Location = New System.Drawing.Point(12, 80 + 60)
Me.Controls.Add(lbl3)
ElseIf CantidadVer1.Text = 3 Then
CantidadVer1.Text = +1
Dim lbl4 As New Label
lbl4.Size = New System.Drawing.Size(159, 23)
lbl4.Text = (Product.Text)
lbl4.Location = New System.Drawing.Point(12, 80 + 80)
Me.Controls.Add(lbl4)
ElseIf CantidadVer1.Text = 4 Then
CantidadVer1.Text = +1
Dim lbl4 As New Label
lbl4.Size = New System.Drawing.Size(159, 23)
lbl4.Text = (Product.Text)
lbl4.Location = New System.Drawing.Point(12, 80 + 100)
Me.Controls.Add(lbl4)
End If
End Sub
So I execute it and then it creates 2 labels and then crashes.
It is supposed to create 5 labels .
Is there an easier way to create multiple labels without making the program crash?
In your code I only see one label created every time. I don't think the code crashes. Remember in an IF block, if the condition is met in the first part, it skips all subsequent ElseIf conditions.
With a little research, you'll see that the best option would be to use a DataGridView instead of a bunch of labels. You could simply add a new row for each item. Lets say you have a datagridview named DGV_Product with 3 textbox columns for product, quantity and price:
Dim price as Double = 1.99
Dim product as String = "Apple"
Dim qty as integer = 3
DGV_Product.Rows.Add(New String() {product, Cstr(qty), CStr(price)})
That adds a row containing "Apple, 3, 1.99"
But if you insist on using labels, This would work better:
1) Set The following variable at a class level.
Dim lbl_pos as integer
2) In your form Load event handler set the value to 20 less than where you want your first label to appear:
lbl_pos = 80 ' gathered from your code
3) Then in your click event handler of the button you increment the new label's position by 20 before adding the new label.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
lbl_pos = lbl_pos + 20 'increment the position
Dim lbl as New Label
With lbl
.Text = "Your Text"
.Location = New Point(10,lbl_pos) '(left position, top position)
' And so Forth
End With
me.controls.add(lbl)
End Sub
With this approach you can add as many labels as you like without a bunch of If-ElseIf blocks.
I hope you rethink your approach and go with the DataGridView instead as it's much more practical and you can add as many rows as you like without hassle.
You have an error in your code .. The line
ElseIf CantidadVer1.Text = 2 Then ''at this point it creates the label but "crashes" (It dosent work anymore
should be
ElseIf CantidadVer1.Text = 1 Then ''at this point it creates the label but "crashes" (It dosent work anymore)
If you dont set the value for CantidadVer1 in the begining. Its value = "" not 0. so it will be the bug if you dont put 0 to the CantidadVer1.text
the first if maybe like this
If CantidadVer1.Text = "" OR CantidadVer1.Text = 0 Then
Your PTP is poorly designed. All the answers here are trying to solve your problem by looking based on what you've done.
But your model is kinda based on your what's in your view. For example, have you planned how you will retrieve all the added products ? The history is inside your view : good luck with that.
That's why I suggest to review your design. For example :
Use a List in your code behind to save your data. Your button_click should only do that (with some validations).
Use a repeater in your view. Bind that repeater to your List : and voila. Your repeater will take care of creating all the label corresponding to all the added products.

Dynamic entries from textfiles into FlowLayoutPanel

I'm currently working on a ToDo program, where you can simply add notes. (With the option to set priority and attachment) The Note will be saved as a textfile. Now I want to make an over view of the ToDo's which have to be done. In the end it's supposed to look like a list, one row for each Note, built like this:
TITLE.............................[Attachment Icon]..[Priority Icon]..[CheckBox]
The checkbox should always be on the very right side of the row.
My idea is to add a FlowLayoutPanel to a FlowLayoutPanel for every Note. In the child-FlowLayoutPanel I'll add the controls. My problem is that I have no Idea how to set the positions of the controls. I tried to add a label and use it as a space between the title and the other controls, but it doesn't really work.
Here's the code I currently have:
For Each File In Directory.GetFiles(myPath)
Dim tlp As New FlowLayoutPanel
tlp.BackColor = Color.Gray
tlp.Width = 260
tlp.FlowDirection = FlowDirection.TopDown
Dim lbl As New Label
lbl.AutoSize = True
lbl.Text = getInsert(File, 0)
lbl.Anchor = AnchorStyles.Right
lbl.ForeColor = Color.White
AddHandler lbl.Click, Sub() Me.getInsert(File, 0)
tlp.Height = 40
Dim cbx As New CheckBox
cbx.FlatStyle = FlatStyle.Flat
cbx.Text = ""
cbx.Width = 15
cbx.Height = 30
AddHandler cbx.CheckStateChanged, Sub() Me.deleteEntry(tlp)
Dim space As New Label
space.AutoSize = False
space.Text = ""
space.Height = 30
space.Anchor = AnchorStyles.Right
tlp.Controls.Add(lbl)
tlp.Controls.Add(Space)
tlp.Controls.Add(cbx)
Space.Width = tlp.Width - lbl.Width - cbx.Width - 15
mfp.Controls.Add(tlp)
Next
Here's an example:
todo_example
I hope anyone has a good idea how to solve this problem.
Thanks in advance :)

How to add a UserControl to a DataGridView in VB.net, and have the control always showing?

I made a usercontrol that is basically a panel with a bunch of labels, buttons, and Event Handlers on it. I want to be able to populate a DataGridView with these controls.
I have followed the instructions in this MSDN article, and have created a DataGridViewColumn and a DataGridViewCell class to associate with my Control.
The issue I am having is that the control is not rendering. When I run the code at the link above, It shows the date in the cell, and the user control only shows when the cell is being edited. What I'd like to have happen is that the control is always visible in the cell, similar to the behavior in a DataGridViewImageColumn.
How can I force the DataGridView to always show the control?
The main idea:
Create a user control
In form load event, for each record, create an instance of user control, set values to its properties, add event handlers, set its visiblity to false and then add it to your grid Controls collection, and set the tag of your wanted cell or that row to this control.
Handle CellPainting event of your grid and for each row of grid, retrieve tag of your wanted cell and convert it to your control and position it in cell bounds and make it visible. To get the position that control should be shown use GetCellDisplayRectangle method of DataGridView
Screenshot:
My sample control with name of SomeControl, contains a label and a text box and a property SomeProperty and an event SomePropertyChanges. I show cell value in TextBox of my custom control.
You can do anything with your custom control containing any control and any type of properties and events.
Code:
Here is vb-like pseudo code.
'This is not VB, this is vb-like pseudo code
Private Sub Form_Load(sender as object , e as EventArgs)
'Get data
'Show data in grid
'For each row
For Each row as DataGridViewRow in this.categoryDataGridView.Rows
'Create an instance of control
Dim myControl as New SomeControl()
'Set properties and register event handlers
myControl.SomeProperty = row.Cells[2].Value
myControl.SomePropertyChanged += myControl_SomePropertyChanged
'Make it invisible
myControl.Visible = False
'Set tag of your wanted cell to control
row.Cells[2].Tag = myControl
'Add control to Controls collection of grid
this.categoryDataGridView.Controls.Add(myControl)
Next
End Sub
Private Sub myControl_SomePropertyChanged(sender as object, e as EventArgs)
'event handler of SomePropertyChanged for custom control
'do stuff here
End Sub
Private Sub dataGridView_CellPainting(sender as object, e as DataGridViewCellPaintingEventArgs )
'for each row
For i as Integer=0 To dataGridView.RowCount- 1
'Extract control from tag of your wanted cell
var myControl= this.dataGridView.Rows[i].Cells[2].Tag as SomeControl
'Get cell rectangle
Dim cellRectangle As Rectangle= dataGridView.GetCellDisplayRectangle(2, i, True)
'Set location
myControl.Location = New Point(cellRectangle.X, cellRectangle.Y )
'Set size
myControl.Size = New Size(cellRectangle.Width - 1, cellRectangle.Height - 1)
'Make visible
myControl.Visible = True
Next
}
read this article
How can you add a custom user control to a datagridview?
http://bytes.com/topic/visual-basic-net/answers/444528-how-can-you-add-custom-user-control-datagridview
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Email Id");
for (int j = 0; j < 10; j++)
{
dt.Rows.Add("", "ravindra.m089#gmail.com");
}
this.dataGridView1.DataSource = dt;
this.dataGridView1.Columns[0].Width = 200;
/*
* First method : Convert to an existed cell type such ComboBox cell,etc
*/
DataGridViewComboBoxCell ComboBoxCell = new DataGridViewComboBoxCell();
ComboBoxCell.Items.AddRange(new string[] { "aaa", "bbb", "ccc" });
this.dataGridView1[0, 0] = ComboBoxCell;
this.dataGridView1[0, 0].Value = "bbb";
DataGridViewTextBoxCell TextBoxCell = new DataGridViewTextBoxCell();
this.dataGridView1[0, 1] = TextBoxCell;
this.dataGridView1[0, 1].Value = "some text";
DataGridViewCheckBoxCell CheckBoxCell = new DataGridViewCheckBoxCell();
CheckBoxCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
this.dataGridView1[0, 2] = CheckBoxCell;
this.dataGridView1[0, 2].Value = true;
DataGridViewTextBoxCell txt = new DataGridViewTextBoxCell();
txt.ToolTipText = "Enter Ur Name";
this.dataGridView1[0, 3] = txt;
this.dataGridView1[0, 3].Value = "lakshman";
/*
* Second method : Add control to the host in the cell
*/
DateTimePicker dtp = new DateTimePicker();
dtp.Value = DateTime.Now.AddDays(-10);
//add DateTimePicker into the control collection of the DataGridView
this.dataGridView1.Controls.Add(dtp);
//set its location and size to fit the cell
dtp.Location = this.dataGridView1.GetCellDisplayRectangle(0, 3, true).Location;
dtp.Size = this.dataGridView1.GetCellDisplayRectangle(0, 3, true).Size;
TextBox Text = new TextBox();
Text.Text = "This my Name";
Text.BackColor = Color.RosyBrown;
this.dataGridView1.Controls.Add(Text);
Text.Location = this.dataGridView1.GetCellDisplayRectangle(0, 4, true).Location;
Text.Size = this.dataGridView1.GetCellDisplayRectangle(0, 4, true).Size;
}

Clear the DataGridView without loosing the source

I have a form with DataGridView, dataSourced by dataTable. When i insert the record at the end i press the Clear button or form all fields (textboxes, comboboxes, DGV checkboxes) get cleared automatically when i press the save button.
I normally clear the textboxes and comboboxes by writing:
textbox1.text = ""
combobox1.text = ""
but when i do clear DGV with this like:
dgvPurchase.dataSource = nothing
dgvPurchase.Refresh()
or
dgvPurchase.rows.clear()
It does clear the DGV but when i add new entry so DataGridView and data both do not appear into DGV. On the Form Load event I have the following code:
dtField = retrieveFull("ProductBasicInfo")
cmbPurProdID.DisplayMember = "ProdName"
cmbPurProdID.ValueMember = "ProdID"
cmbPurProdID.DataSource = dtField
dtPurchase.Columns.Add("ProdID")
dtPurchase.Columns.Add("ProdName")
dtPurchase.Columns.Add("RetailerID")
dtPurchase.Columns.Add("RetailerName")
dtPurchase.Columns.Add("UnitPrice")
dtPurchase.Columns.Add("Qty")
dtPurchase.Columns.Add("Amount")
dgvPurchase.DataSource = dtPurchase
Please guide me that is there any way that my DataSource do not disturb or if I clear it so it also refresh the DataSource.
This is the add button code, which adds the data into DataGridView
Try
For a As Integer = 0 To dtPurchase.Rows.Count - 1
If dtPurchase.Rows(a).Item("ProdID") = cmbPurProdID.SelectedValue Then
MessageBox.Show("This product is already enlisted")
Exit Sub
End If
Next
dRow = dtPurchase.NewRow
dRow.Item("ProdID") = cmbPurProdID.SelectedValue
dRow.Item("ProdName") = cmbPurProdID.Text
dRow.Item("RetailerID") = cmbPurRetailerID.SelectedValue
dRow.Item("RetailerName") = cmbPurRetailerID.Text
dRow.Item("UnitPrice") = txtPurUnitPrice.Text.Trim
dRow.Item("Qty") = txtPurQty.Text.Trim
dRow.Item("Amount") = txtPurAmount.Text.Trim
dtPurchase.Rows.Add(dRow)
You can create new empty DataTable and set it like this:
c#
DataTable emptyDT = new DataTable();
dgvPurchase.DataSource = emptyDT
vb.Net
Dim emptyDT As New DataTable()
dgvPurchase.DataSource = emptyDT
Does this work for you?

Controls are not added to tabpage VB.NET

I run the following code in the constructor of a window. The "label" gets added but none of the other controls are shown on screen. If I debug the newTab.Controls there are several controls in it. Why don't they show up on the screen and I only see the "label" control.
Thanks
Dim graphlist As ArrayList = New ArrayList
For Each funct As TL_FUNCTION In functionlist
If (funct.functionname = functi) Then
If Not (graphlist.Contains(funct.picture)) Then
graphlist.Add(funct.picture)
End If
End If
Next
For Each picture In graphlist
Dim NewTab As New TabPage
NewTab.Name = picture
NewTab.Text = NewTab.Name
Me.TabControl1.Controls.Add(NewTab)
Me.TabControl1.SelectedIndex = Me.TabControl1.TabCount - 1
For Each func As TL_FUNCTION In functionlist
If (func.picture = picture) Then
Dim label As Label = New Label
label.Text = func.curve.ToString
NewTab.Controls.Add(label) 'This label shows up
Dim key As String
Dim values() As String
For Each key In func.values.Keys
values = func.values.GetValues(key)
For Each value As String In values
Dim label2 As New Label
label2.Text = key.ToString
Dim textb As TextBox = New TextBox
textb.Text = value
NewTab.Controls.Add(label2) 'this one is not shown on the tab
NewTab.Controls.Add(textb) 'this one is not shown on the tab
Next value
Next key
End If
Next
Next
You are placing the new labels and textboxes underneath the new label that you see in the TabPage because you never set their location, so it defaults to point (0, 0).
Try setting the location for the controls:
For Each value As String In values
Dim label2 As New Label
label2.Text = key.ToString
label2.Location = New Point(10, NewTab.Controls.Count * 24)
Dim textb As TextBox = New TextBox
textb.Text = value
textb.Location = New Point(label2.Right + 4, label2.Top)
NewTab.Controls.Add(label2)
NewTab.Controls.Add(textb)
Next value