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());
}
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;}
I want to check the string value for a specified time. I set it. During this time you will press the button to change the value of the string and achieve the condition. If you did not press the button during the specified time you will not check the condition. If the time is over, the condition will not be met . When using this code, the condition is always met :( :
string value;
private void button3_Click(object sender, EventArgs e)
{
IAsyncResult result;
Action action = () =>
{
do //loop to check value through 10s
{
return;
}
while ( value == "");
};
result = action.BeginInvoke(null, null);
if(result.AsyncWaitHandle.WaitOne(10000))
//wait 10s to check response
{
listBox2.Items.Add("good"); // if response string value != ""
}
else
{
listBox2.Items.Add("bad");
// if response string value == "" or timeout
}
}
private void button4_Click(object sender, EventArgs e)
{
value = "best"; // add value
}
This would be my approach to your problem, that do while loop, wont do any good honestly
string value;
private void button3_Click(object sender, EventArgs e)
{
WaitSecondsAndExecute(10);
}
private async void WaitSecondsAndExecute(int seconds)
{
for (int i = 0; i < seconds; i++)
{
await Task.Delay(1000);
if (value != null)
{
break;
}
}
if (value != null)
{
listBox2.Items.Add("good"); // if response string value != ""
}
else
{
listBox2.Items.Add("bad");
// if response string value == "" or timeout
}
}
private void button4_Click(object sender, EventArgs e)
{
value = "best"; // add value
}
if you need it faster its a matter of dividing the delay and multiplying the seconds for the same amount
I have a small program that has several checkedboxlists in VS2010. I wanted to allow a user to select all in one of the lists and came up with this looping structure...
private void CheckedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (e.NewValue == CheckState.Checked)
{
Applications.Add(CheckedListBox1.Items[e.Index].ToString());
}
else if (e.NewValue == CheckState.Unchecked)
{
Applications.Remove(CheckedListBox1.Items[e.Index].ToString());
}
}
private void CheckedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (CheckedListBox1.SelectedIndex == 0)
{
for (int i = 1; i < CheckedListBox1.Items.Count; i++)
{
CheckedListBox1.SetItemChecked(i, CheckedListBox1.GetItemChecked(0));
}
}
else
{
if (!CheckedListBox1.GetItemChecked(CheckedListBox1.SelectedIndex))
{
CheckedListBox1.SetItemChecked(0, false);
}
}
}
The problem is this also puts the "Select All" checkbox into the output. Is there a way I can tweak the loop to not include the first checkbox (which is the "Select All" Check) or should I be going about this a different way?
Pretty unclear what "into the output" might mean. Using the SelectedIndexChanged event isn't very appropriate, the ItemCheck event signals checking an item. Try this instead:
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) {
if (e.Index == 0) {
for (int item = 1; item < checkedListBox1.Items.Count; item++) {
checkedListBox1.SetItemChecked(item, true);
}
e.NewValue = CheckState.Unchecked; // Prevent "Check All" from getting checked
}
}
If you want to use SelectedIndexChanged anyway then still keep this event handler to prevent the item from getting checked.
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.
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;