Maximum value in the track bar - c#

I have a button that adds +100 to the track Bar.
Maximum value 43000, if the value is at 43000 and clicking the button will give error.
Value '43001' is not valid for 'Value'. 'Value' must be between 'Minimum' and 'Maximum'.
private void button41_Click(object sender, EventArgs e)
{
trackBar1.Value = trackBar1.Value += 100;
label27.Text = "" + trackBar1.Value;
}
Issue resolved:
public Form1()
{
me = this;
InitializeComponent();
trackBar1.Maximum = 43000;
trackBar1.Minimum = 40;
}
button
private void button41_Click(object sender, EventArgs e)
{
if (trackBar1.Value + 100 <= trackBar1.Maximum)
{
trackBar1.Value = trackBar1.Value += 100;
label27.Text = "Frequency = " + trackBar1.Value;
}
else
{
MessageBox.Show("Max value = " + trackBar1.Maximum);
}
}
42990 + 100 without errors if I click add
Message displayed when trying add more than the supported value

The message already says all: The value may not be greater than the max-value.
Just add a condition before you increment the value:
if (trackBar1.Value < trackBar1.Maximum)
trackBar1.Value++;
Or here your complete event handler:
private void button41_Click(object sender, EventArgs e)
{
if (trackBar1.Value < trackBar1.Maximum)
{
trackBar1.Value++;
label27.Text = trackBar1.Value;
}
else
{
MessageBox.Show("Max value = " + trackBar1.Maximum);
}
}

Related

NumericupDown comparison Visual Studio c#

I have to make a quantile configuration and I have to set a condition so that the second value is higher than the first, the third value is higher than the second and so on. I am using numericupdowns and the value is set by the user. I tried to implement this code but it always shows a message box error even if the values are correct. This is my code so far (using Visual studio and C#):
decimal min = 1;
//when the first numeric is changed:
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
numericUpDown1.Minimum = min;
min = numericUpDown1.Value;
}
private void numericUpDown2_ValueChanged(object sender, EventArgs e)
{
min++;
numericUpDown2.Minimum = min;
min = numericUpDown2.Value;
}
private void numericUpDown3_ValueChanged(object sender, EventArgs e)
{
min++;
numericUpDown3.Minimum = min;
min = numericUpDown3.Value;
}
private void numericUpDown4_ValueChanged(object sender, EventArgs e)
{
min++;
numericUpDown4.Minimum = min;
numericUpDown4.Maximum = 99;
}
private void button_OK_Click(object sender, EventArgs e)
{
if (
numericUpDown1.Value > numericUpDown2.Value ||
numericUpDown2.Value > numericUpDown3.Value ||
numericUpDown3.Value > numericUpDown4.Value )
{
MessageBox.Show(
"Quantiles are not filled correctly",
"The quantiles aren't filled in correctly", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBoxName.Select();
DialogResult = DialogResult.None;
return;
}
}
You need to keep all the NumericUpDown value inside a List or an array where you can manipulate them like a single entity trhough a loop.
Of course changing the minimum value of one of the numeric elements should be linked to a check for the current value inside that control because you can't have a current value lesser than the new minimum value.
So the first thing to do is to create a global variable inside your form class where you keep the references to all the numeric controls that you want to synchronize
public class Form1
{
private List<NumericUpDown> numbers = new List<NumericUpDown>();
public Form1 : Form
{
InitializeComponent();
numbers.AddRange(new [] {n1, n2,n3,n4,n5});
}
......
}
Now you can write a method like this one that adjust the minimum on all the numeric included in the list
private void UpdateMinimum()
{
for (int x = 0; x < numbers.Count-1; x++)
{
if(numbers[x].Value > numbers[x+1].Value)
numbers[x+1].Value = numbers[x].Value;
numbers[x+1].Minimum = numbers[x].Value;
}
}
finally you have all your NumericUpDown event ValueChanged call the same method
void numerics_ValueChanged(object sender, EventArgs e)
{
UpdateMinimum();
}
If you want to set a condition so that the second value is higher than the first, the third value is higher than the second and so on, you can refer to the following code:
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
numericUpDown1.Minimum = 1;
}
private void numericUpDown2_ValueChanged(object sender, EventArgs e)
{
numericUpDown2.Minimum = numericUpDown1.Value + 1;
}
private void numericUpDown3_ValueChanged(object sender, EventArgs e)
{
numericUpDown3.Minimum = numericUpDown2.Value + 1;
}
private void numericUpDown4_ValueChanged(object sender, EventArgs e)
{
numericUpDown4.Minimum = numericUpDown3.Value + 1;
numericUpDown4.Maximum = 99;
}
private void button_OK_Click(object sender, EventArgs e)
{
if (
numericUpDown1.Value > numericUpDown2.Value ||
numericUpDown2.Value > numericUpDown3.Value ||
numericUpDown3.Value > numericUpDown4.Value)
{
MessageBox.Show(
"Quantiles are not filled correctly",
"The quantiles aren't filled in correctly", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBoxName.Select();
DialogResult = DialogResult.None;
return;
}
}
Here is the test result:

I am writing a c# windows form code

I am writing a c# windows form code to
get the number from button1 and button2 and add them together in a text box but The compiler argues on the convert.toint32(textbox3.text) statement
and also it increases the value of the two variable and three variable how can I keep it constant but increase the value of textbox
and I need a solution?
int Three = 0;
int Two = 0;
//int one = 0;
int sum = 0;
// int sum = 0;
//int dec = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// MessageBox.Show("Enter the teams` name");
}
private void button1_Click(object sender, EventArgs e)
{
//Three += 3;
//textBox3.Text = sum.ToString();
Three += 3;
sum = Convert.ToInt32(textBox3.Text) + Three;
textBox3.Text = sum.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
Two += 2;
sum = Two + Convert.ToInt32(textBox3.Text) + Three;
textBox3.Text =Convert.ToInt32(textBox3.Text) + Two.ToString();
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
textBox3.Text = 0.ToString();
}
`
Change
sum = Convert.ToInt32(textBox3.Text) + Three;
To
sum = Convert.ToInt32(textBox3.Text == "" ? "0" : textBox3.Text) + 3;
Also, remove
private void textBox3_TextChanged(object sender, EventArgs e)
{
textBox3.Text = 0.ToString(); // this
}
because it doesn't make any sense.
Your variables belong to class and they are accessible for initialization in constructor. This can be done in many ways but you need to check if textboxes have values then try to convert it and add it.
private int Two;
private int Three;
private int sum;
public Form1()
{
this.Two = 0;
this.Three = 0;
this.sum = 0;
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// MessageBox.Show("Enter the teams` name");
}
private void button1_Click(object sender, EventArgs e)
{
this.Three += 3;
sum = textBox3.Text != String.Empty ? Convert.ToInt32(textBox3.Text) : 0;
textBox3.Text = Convert.ToString(sum + this.Three);
}
... same for number Two
private void textBox3_TextChanged(object sender, EventArgs e)
{
textBox3.Text = "0";
}

Counter doesn't start on time

Ok, I'm trying to explain this on a simple example.
I want counter to have the value 0 at the beginning. label1 is invisible until I click on button1. My problem now is that when I click on button1 for the first time, 0 appears instead of 1. Meaning I need to click two times on button1, so that "1" appears. (I'm quite new to C#, so don't use jargon please =P)
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
label1.Visible = false;
}
int counter = 0;
private void button1_Click(object sender, EventArgs e)
{
label1.Visible = true;
label1.Text = "number " + counter;
counter++;
}
}
Look closely at your click method:
private void button1_Click(object sender, EventArgs e)
{
label1.Visible = true;
label1.Text = "number " + counter;
counter++;
}
You are first assigning the (current) value of counter to label1.Text and then increment it. Swap statements 2 and 3:
private void button1_Click(object sender, EventArgs e)
{
label1.Visible = true;
counter++;
label1.Text = "number " + counter;
}
Either:
Initialize counter to 1
Increment the counter before showing it
label1.Text = "number " + (++counter).ToString();
or
counter++;
label1.Text = "number " + counter.ToString();
Use (counter+1) as your value
label1.Text = "number " + (counter+1).ToString();

Label is not updating as I am changing values

I am writing a small program to manage store and price based on the item quantity. In the desire functionality I want the Grand Total of all items cost to be displayed in the label. This label updates as I change the values in qty text boxes.
So far I have done writing this code and it is working perfectly and even the values are exactly the same in variables as I want but the problem is label doesn't update even I have null values in the qty text boxes. It keep shows the previous total until I change the values, and it seems get stuck on the last updated total.
namespace wpfTest
{
public partial class MainWindow : Window
{
List<int> myList = new List<int>();
int val1, val2;
int total;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
qtyTxt1.IsEnabled = false;
priceTxt1.IsEnabled = false;
qtyTxt2.IsEnabled = false;
priceTxt2.IsEnabled = false;
}
private void chk1_Click(object sender, RoutedEventArgs e)
{
if (chk1.IsChecked == true)
{
qtyTxt1.IsEnabled = true;
priceTxt1.IsEnabled = true;
}
else
{
qtyTxt1.IsEnabled = false;
priceTxt1.IsEnabled = false;
qtyTxt1.Clear();
priceTxt1.Text = "#50";
}
}
private void chk2_Click(object sender, RoutedEventArgs e)
{
if (chk2.IsChecked == true)
{
qtyTxt2.IsEnabled = true;
priceTxt2.IsEnabled = true;
}
else
{
qtyTxt2.IsEnabled = false;
priceTxt2.IsEnabled = false;
qtyTxt2.Clear();
priceTxt2.Text = "#100";
}
}
private void qtyTxt1_TextChanged(object sender, TextChangedEventArgs e)
{
if (qtyTxt1.Text=="")
{
priceTxt1.Text = "#50";
val1 = 0;
}
if (qtyTxt1.Text.Length > 0)
{
priceTxt1.Text = (Convert.ToInt32(qtyTxt1.Text) * 50).ToString();
val1 = Convert.ToInt32(priceTxt1.Text);
updateTotal();
}
}
private void qtyTxt2_TextChanged(object sender, TextChangedEventArgs e)
{
if (qtyTxt2.Text.Length == 0)
{
priceTxt2.Text = "#100";
val2 = 0;
}
if (qtyTxt2.Text.Length > 0)
{
priceTxt2.Text = (Convert.ToInt32(qtyTxt2.Text) * 100).ToString();
val2 = Convert.ToInt32(priceTxt2.Text);
updateTotal();
}
}
private void updateTotal()
{
lblTotal.Content = "";
lblTotal.Content = (val1 + val2).ToString();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Total : " + total + "\n" + "Val1 : " + val1 + "\n" + "Val2 : " + val2);
}
}
}
I have also uploaded screen shot at my Microsoft OneDrive account because I have limitation here to upload photograph. :
http://1drv.ms/1lM0IdE
Assign to Label Property Text
Try this
lblTotal.Text = "";
lblTotal.Text = (val1 + val2).ToString();
Well the first glaring problem is the fact that your trying to change the UILabel text through a "Content" property that doesn't exist unless you defined it yourself. You should be using the "text" property. Refer to the Apple documentation:
https://developer.apple.com/library/ios/documentation/uikit/reference/UILabel_Class/Reference/UILabel.html

displaying line number in rich text box c#

I have a Multiline richtextbox control into which i want to integrate the feature of adding a line number. i have considered many approaches
Add a label and updating the line numbers as the line count changes
Add a picturebox along with to draw string on it.
Add another textbox along with and show line numbers on it
Add listbox along and display line numbers in it.
I got two doubts.
The richtextbox which i'm using is a custom made control and derieves from RichTextBox class. How can i add multiple controls to it.
What is the best approach to show line numbers for the multiline text in c#
My own example. All is fine, but wordwrap must be disabled :(
int maxLC = 1; //maxLineCount - should be public
private void rTB_KeyUp(object sender, KeyEventArgs e)
{
int linecount = rTB.GetLineFromCharIndex( rTB.TextLength ) + 1;
if (linecount != maxLC)
{
tB_line.Clear();
for (int i = 1; i < linecount+1; i++)
{
tB_line.AppendText(Convert.ToString(i) + "\n");
}
maxLC = linecount;
}
}
where rTB is my richtextbox and tB is textBox next to rTB
J.T. jr
this code helped me thank you, needed to convert visual basic but could:
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
Dim maxlc As Integer = 1
Dim linecount As Integer = TextBox1.GetLineFromCharIndex(TextBox1.Height) + 1
If linecount <> maxlc Then
TextBox2.Clear()
For i = 0 To linecount - 1 Step 1
TextBox2.AppendText(Convert.ToString(i) + vbNewLine)
Next i
maxlc = linecount
End If
End Sub
public int getWidth()
{
int w = 25;
// get total lines of richTextBox1
int line = richTextBox1.Lines.Length;
if (line <= 99)
{
w = 20 + (int)richTextBox1.Font.Size;
}
else if (line <= 999)
{
w = 30 + (int)richTextBox1.Font.Size;
}
else
{
w = 50 + (int)richTextBox1.Font.Size;
}
return w;
}
public void AddLineNumbers()
{
// create & set Point pt to (0,0)
Point pt = new Point(0, 0);
// get First Index & First Line from richTextBox1
int First_Index = richTextBox1.GetCharIndexFromPosition(pt);
int First_Line = richTextBox1.GetLineFromCharIndex(First_Index);
// set X & Y coordinates of Point pt to ClientRectangle Width & Height respectively
pt.X = ClientRectangle.Width;
pt.Y = ClientRectangle.Height;
// get Last Index & Last Line from richTextBox1
int Last_Index = richTextBox1.GetCharIndexFromPosition(pt);
int Last_Line = richTextBox1.GetLineFromCharIndex(Last_Index);
// set Center alignment to LineNumberTextBox
LineNumberTextBox.SelectionAlignment = HorizontalAlignment.Center;
// set LineNumberTextBox text to null & width to getWidth() function value
LineNumberTextBox.Text = "";
LineNumberTextBox.Width = getWidth();
// now add each line number to LineNumberTextBox upto last line
for (int i = First_Line; i <= Last_Line + 2; i++)
{
LineNumberTextBox.Text += i + 1 + "\n";
}
}
private void Form1_Load(object sender, EventArgs e)
{
LineNumberTextBox.Font = richTextBox1.Font;
richTextBox1.Select();
AddLineNumbers();
}
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
Point pt = richTextBox1.GetPositionFromCharIndex(richTextBox1.SelectionStart);
if (pt.X == 1)
{
AddLineNumbers();
}
}
private void richTextBox1_VScroll(object sender, EventArgs e)
{
LineNumberTextBox.Text = "";
AddLineNumbers();
LineNumberTextBox.Invalidate();
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
if (richTextBox1.Text == "")
{
AddLineNumbers();
}
}
private void richTextBox1_FontChanged(object sender, EventArgs e)
{
LineNumberTextBox.Font = richTextBox1.Font;
richTextBox1.Select();
AddLineNumbers();
}
private void LineNumberTextBox_MouseDown(object sender, MouseEventArgs e)
{
richTextBox1.Select();
LineNumberTextBox.DeselectAll();
}
private void Form1_Resize(object sender, EventArgs e)
{
AddLineNumbers();
}
WORKS 100%!!! But you need to add richTextBox2 for line numbers, if you want change it to other
form like listbox, anyway it served me well.
private void richTextBox1_keyDown(object sender, KeyEventArgs e)
{
for (int i = 0; i <= richTextBox1.Lines.Count(); i++)
{
if (!(e.KeyCode == Keys.Back))
{
if (!richTextBox2.Text.Contains(i.ToString()))
{
richTextBox2.Text += i.ToString() + "\n";
}
}
else
{
richTextBox2.Clear();
}
}
}

Categories

Resources