What am doing here is; making a UI to update values visually, will add more support for other types too. Possibly all types.
updateIcons this function is called everytime the controller is loaded, and has new values,names everytime.
countControls to keep track of controllers, so if can update values on clicks.
myP is the object that holds the values taken at runtime, user shuffles values by pressing tab from another screen
created radiobuttons groupboxes to allow radiobutton group to be managed.
properties all belong to one object. each property has few possible values like in my example, the enums.
now am kinda lost, not sure how to best do this, as now my rb_CheckedChanged is returning some kind of mess.
How do i do this the right way ? all together, i feel its somewhat the right approach At least.
I thought of making a dictionary of ? to use it at the checked event. not exactly sure how
private void updateIcons(List<Props> prop) {
countControls++;
locationY = 10;
int gbHeight;
foreach (var p in prop) {
radioButtonY = 10;
IType pType = p.Type;
if (pType is Enum) {
var myP = new MyProp(p, this);
GroupBox gb = new GroupBox();
gb.Location = new Point(nextLocationX,locationY);
nextLocationX += rbWidth+10;
gb.Name = "groupBox" + countControls;
gb.Text = "smthn";
var TypesArray = set here;
gbHeight = TypesArray.Length;
foreach (var type in TypesArray) {
getimagesPath(TypesArray);
RadioButton rb = new RadioButton();
rb.Appearance = Appearance.Button;
rb.Width = rbWidth;
rb.Height = rbHeight;
rb.Name = type.Name + countControls;
rb.Text = type.Name;
string path = imagePaths[type.Name];
Bitmap rbImage = new Bitmap(path);
rb.BackgroundImage = rbImage;
countControls++;
rb.Location = new Point(radioButtonX, radioButtonY);
if (myP.Value != null && type.Name.SafeEquals(myP.Value.ToString())) {
rb.Checked = true;
}
radioButtonY += rbHeight;
gb.Controls.Add(rb);
rb.CheckedChanged += rb_CheckedChanged;
}
gb.Height = rbHeight * gbHeight + 20;
gb.Width = rbWidth + 10;
Controls.Add(gb);
}
}
}
void rb_CheckedChanged(object sender, EventArgs e) {
RadioButton rb = (RadioButton)sender;
Control control = (Control)sender;
if (rb.Checked) {
MessageBox.Show("You have just checked: " + rb.Text);
MessageBox.Show("You have just called Controller: " + control.Name);
var t = PropSeq;
}
else {
MessageBox.Show("you have just unchecked: " + rb.Text);
MessageBox.Show("You have just called Controller: " + control.Name);
}
}
I think your code might be a little messed up and not the easiest to read. It looks plain invalid with not all closing braces present? Try the code below which will create two group boxes, each with five radio buttons. This should help you achieve what you are trying to do (full listing for a basic Form):
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
CreateButton();
}
private void CreateButton()
{
// Add two group boxes
for (int groupCount = 1; groupCount < 3; groupCount++)
{
var groupBox = new GroupBox();
groupBox.Location = new Point(220 * (groupCount - 1), 10);
groupBox.Name = string.Format("groupBox{0}", groupCount);
groupBox.Text = string.Format("Group Box {0}", groupCount);
// Add some radio buttons to each
for (int buttonCount = 1; buttonCount < 6; buttonCount++)
{
var radioButton = new RadioButton();
radioButton.Width = 150;
radioButton.Location = new Point(10, 30 * buttonCount);
radioButton.Appearance = Appearance.Button;
radioButton.Name = string.Format("radioButton{0}", buttonCount);
radioButton.Text = string.Format("Dynamic Radio Button {0} - {1}", groupCount, buttonCount);
radioButton.CheckedChanged += radioButton_CheckedChanged;
// Add radio button to the group box
groupBox.Controls.Add(radioButton);
groupBox.Height += 20;
}
// Add group box to form
Controls.Add(groupBox);
}
}
private void radioButton_CheckedChanged(object sender, EventArgs e)
{
// Get button and only show the selected (not now de-selected item)
var radioButton = (RadioButton)sender;
if (radioButton.Checked)
{
MessageBox.Show("You have just checked: " + radioButton.Text);
}
}
}
}
Related
I can't find any solution or hint on this problem. Problem described after this code.
I must create one picturebox and radiobutton for every folder found on a specific path:
{
InitializeComponent();
string pathtocircuits = "../../tracks";
string[] allfiles = Directory.GetDirectories(pathtocircuits, "*.*", SearchOption.TopDirectoryOnly);
int imgx = 387;
int imgy = 153;
int radx = 428;
int rady = 259;
String track = "";
String pici = "";
String pic = "pictureBox";
String rad = "radiobutton";
String radr = "";
String picr = "";
foreach (String file in allfiles)
{
track = Path.GetFileName(file);
pici = "../../tracks/" + track + "/p_" + track + ".png";
picr = pic + element.ToString();
radr = rad + element.ToString();
PictureBox pb = new PictureBox();
pb.Location = new System.Drawing.Point(imgx, imgy); ;
pb.Image = Image.FromFile(pici);
pb.Width = 100;
pb.Height = 100;
pb.SizeMode = PictureBoxSizeMode.StretchImage;
pb.Name = picr;
Controls.Add(pb);
RadioButton rdo = new RadioButton();
rdo.Name = radr;
rdo.Text = "";
rdo.Tag = track;
rdo.Location = new Point(radx, rady);
this.Controls.Add(rdo);
element += 1;
imgx += 110;
radx += 110;
}
}
With this part I get to create the elements I need (it works).
My problem is when I press a button to reach Form2. How can I check which radiobutton is selected and store its Tag value in a String?
for(int i = 0; i<element; i++)
{
if( ??? .Checked == true )
{
globalstring = ??? .Tag;
}
}
If I try to use the name of a created radiobutton instead of a ??? it gives me an error like 'element ??? does not have a Checked or Tag attribute'
Add method below
Add to For loop : rdo.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
private void radioButton_CheckedChanged(object sender, EventArgs e)
{
RadioButton button = sender as RadioButton;
string name = button.Text;
}
RadioButtons as any other control are stored in the Controls collection of their container. If you add them directly to the form you can retrieve them using this code.
protected void Button1_Click(object sender, EventArgs e)
{
var radio = this.Controls.OfType<RadioButton>().FirstOrDefault(x => x.Checked);
if(radio != null)
{
string tag = radio.Tag.ToString();
.....
// Form2 = new Form2(tag);
}
}
The radiobuttons are added to a same group by default, so you can get the checked radiobutton as followed.
List<RadioButton> radioButtons = this.Controls.OfType<RadioButton>().ToList();
RadioButton rb = radioButtons
.Where(r => r.Checked)
.Single();
string tag = rb.Tag.ToString();
I wanna change the Text properties of Label using Buttons just like in hangman; but after I created the Label, I became confused when I try to access the specific Label
// creating label
for (int i = 0; i < numericUpDown1.Value; i++)
{
Label l = new Label();
l.Text = "_";
l.Width = 20;
l.Height = 25;
l.Left = i * 20 + 510;
l.Top = 20;
l.BackColor = Color.Transparent;
groupBox2.Controls.Add(l);
}
// function to change the label text
// if I clicked the button
// the first label text will be changed to the text in the button i clicked
private void B_Click(object sender, EventArgs e)
{
var thsBtn = (Button)sender;
bool benar = false;
if (benar == false)
{
thsBtn.Text = " ";
thsBtn.Enabled = false;
}
else
{
thsBtn.Enabled = false;
}
}
You can organize created Labels into a collection, say, List<Label>:
private List<Label> m_CreatedLabels = new List<Label>();
...
// Remove all previous labels
foreach (Label lbl in m_CreatedLabels)
lbl.Dispose();
m_CreatedLabels.Clear();
// Create new ones
for (int i = 0; i < numericUpDown1.Value; i++) {
m_CreatedLabels.Add(new Label() {
Text = "_",
Width = 20,
Height = 25,
Left = i * 20 + 510,
Top = 20,
BackColor = Color.Transparent,
Parent = groupBox2
});
}
Now you have m_CreatedLabels collection to work with created Labels, e.g.
private void B_Click(object sender, EventArgs e) {
var thsBtn = sender as Button;
// you may want to add a condition into FirstOrDefault(), e.g.
// .FirstOrDefault(lbl => lbl.Text == "_")
// - first label with "_" Text
Label lblToProcess = m_CreatedLabels
.FirstOrDefault();
if (null != lblToProcess)
lblToProcess.Text = thsBtn.Text;
thsBtn.Enabled = false;
}
One option here is to give your dynamically created Label instances a Name. From there, you should be able to use ControlCollection.Find to find your Label instances by name.
private void CreateLabels()
{
for (int i = 0; i < numericUpDown1.Value; i++)
{
Label l = new Label();
l.Name = $"DynamicLabel{i}";
l.Text = "_";
l.Width = 20;
l.Height = 25;
l.Left = i * 20 + 510;
l.Top = 20;
l.BackColor = Color.Transparent;
groupBox2.Controls.Add(l);
}
}
private void DoSomethingWithADynamicLabel(int dynamicLabelIndex)
{
Label l = groupBox2.Controls.Find($"DynamicLabel{i}", true).FirstOrDefault() as Label;
if (l is null)
{
// Couldn't find the label...
return;
}
// Do something with l
}
When creating the Label instances inside CreateLabels, I'm simply appending the for loop's counter to the string "DynamicLabel". This gives you a bunch of Labels with names like "DynamicLabel0", "DynamicLable1", "DynamicLabel2", etc...
Then in DoSomethingWithADynamicLabel, assuming you have the index of the Label you want to deal with, you can use groupBox2.Controls.Find to actually find the Label you're interested in. ControlCollection.Find returns Control[], so calling FirstOrDefault will take the first item from the array or null if no Control with the given name exists.
Im trying to make an app that adds shopping items with the price(just for education purpose). I want to add a button as well so that I can delete this item if I want too. this is what Ive done:
class ShopItem
{
string name;
decimal price;
int labelNumber;
public ShopItem(string name, decimal price, int labelNumber)
{
this.name = name;
this.price = price;
this.labelNumber = labelNumber;
}
public string GetName()
{
return name;
}
public decimal GetPrice()
{
return price;
}
public int GetLabelNumber()
{
return labelNumber;
}
}
Then in my main class:
public partial class Form1 : Form
{
List<ShopItem> shopItems = new List<ShopItem>();
List<Button> buttons = new List<Button>();
List<Label> labelsList = new List<Label>();
int shopItemNumber;
int top = 50;
int left = 50;
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
this.shopItemNumber = shopItems.Count;
label3.Text = "";
label3.Text = shopItemNumber.ToString();
shopItems.Add(new ShopItem(textBox1.Text, Convert.ToDecimal(textBox2.Text), shopItemNumber));
AddLabel();
AddAButton();
}
void AddAButton()
{
Button newButton = new Button();
buttons.Add(newButton);
newButton.Left = left + 100;
newButton.Top = top;
newButton.Text = "DELETE";
panel1.Controls.Add(newButton);
top += 40;
}
void AddLabel()
{
Label newLabel = new Label();
labelsList.Add(newLabel);
newLabel.Left = left;
newLabel.Top = top;
newLabel.Text = shopItems[shopItemNumber].GetName() + " " + shopItems[shopItemNumber].GetPrice();
panel1.Controls.Add(newLabel);
}
}
Im adding new objects and then checking the list count so that I know each item number. Now I dont know how to make buttons to remove each item. I would also like to know what is the best practice for this type of tasks? I dont want to use database as its too complicated for now, just make it in runtime? How would you, advanced guys, would make it?
I managed to do something like this based on your answers:
public partial class Form1 : Form
{
List<ShopItem> shopItems = new List<ShopItem>();
List<Button> buttons = new List<Button>();
List<Label> labelsList = new List<Label>();
int shopItemID;
int top = 50;
int left = 50;
public Form1()
{
InitializeComponent();
shopItemID = 0;
}
private void Button1_Click(object sender, EventArgs e)
{
int shopItemID = this.shopItemID;
label3.Text = "";
label3.Text = shopItemID.ToString();
decimal price;
if(Decimal.TryParse(textBox2.Text.Replace(",", "."), out price))
{
shopItems.Add(new ShopItem(textBox1.Text, price, shopItemID));
label3.Text = price.ToString();
AddLabel();
AddAButton();
this.shopItemID++;
}
else
{
label3.Text = "Wrong price format provided";
}
}
void AddAButton()
{
Button newButton = new Button();
buttons.Add(newButton);
newButton.Left = left + 100;
newButton.Top = top;
newButton.Text = "DELETE";
panel1.Controls.Add(newButton);
top += 40;
newButton.Tag = this.shopItemID;
newButton.Click += DeleteClicked;
}
void DeleteClicked(object sender, EventArgs e)
{
Button button = (Button)sender;
int itemNumber = (int)button.Tag;
ShopItem item = shopItems[itemNumber];
button.Dispose();
labelsList[itemNumber].Dispose();
}
void AddLabel()
{
Label newLabel = new Label();
labelsList.Add(newLabel);
newLabel.Left = left;
newLabel.Top = top;
newLabel.Text = shopItems[shopItemID].GetName() + " " + shopItems[shopItemID].GetPrice();
newLabel.Tag = this.shopItemID;
panel1.Controls.Add(newLabel);
}
}
It's working as I wanted it. The question is: The way I did it was according to the right coding practice?
The other question is: I would like to add placement functionality so that if some item is deleted other items go up the way. Should I use some position list? Can All of those items be contained within a ShopItem class? I mean, can I add button, label and other stuff withing this class so they all are created at the same time and becomes one object?
First thing you'll want to do is start using IDs instead of the count. The reason is, let's say you have 5 items (0 through 4) and you delete one. Now when you add another new one, it will get #4 because the count is 4 again (before adding the new one to the list), which means you'll have two #4s.
What you'll want to do instead is something like look for the highest existing number and add 1 to it:
this.shopItemNumber = shopItems.Any() ? shopItems.Max(item => item.labelNumber) + 1 : 0;
Now for the Delete button, just pass the item's number to the AddAButton() method:
AddAButton(shopItemNumber);
...and change the method's signature accordingly:
void AddAButton(int itemNumber)
And then in the AddAButton() method, add that number to the Tag property (you'll see why below):
newButton.Tag = itemNumber;
You'll also need to subscribe to the button's Click event:
newButton.Click += DeleteClicked;
Then create the DeleteClicked() method. This is where you'll make use of that Tag property:
void DeleteClicked(object sender, EventArgs e)
{
Button button = (Button)sender;
int itemNumber = (int)button.Tag;
ShopItem item = shopItems.FirstOrDefault(item => item.labelNumber = itemNumber);
if (item != null)
shopItems.Remove(item);
}
You'll also want to clean up the controls. Make sure to Dispose() them after removing them.
You just have to identify what Label belongs to the button.
Make AddLabel return the label it just created:
Label AddLabel()
{
Label newLabel = new Label();
labelsList.Add(newLabel);
newLabel.Left = left;
newLabel.Top = top;
newLabel.Text = shopItems[shopItemNumber].GetName() + " " + shopItems[shopItemNumber].GetPrice();
panel1.Controls.Add(newLabel);
return newLabel;
}
Pass the label to the new button, and add code to remove it. Store the current shopItem in a variable, so you can access it from the removal code.
void AddAButton(Label newLabel)
{
Button newButton = new Button();
buttons.Add(newButton);
newButton.Left = left + 100;
newButton.Top = top;
newButton.Text = "DELETE";
panel1.Controls.Add(newButton);
top += 40;
// Store the current shopItem in the context (for the lambda)
var shopItem = shopItems[shopItemNumber]
newButton.Click += (s, e) => {
panel1.Controls.Remove(newLabel);
panel1.Controls.Remove(newButton);
newLabel.Dispose();
newButton.Dispose();
shopItems.Remove(shopItem);
}
}
Since controls are IDisposable, it is advised to call Dispose after removing them.
Background
I create a set of linklabel and label controls using a loop that uses data from a database as there content (Text).
Question
How do I then remove or change there visibility?
What I would Like to Happen?
On a button click event, I would like all of the link's and linklabel's text properties to be set to either null, or their visibility properties to be set as false.
Code
private void getInfoStationID()
{
//SQL Connection Stuff
for (int i = 0; i <= rowCount - 1; i++)
{
LinkLabel Linklabel = new LinkLabel();
Linklabel.Text = ds.Tables[0].Rows[i] ["code"].ToString();
Linklabel.Height = 15;
Linklabel.Width = 50;
Linklabel.AutoSize = true;
Linklabel.Location = new Point(10, (i + 1) * 30);
tabControl1.TabPages[0].Controls.Add(Linklabel);
// Add an event handler to do something when the links are clicked.
Linklabel.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
Label label1 = new Label();
label1.Text = ds.Tables[0].Rows[i]["name"].ToString();
label1.Height = 15;
label1.Width = 70;
label1.Location = new Point(100, (i + 1) * 30);
tabControl1.TabPages[0].Controls.Add(label1);
Label label3 = new Label();
label3.Text = ds.Tables[0].Rows[i]["toc"].ToString();
label3.Height = 15;
label3.Width = 50;
label3.Location = new Point(240, (i + 1) * 30);
tabControl1.TabPages[0].Controls.Add(label3);
}
}
private void clearAllBtn_Click(object sender, EventArgs e)
{
//Would like this to clear all previously drawn labels and linklabels
}
Simply add the dynamic controls to a List so you have a quick reference to them:
// out at CLASS/FORM level:
private List<Control> MyControls = new List<Control>();
// ... some method ...
for (int i = 0; i <= rowCount - 1; i++)
{
LinkLabel Linklabel = new LinkLabel();
MyControls.Add(Linklabel);
// ... rest of your code ...
Label label1 = new Label();
MyControls.Add(label1);
// ... rest of your code ...
Label label3 = new Label();
MyControls.Add(label3);
// ... rest of your code ...
}
Now you can use that List from somewhere else:
private void clearAllBtn_Click(object sender, EventArgs e)
{
foreach(Control ctl in MyControls)
{
ctl.Visible = false; // or something else
}
}
*Don't forget to dispose of those controls and empty the list if you decide to create a new set of dynamic controls. If you want to completely get rid of them:
private void clearAllBtn_Click(object sender, EventArgs e)
{
foreach(Control ctl in MyControls)
{
ctl.Dispose();
}
MyControls.Clear();
}
You can loop through all controls on a certain tabpage. You could use a open generic function to make the code nice and clean. Like this:
private void HideControls<TControl>(Control parentControl)
where TControl : Control
{
var controls = parentControl.Controls.OfType<TControl>();
foreach (var control in controls)
{
control.Visible = false;
}
}
And use it like this:
private void button1_Click(object sender, EventArgs e)
{
this.HideControls<Label>(tabControl1.TabPages[0]);
this.HideControls<LinkLabel>(tabControl1.TabPages[0]);
}
You could even refactor this to a nice extension method:
public static class ControlExtensions
{
public static void HideControlsOfType<TControl>(this Control parentControl)
where TControl : Control
{
var controls = parentControl.Controls.OfType<TControl>();
foreach (var control in controls)
{
control.Visible = false;
}
}
}
and use like:
this.tabControl1.TabPages[0].HideControlsOfType<Label>();
I'm trying to make a simple program with GUI in c# but unfortunately I have some difficulties. Now I'll try to explain the basic structure of my program. I have 3 classes for three different groups of people(University Professors, University Students and people who don't work or study either). I have some methods for reading information from a file(professor's title, name, university name, student's faculty number, etc.). I read the file line by line and save the information in an object of type one of the 3 classes. After that I put that object in a List. So here comes my problem. I want to read every object from the list and take its name and put in on a dynamically created labels on other windows form. Here it is a little part of my code:
private void button1_Click(object sender, EventArgs e)
{
ForeignPeople fPeople = new ForeignPeople();
UniversityProfessors uProf = new UniversityProfessors();
UniversityStudents uStudents = new UniversityStudents();
if (radioButton1.Checked == true)
{
objList1 = loadList();
Form2 f2 = new Form2();
for (int i = 0; i < objList1.Count; i++)
{
if (objList1[i] is UniversityStudents)
{
uStudents = (UniversityStudents)objList1[i];
tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
Label et_tag = new Label();
et_tag.Name = "label" + i.ToString();
et_tag.Text = uStudents.getFirstName().ToString() + " " + uStudents.getLastName().ToString();
et_tag.AutoSize = true;
f2.tableLayoutPanel1.Controls.Add(et_tag, 0, i);
Label op = new Label();
op.AutoSize = true;
op.Text = "description";
f2.tableLayoutPanel1.Controls.Add(op, 1, i);
}
else if (objList1[i] is UniversityProfessors)
{
uProf = (UniversityProfessors)objList1[i];
tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
Label et_tag = new Label();
Label label = new Label();
et_tag.Name = "label" + i.ToString();
et_tag.Text = uProf.getFirstName().ToString() + " " + uProf.getLastName().ToString();
et_tag.AutoSize = true;
f2.tableLayoutPanel1.Controls.Add(et_tag, 0, i);
Label op = new Label();
op.AutoSize = true;
op.Text = "description";
f2.tableLayoutPanel1.Controls.Add(op, 1, i);
}
else if (objList1[i] is ForeignPeople)
{
fPeople = (ForeignPeople)objList1[i];
String name, Name;
tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
Label et_tag = new Label();
et_tag.Name = "label" + i.ToString();
et_tag.Text = fPeople.getFirstName().ToString() + " " + fPeople.getLastName().ToString();;
et_tag.AutoSize = true;
f2.tableLayoutPanel1.Controls.Add(et_tag, 0, i);
Label op = new Label();
op.AutoSize = true;
op.Text = "description";
f2.tableLayoutPanel1.Controls.Add(op, 1, i);
}
}
f2.FormClosed += new FormClosedEventHandler(childFormClosed);
f2.Show();
this.Hide();
}
But if I have two or more lines which belongs to one Object(for example I have two or more students, or two or more professors in the file) the text of all the labels becomes with the name of the last read object. I know that the problem is in the List or in the cast which I make but I can't figure out another way of doing what I want. I'll be extremely grateful if someone can help.
In addition to the change that phoog mentioned in the comments, I would also move the instantiation of your "people" objects down into the section for each within the loop. That way you can be sure that the old one is being properly destroyed. I would also add a check to make sure that the cast worked.
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked == true)
{
objList1 = loadList();
Form2 f2 = new Form2();
for (int i = 0; i < objList1.Count; i++)
{
if (objList1[i] is UniversityStudents)
{
UniversityStudents uStudents = (UniversityStudents)objList1[i];
if (uStudents != null)
{
// do stuff
}
else
{
// do something sensible with the error here
}
}
// if clauses for the other "people" objects
// ...
}
f2.FormClosed += new FormClosedEventHandler(childFormClosed);
f2.Show();
this.Hide();
}
}