Increment/Decrement value from NumericUpDown - c#

I have the following problem. I create a form with two NumericUpDown and I want to increment/decrement the value from them by 0.01. I tried to use NumericUpDownObject.Increment=0.01M, but there is no change after that, the value don't change. This is the code:
namespace Proiect1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int i = 1;
int o = 1;
private void button1_Click(object sender, EventArgs e)
{
Label l = new Label();
Label l2 = new Label();
NumericUpDown t = new NumericUpDown();
NumericUpDown t2 = new NumericUpDown();
l.Top = i * 25;
l.Left = 3;
l2.Top = i * 25;
l2.Left = 63;
t.Value = 0;
t.Increment = 0.01M;
t.Top = i * 25;
t.Left = 30;
t2.Value = 0;
t2.Top = i * 25;
t2.Left = 90;
i++;
t.Size =new System.Drawing.Size(30, 30);
t2.Size = new System.Drawing.Size(30, 30);
l.Size = new System.Drawing.Size(25, 20);
l2.Size = new System.Drawing.Size(29, 20);
l.Text = "n" + o;
l2.Text = "w" + o;
o++;
panel1.Controls.Add(t);
panel1.Controls.Add(t2);
panel1.Controls.Add(l);
panel1.Controls.Add(l2);
panel1.AutoScroll = true;
}

Try this
This will setup the whole form when it's created, but only increment when clicked.
It looks like your solution is re-creating everything each time it's clicked.
P.s apols - been a while since I did Winforms/WPF
namespace Proiect1
{
public partial class Form1 : Form
{
NumericUpDown _t = new NumericUpDown();
NumericUpDown _t2 = new NumericUpDown();
Label _l = new Label();
Label _l2 = new Label();
public Form1()
{
InitializeComponent();
int i = 1;
int o = 1;
_l.Top = i * 25;
_l.Left = 3;
_l2.Top = i * 25;
_l2.Left = 63;
_t.Value = 0;
_t.Increment = 0.01;
_t.Top = i * 25;
_t.Left = 30;
_t2.Value = 0;
_t2.Top = i * 25;
_t2.Left = 90;
i++;
_t.Size =new System.Drawing.Size(30, 30);
_t2.Size = new System.Drawing.Size(30, 30);
_l.Size = new System.Drawing.Size(25, 20);
_l2.Size = new System.Drawing.Size(29, 20);
_l.Text = "n" + o;
_l2.Text = "w" + o;
o++;
panel1.Controls.Add(_t);
panel1.Controls.Add(_t2);
panel1.Controls.Add(l_);
panel1.Controls.Add(l_2);
panel1.AutoScroll = true;
}
private void button1_Click(object sender, EventArgs e)
{
_t.Increment = 0.01;
}

Related

Dynamically update button

I am attempting to dynamically add a list of buttons:
private void updateClientListUI()
{
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(this.updateClientListUI));
}
else
{
int count = 1;
int x = 0;
int y = 0;
for (int i = 0; i < 5; i++)
{
Button btn = new Button();
btn.Text = count.ToString();
btn.Name = count.ToString();
btn.Size = new Size(35, 35);
btn.Location = new Point(150, 150 * y);
//btn.Dock = DockStyle.Fill;
y++;
count++;
Controls.Add(btn);
}
}
}
Unfortunately this does not apply any buttons to the form.
In addition I was wondering how could I append these buttons in a panel called subPanelClient
This worked for me, the issue was the position of the button, as you have to indicate it within a panel or form.
In this case i just docked them to the panel
private void updateClientListUI()
{
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(this.updateClientListUI));
}
else
{
//Debug.WriteLine(clientNames[0]);
int basex = subPanelClient.Location.X;
int basey = subPanelClient.Location.Y;
subPanelClient.Controls.Clear();
Debug.WriteLine(clientNames.Count);
for (int i = 0; i < clientNames.Count; i++)
{
Button b = new Button();
b.Left = basex;
b.Top = basey;
b.Size = new Size(25, 25); // <== add this line
b.Dock = DockStyle.Top;
b.ForeColor = Color.Gainsboro;
b.FlatStyle = FlatStyle.Flat;
b.FlatAppearance.BorderSize = 0;
b.Padding = new Padding(35, 0, 0, 0);
b.TextAlign = ContentAlignment.MiddleLeft;
basey += 25;
b.Name = clientNames[i];
b.Text = clientNames[i];
subPanelClient.Controls.Add(b);
buttonsAdded.Insert(i, b);
}
}
}

How to use properties of text boxes outside of the current context in visual studio [duplicate]

I have created WinForm App in which user can set how many textboxes he want (Range 1-99)
I am using this code to create Textboxes during runtime
for (int i = 0; i < Calculation.Num; i++)
{
TextBox txtRun = new TextBox();
txtRun.Name = "txtBox" + i;
txtRun.Location = new System.Drawing.Point(35, 50 + (20 * i) * 2);
txtRun.Size = new System.Drawing.Size(75, 25);
this.Controls.Add(txtRun);
}
Suppose user create 2 textboxes and then enter data in each textbox and click calculate button
Now i want to get the textboxes data and divide it by 100
See The Picture I want txtbox1 and txtbox2 data
EDIT 3:
This is the Whole Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GPA_Calculatior__New_
{
public partial class Form1 : Form
{
int j = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Label + Marks Obtained Textbox
for (int i = 0; i < Calculation.Num; i++)
{
Label lblCount = new Label();
lblCount.Name = "lblCount" + i;
lblCount.Location = new System.Drawing.Point(5, 55 + (20 * i) * 2);
lblCount.Size = new System.Drawing.Size(20, 30);
lblCount.Text = (i + 1).ToString();
this.Controls.Add(lblCount);
TextBox txtRun = new TextBox();
txtRun.Name = "txtBox" + i;
txtRun.Location = new System.Drawing.Point(35, 50 + (20 * i) * 2);
txtRun.Size = new System.Drawing.Size(75, 25);
this.Controls.Add(txtRun);
}
//Creating Textbox which is for total marks
for (j = 0; j < Calculation.Num; j++)
{
TextBox txtRun = new TextBox();
txtRun.Name = "TotaltxtBox" + j;
txtRun.Location = new System.Drawing.Point(160, 50 + (20 * j) * 2);
txtRun.Size = new System.Drawing.Size(50, 25);
txtRun.Text = "100";
txtRun.Enabled = false;
this.Controls.Add(txtRun);
}
// Creating 2 Buttons (Calculate and Back)
for (int k = 0; k < 2; k++)
{
Button Btn = new Button();
Btn.Name = "btn" + k;
Btn.Location = new System.Drawing.Point(20 + (k *110), 60 + (20 * j) * 2);
Btn.Size = new System.Drawing.Size(90, 30);
if (k == 0)
Btn.Text = "Back";
else
Btn.Text = "Calculate";
Btn.Click += button_Click;
this.Controls.Add(Btn);
}
//Just for Giving free space in last
Label lbl = new Label();
lbl.Name = "lbl" + j;
lbl.Location = new System.Drawing.Point(30, 90 + (20 * j) * 2);
lbl.Size = new System.Drawing.Size(90, 30);
lbl.Text = "";
this.Controls.Add(lbl);
//**********************************************
}
//Caculate and back button function
private void button_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
if (btn.Name.Equals("btn1"))
{
for (int i = 0; i < Calculation.Num; i++)
{
}
}
else
{
GPA_Calculator mainForm = new GPA_Calculator();
mainForm.Show();
this.Hide();
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
for (j = 0; j < 10; j++)
{
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
}
}
var sum = this.Controls.OfType<TextBox>()
.Where(t => char.IsDigit(t.Name.Reverse().Take(1).FirstOrDefault())
&& t.Enabled)
.Select(t =>
{
double i;
if (!double.TryParse(t.Text, out i)) { return 0d; }
return i / 100d;
})
.Sum();
This may work, I built a separate class to store the control and its value, then you can work out the values independently from the rest of the form. You need to trigger the calculations though:
private List<InfoTextBox> activeTextBoxes = new List<InfoTextBox>();
public Form1()
{
for (int i = 0; i < Calculation.Num; i++)
{
TextBox txtRun = new TextBox();
txtRun.Name = "txtBox" + i;
txtRun.Location = new System.Drawing.Point(35, 50 + (20 * i) * 2);
txtRun.Size = new System.Drawing.Size(75, 25);
this.Controls.Add(txtRun);
InfoTextBox iBox = new InfoTextBox();
iBox.textbox = txtRun;
activeTextBoxes.Add(iBox);
}
}
public class InfoTextBox
{
private double _textboxValue;
public TextBox textbox { get; set; }
public double TextBoxValue { get { return _textboxValue; } set { _textboxValue = setValue(value); } }
private double setValue(double invalue)
{
return invalue / 100;
}
}

Strange drawing behavior with labels

on startup I'm generating a lot of controls 90 to be exact and everything is working ok EXCEPT for the labels they are not being drawn or something? they are there because I can click them and they show proper ID (click event) here's the genereation code
private static bool ClientsLoaded = false;
private static WebBrowser[] Clients = new WebBrowser[45];
private static Label[] ClientLabel = new Label[45];
private static int MaximizedClient = -1;
public Form1()
{
InitializeComponent();
int WBoffsetX = 0;
int WBoffsetY = 0;
int lbloffsetX = 0;
int lbloffsetY = 0;
for (int i = 0; i < 45; i++)
{
var wb = new WebBrowser();
Clients[i] = wb;
wb.ScrollBarsEnabled = false;
wb.Height = 12;
wb.Width = 12;
wb.Location = new Point(2 + WBoffsetX, 2 + WBoffsetY);
WBoffsetX += 13;
wb.ScriptErrorsSuppressed = true;
this.Controls.Add(wb);
ClientLabel[i] = new Label();
ClientLabel[i].Name = "lbl_" + i;
ClientLabel[i].Font = new Font("Arial", 12);
ClientLabel[i].ForeColor = System.Drawing.Color.White;
ClientLabel[i].Location = new Point(12 + lbloffsetX, 450 + lbloffsetY);
lbloffsetX += 22;
ClientLabel[i].Click += new EventHandler(lbl_click);
ClientLabel[i].Text = "C" + i + ": o";
this.Controls.Add(ClientLabel[i]);
}
}
I've tried adding a button with for(45) clientlabel[i].Refresh() and it did nothing I tried changing the visibilty of them all to false and then back to true and nothing however I did find 1 thing interesting if I hide lbl_1 label 2 text will appear if I had label 2 label 3 text will appear but if I change the previous label back to visible they stay invisible textwise
I can click in a line on the form and
private void lbl_click(object sender, EventArgs e)
{
int id = -1;
var s = sender.ToString();
for(int i = 0; i<=45; i++)
{
if (s.Contains("C" + i + ":"))
{
id = i;
}
}
MessageBox.Show("Hello label, " + id);
}
will pop up the proper ids etc
does anyone know what's causing this maybe? or how to fix it
Well, I don't know what is the problem. This code works well enough and it has only marginal differences with the original(AutoSize property, explicit statement of Height and Width, and minor Location adjustment):
for (int i = 0; i < ClientLabel.Length; i++)
{
// Web browsers
WebBrowser wb = new WebBrowser()
{
ScrollBarsEnabled = false,
Height = 12,
Width = 12,
Location = new Point(2 + WBoffsetX, 2 + WBoffsetY),
ScriptErrorsSuppressed = true
};
WBoffsetX += 13;
Clients[i] = wb;
// Labels
Label label = new Label()
{
Name = "label_" + i,
Text = "Data",
AutoSize = true,
Location = new Point(50 + lbloffsetX, 50 + lbloffsetY),
Width = 100,
Height = 20,
Font = new Font("Arial", 12),
ForeColor = System.Drawing.Color.White,
};
label.Click += new EventHandler(lbl_click);
ClientLabel[i] = label;
lbloffsetX += 30;
}
this.Controls.AddRange(Clients);
this.Controls.AddRange(ClientLabel);

Add label to Panel programmatically

So I have a form, and I want to add some Panels with some controls(labels, and radiobuttons) when the form loads.
And I want to do it from the code, of course(it's for making an application with tests, and the questions will be random)
This is what I have done till now:
List<Panel>ls=new List<Panel>();
private void VizualizareTest_Load(object sender, EventArgs e)
{
for (int i = 0; i < 4; i++)
{
Panel pan = new Panel();
pan.Name = "panel" + i;
ls.Add(pan);
Label l = new Label();
l.Text = "l"+i;
pan.Controls.Add(l);
pan.Show();
}
}
But it doesn't show anything on the form.
Add the panel just created to the Form.Controls collection
private void VizualizareTest_Load(object sender, EventArgs e)
{
for (int i = 0; i < 4; i++)
{
Panel pan = new Panel();
pan.Name = "panel" + i;
ls.Add(pan);
Label l = new Label();
l.Text = "l"+i;
pan.Location = new Point(10, i * 100);
pan.Size = new Size(200, 90); // just an example
pan.Controls.Add(l);
this.Controls.Add(pan);
}
}
enter image description here
private void button2_Click(object sender, EventArgs e)
{
int X = 153;
int Y = 34;
for (int i = 1; i < 4; i++)
{
Panel pnl = new Panel();
pnl.SuspendLayout();
pnl.Location = new Point(X, Y);
pnl.Name = "pnl"+i;
pnl.Size = new Size(200, 57);
pnl.BorderStyle = BorderStyle.FixedSingle;
Label lbl = new Label();
lbl.Location = new Point(X - 100, Y - 17);
lbl.Name = "lbl" + i;
lbl.Size = new Size(75, 23);
lbl.Text = "lable_" +i;
pnl.Controls.Add(lbl);
pnl.ResumeLayout(false);
this.Controls.Add(pnl);
Y = Y + 95;
}
}
why not display label2 & label3?

Checkbox event handling

I have a MyControl class. Inside MyClass object there are textboxes and a checkbox.
I am trying to add a checkbox event handler, but it does not work. What can be the problem?
private List<MyControls> _myControls = new List<MyControls>();
MyControls mc = new MyControls();
public void CreateFormElements(int i, StringReader sr)
{
ProductForm form2 = new ProductForm();
form2.Visible = true;
form2.Activate();
String line = "";
for (int n = 0; n < i; n++)
{
line = sr.ReadLine();
mc = new MyControls();
if (line.Length > 3)
{
String[] _line = line.Split(new char[] { '\t' });
mc.SetY(30 + n * 20);
mc.initElements(_line, n);
_myControls.Add(mc);
**mc.cb.CheckedChanged += cb_CheckedChanged;**
}
}
}
private void cb_CheckedChanged(Object sender, EventArgs e)
{
string NameSet = (sender as CheckBox).Name.Split(new char[]{'_'})[1];
MessageBox.Show(NameSet);
}
Here is the code of MyControls Class:
class MyControls
{
int x=5;
int y=30;
public CheckBox cb = new CheckBox();
public TextBox tb1 = new TextBox();
public TextBox tbSpecs = new TextBox();
public TextBox tb3 = new TextBox();
public TextBox tb4 = new TextBox();
public void initElements(String[] name, int i)
{
cb.Width = 10;
cb.Height = 10;
cb.Name = "cb_" + i.ToString();
cb.Location = new Point(x, y+5);
cb.Checked = false;
Form.ActiveForm.Controls.Add(cb);
x += 15;
tb1.Width = 50;
tb1.Height = 20;
tb1.Location = new Point(x, y);
tb1.Name = "tb1_" + i.ToString();
tb1.Text = name[0];
Form.ActiveForm.Controls.Add(tb1);
x += 60;
tbSpecs.Width = 150;
tbSpecs.Height = 20;
tbSpecs.Name = "tb2_" + i.ToString();
tbSpecs.Text = name[1];
tbSpecs.Location = new Point(x, y);
Form.ActiveForm.Controls.Add(tbSpecs);
x += 160;
tb3.Width = 40;
tb3.Height = 20;
tb3.Name = "tb3_" + i.ToString();
tb3.Text = name[2];
tb3.Location = new Point(x, y);
Form.ActiveForm.Controls.Add(tb3);
x += 50;
tb4.Width = 450;
tb4.Height = 20;
tb4.Name = "tb4_" + i.ToString();
tb4.Text = name[3];
tb4.Location = new Point(x, y);
Form.ActiveForm.Controls.Add(tb4);
x = 0;
}
public int SetX(int X)
{
x = X;
return x;
}
public int SetY(int Y)
{
y = Y;
return y;
}
}
mc.cb.CheckedChanged += new System.EventHandler(this.cb_CheckedChanged)
A few points to check in CreateFormElements(int i, StringReader sr){...}:
Is i greater than zero? If not the loop will never run and the event handler will never get attached.
Is line.Length ever greater than zero? If not you'll never get inside the if-block and the handler won't get attached.

Categories