I am just a student begining to study C#, so I apologize if my questions are not very clear. I am stuck for the answer. I don't know how to code the ArgumentOutofRangeException, so the user doesn't go beyond the edges of the Lists. I have 2 of them, with two index variables. I also have a problem with updateControls. Any help would be greatly appreciated.
private void updateControls()
{
pictureBox1.Image = resList[currentResIndex].Photo;
lblName.Text = resList[currentResIndex].Title;
lblCity.Text = resList[currentResIndex].City;
lblPrice.Text = resList[currentResIndex].Price.ToString("C");
pictureBox1.Image = comList[currentCommIndex].Photo;
lblName.Text = comList[currentCommIndex].Title;
lblCity.Text = comList[currentCommIndex].City;
lblPrice.Text = comList[currentCommIndex].Price.ToString("C");
}
private void btnNext_Click(object sender, EventArgs e)
{
if (cboType.SelectedItem.ToString() == "Residential") //if they chose residential then increment resIndex
currentResIndex++;
updateControls();
else
currentCommIndex++;//or else commIndex
updateControls();
}
if (cboType.SelectedItem.ToString() == "Residential"
&& currentResIndex < resList.Count -1) // add this condition
currentResIndex++;
updateControls();
You could just enforce the range in your btnNext_Click method:
private void btnNext_Click(object sender, EventArgs e)
{
if (cboType.SelectedItem.ToString() == "Residential")//if they chose residential then increment resIndex
currentResIndex++;
else
currentCommIndex++;//or else commIndex
currentCommIndex = Math.Min(comList.Length-1, currentCommIndex);
currentResIndex = Math.Min(resList.Length-1, currentResIndex);
updateControls();
}
try
{
if (cboType.SelectedItem.ToString() == "Residential") //if they chose residential then increment resIndex
{
currentResIndex++;
}
else
{
currentCommIndex++;//or else commIndex
}
updateControls();
}
catch (ArgumentOutOfRangeException ex)
{
//Do the things you want when this exception occurs
}
But imho you shouldn't be using this. You should check whether or not the end of the List is reached.
Related
I'm a beginner to C# programming and I really need some help :)
I made 2 buttons that will increment or decrement the value in the text box which is initially zero.
private void bminus_Click(object sender, EventArgs e)
{
NUMBER--;
textBox2.Text = NUMBER.ToString();
}
private void bplus_Click(object sender, EventArgs e)
{
NUMBER++;
textBox2.Text = NUMBER.ToString();
}
Will there be a way that the decrement button will be disabled if the value is 0 so that there won't be negative numbers? Thank you, I'd really appreciate anyone's help! :)
private void bminus_Click(object sender, EventArgs e)
{
NUMBER--;
textBox2.Text = NUMBER.ToString();
if(NUMBER == 0){
bminus.Enabled = false;
}
}
private void bplus_Click(object sender, EventArgs e)
{
NUMBER++;
textBox2.Text = NUMBER.ToString();
bminus.Enabled = true;
}
That should do.
You can just directly insert logical state NUMBER not being 0 to your textBox2 Enabled property:
textBox2.Enabled = NUMBER != 0;
Or make a separate function from this:
private bool IsNotZero(double n)
{
return n != 0;
}
then:
textBox2.Enabled = IsNotZero(NUMBER);
All you need to do is add the following to the textBox2.TextChanged event handler and it should work with your current code.
private void textBox2_TextChanged(object sender, EventArgs e)
{
int output;
if (int.TryParse(textBox2.Text, out output))
{
bminus.Enabled = int.Parse(textBox2.Text) > 0;
}
}
NOTE: No one here has specified checking for an integer so I added int.TryParse and int.Parse for this purpose.
Button have a property named Enabled which you can set to true or false based on your logic.
Check the value using if condition. If the value is null or zero you can use button.enabled = false; code.if(value == zero || value == null) { button.enabled=false;} else {button.enabled = true;}
In my case, I don't want this block:if (Clicked == true){i = i+2;} and if (Clicked == false){i = i-2;}to work if compiler comes inside of different particular button ;
This is Button2 which returns elements of a sequence for a brand name given as
textBox4.
bool Clicked = false;
private void button2_Click(object sender, EventArgs e)
{
var car = cars.FirstOrDefault(c => c.Brand == textBox4.Text);
i = cars.FindIndex(c => c.Brand == textBox4.Text);
if (car != null)
{
label1.Text = car.Brand;
label2.Text = car.Model;
label3.Text = car.Color;
i++;
}
else
{
MessageBox.Show("This car is not in the list");
}
}
And button3_click event should display next elements of the sequence.
private void button3_Click(object sender, EventArgs e)
{
var car = cars.Select(a => a);
if (Clicked == true)
{
i=i+2;
}
if (i >= 0&&i < cars.Count)
{
label1.Text = car.ToArray()[i].Brand;
label2.Text = car.ToArray()[i].Model;
label3.Text = car.ToArray()[i].Color;
i++; //abc; i = 1;
Clicked = false;
}
}
And Button4 displays the previous elements of a sequence
private void button4_Click(object sender, EventArgs e)
{
if (Clicked == false)
{
i=i-2;
Clicked = true;
}
var car = cars.Select(a => a);
if (i >= 0&&i<=cars.Count)
{
label1.Text = car.ToArray()[i].Brand;
label2.Text = car.ToArray()[i].Model;
label3.Text = car.ToArray()[i].Color;
i--;
}
}
I don't want this :if (Clicked == true){i = i+2;} and this :if (Clicked == false) {i=i-2;} block of codes in the button3 and button4 (next and prev buttons) to run whenever I hit any one of them right after the codes inside of button2 have been worked. I couldn't work it out using bool flags as there're three cases needed to be considered. How to deal with it?
I'm going to try to answer this, not 100% sure exactly what you're asking...
My assumption is that you don't want the "if(Clicked..." part to run if you've just previously clicked button2, but after clicking either button3 or button4, it's fine to run the if statement.
Trying to figure out your code, I think you can shrink it way down to remove those. First, the i++ should be removed from button2_Click
private void button2_Click(object sender, EventArgs e)
{
i = cars.FindIndex(c => c.Brand == textBox4.Text);
if(i >= 0)
{
var car = cars.ToArray()[i]; // no reason to search twice
label1.Text = car.Brand;
label2.Text = car.Model;
label3.Text = car.Color;
}
else
{
MessageBox.Show("This car is not in the list");
}
}
Since the index "i" is not changed in button2_Click, it can be changed in the "next" and "previous" buttons before it's indexed.
private void button3_Click(object sender, EventArgs e)
{
// I don't think you need this
// var car = cars.Select(a => a);
if (i + 1 >= 0 && i + 1 < cars.Count)
{
i++;
var car = cars.ToArray()[i];
label1.Text = car.Brand;
label2.Text = car.Model;
label3.Text = car.Color;
}
}
private void button4_Click(object sender, EventArgs e)
{
if (i - 1 >= 0 && i - 1 <= cars.Count)
{
i--;
var car = cars.ToArray()[i];
label1.Text = car.Brand;
label2.Text = car.Model;
label3.Text = car.Color;
}
}
Since both button3_Click and button4_Click are almost identical, you could probably put them in a single event and check which button is pressed and increment/decrement as needed, but that's an opinion call, there are reasons not to do that as well.
I don't know what type "cars" is, but it may even be better to make a member variable to hold cars.ToArray() so ToArray doesn't have to be called so many times.
How do I show a messagebox based on the various SelectedText in the Combobox? It currently just returns a NULL value when running.
I need to show the specific messagebox for each Combobox Text as once I can do this then depending on the SelectedText different SQL Connections will be used and Queries run.
I've included my code below. After some research it seems that the SelectedText control will always return a null value as it loses focus. How do I get around this?
private void button2_Click(object sender, EventArgs e)
{
if(comboSelectServer.SelectedText == "SERV1")
{
MessageBox.Show("SERV1");
}
else if(comboSelectServer.SelectedText == "SERV2")
{
MessageBox.Show("SERV2");
}
else if(comboSelectServer.SelectedText == "SERV3")
{
MessageBox.Show("SERV3");
}
}
Try this.
if (comboSelectServer.Text == "SERV1")
{
MessageBox.Show("SERV1");
}
else if (comboSelectServer.Text == "SERV2")
{
MessageBox.Show("SERV2");
}
else if (comboSelectServer.Text == "SERV3")
{
MessageBox.Show("SERV3");
}
However, this is easier...
if (comboSelectServer.SelectedIndex == 0) //SERV1
{
MessageBox.Show("SERV1");
}
else if (comboSelectServer.SelectedIndex == 1) //SERV2
{
MessageBox.Show("SERV2");
}
else if (comboSelectServer.SelectedIndex == 2) //SERV3
{
MessageBox.Show("SERV3");
}
Try like this
private void button2_Click(object sender, EventArgs e)
{
if(comboSelectServer.SelectedItem.ToString()== "SERV1")
{
MessageBox.Show("SERV1");
}
else if(comboSelectServer.SelectedItem.ToString()== "SERV2")
{
MessageBox.Show("SERV2");
}
else if(comboSelectServer.SelectedItem.ToString()== "SERV3")
{
MessageBox.Show("SERV3");
}
}
Maybe I'm missing something, but why not simply do:
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(comboSelectServer.SelectedItem.ToString());
}
I'm new to C# so still finding my way around.
I have a button I want to enable only when a user enter text to textbox.
I get this error - "Object reference not set to an instance of an object".
Here is the related code (without the using and variables):
public MainWindow()
{
MessageBox.Show("Make sure to edit Settings tab.");
InitializeComponent();
if (startTextBox.Text == "0") // Checks to see if a textbox has some text other than zero. if no than the user cannot press button1 yet.
{
button1.IsEnabled = false;
}
else
{
button1.IsEnabled = true;
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (radioButton1.IsChecked == false)
{
label17.Content = "No Hourly wage was set.";
}
}
private void add(object sender, RoutedEventArgs e) /// here is a very long method so I've removed its content.
}
public void printTime()
{
int Sum = (this.EndInt - this.StartInt);
int Money = (Sum * this.L1001);
label16.Content = Sum;
label17.Content = Money;
if ((textBox1.Text == "0") && ((textBox2.Text == "0") || (textBox3.Text == "0")))
{
label17.Content = "No Hourly wage was set.";
}
}
public void printTime2()
{
int Sum = (this.EndInt - this.StartInt);
MessageBox.Show("Is it possible that you've worked - " + Sum + " Hours?");
}
public void printTime3()
{
int Sum = (this.EndInt - this.StartInt);
int Money = (Sum * this.L1001);
label16.Content = Sum;
label17.Content = Money;
if (textBox1.Text == "0")
{
label17.Content = "No Hourly wage was set.";
}
}
public int Convert(String S)
{
int i = int.Parse(S);
return i;
}
// Input Validation For Excepting Integers Only!
private void input(object sender, TextCompositionEventArgs e)
{ CheckIsNumeric(e); }
private void CheckIsNumeric(TextCompositionEventArgs e)
{
int result; if (!(int.TryParse(e.Text, out result) || e.Text == "."))
{ e.Handled = true; MessageBox.Show("Numbers Only"); }
}
private void startTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
button1.IsEnabled = true;
}
}
}
It's the scope problem. You didn't show where button1 is defined. But inside your event handler startTextBox_TextChanged, button1 definition is nowhere to be found (actually it needs to be instantiated as well). Since you try to invoke a method on an object (button1) which has not been instantiated yet, that exception was thrown.
If you post more than just those snippets, either I or someone else might be able to further help you.
I want to make an error label come up when my flowLayoutPanel is empty, but i don't know how to check that the flowLayoutPanel is empty. This is my current code:
private void flowLayoutPanel1_ControlRemoved(object sender, ControlEventArgs e)
{
if (flowLayoutPanel1.Controls == null)
{
customtoolwarning.Visible = true;
}
else
{
customtoolwarning.Visible = false;
}
}
Please Help,
Thanks
private void flowLayoutPanel1_ControlRemoved(object sender, ControlEventArgs e)
{
if (flowLayoutPanel1.Controls.Count > 0)
{
customtoolwarning.Visible = true;
}
else
{
customtoolwarning.Visible = false;
}
}
The problem you're running into is you're checking Controls for null to determine if it's empty. The Controls property won't ever be null but instead will be non-null and have 0 length when empty. For example
if (flowLayoutPanel1.Controls.Count == 0) {
// It's empty
}
lblNoContacts.Visible = (flowLayoutPanel.Controls.Count == 0) ? true : false;