visual studio C# combo box event - c#

private void droplesson_SelectedIndexChanged(object sender, EventArgs e)
{
if(e.Equals("LESSON1"));
reload("LESSON1.txt");
if(e.Equals("LESSSON2"));
reload("LESSON2.txt");
if(e.Equals("LESSON3"));
reload("LESSON3.txt");
if(e.Equals("LESSON4"));
reload("LESSON4.txt");
if (e.Equals("LESSON5"));
reload("LESSON5.txt");
}
Above code is not working. I want to change the dropdown menu such that when i select the particular lesson it reload that lesson.enter image description here

You added ';' at the end of every line, including the 'if' lines, soo all of the 'reload' calls got executed...
This is how your code should look:
private void droplesson_SelectedIndexChanged(object sender, EventArgs e)
{
if(e.Equals("LESSON1"))
reload("LESSON1.txt");
if(e.Equals("LESSSON2"))
reload("LESSON2.txt");
if(e.Equals("LESSON3"))
reload("LESSON3.txt");
if(e.Equals("LESSON4"))
reload("LESSON4.txt");
if (e.Equals("LESSON5"))
reload("LESSON5.txt");
}

Related

Bring custom control to front

I recently started to learn C# and right now I want to mess arouwnd with the Form[Design].
Right now I'm trying to BringToFront() custom controls each time I hover over a button (Ive got a few buttons close to each other, each time I hover over them I get a certain User Control).
This is what I've got so far:
private void button1_Hover(object sender, EventArgs e)
{
costumControl1.BringToFront();
}
private void button1_Leave(object sender, EventArgs e)
{
CostumControl0.BringToFront();
}
private void button2_Hover(object sender, EventArgs e)
{
costumControl2.BringToFront();
}
private void button2_Leave(object sender, EventArgs e)
{
CostumControl0.BringToFront();
}
private void button3_Hover(object sender, EventArgs e)
{
costumControl3.BringToFront();
}
private void button3_Leave(object sender, EventArgs e)
{
CostumControl0.BringToFront();
}
But bringing to front the user control costumControl0 when I hover from a button to another it's not what I want.
But I don't know how to go about this.
Is it possible to just add a if statement where I check if I'm not hovering the buttons close to my current one and then display the costumControl0.
Or a timer is necessary to delay the display of the costumControl0 and skip the command if I'm starting another event.
If the timer is needed, can I use one timer for all of the buttons or do I need to create one for each?
Or whats the best approach for this?

The function of buttons is removed when moving to group box?

At the beginning, I had 5 buttons in a panel and they worked perfectly.
For example,
private void btnFlipX_Click(object sender, EventArgs e)
{
imgBox.Image.RotateFlip(RotateFlipType.Rotate180FlipX);
imgBox.Refresh();
}
However, when I moved them to groupbox, they did not work anything. I clicked on a button in groupbox and there was anything in there (there is no code in them). For example,
private void btnFlipX_Click_1(object sender, EventArgs e)
{
}
Anyone can give me the reason of this problem?
It looks like VS just added a new event method for your automatically and named it btnFlipX_Click_1 instead of btnFlipX_Click; Are you sure you are assigning the right event (Where the += is)

C# Changing Label Text to Listbox Selection Text

So i have been browsing around the great mind of Google but have not found a working solution to this; I have a listbox (instanceSelection) and a label (instanceTxt). I want the instanceTxt to be the same as the instanceSelection's text when I select an item in the collection.
Here is the code line that I thought would work earlier:
private void instanceSelection_SelectedIndexChanged(object sender, EventArgs e)
{
instanceTxt.Text = (string)this.instanceSelection.SelectedValue.Text;
}
But in one time it didn't change, and another code block it changed to "0". I also sometimes get a null error when using "ToString".
Thanks,
William
Try this:
private void instanceSelection_SelectedIndexChanged(object sender, EventArgs e)
{
if(instanceSelection.SelectedIndex > -1)
instanceTxt.Text = instanceSelection.Items[instanceSelection.SelectedIndex].ToString();
}

How can I make a pictureBox that, when clicked, will display some text to a label?

I'm using Visual Studio, so I already have drag-and-dropped some pictureBoxes in there, and they have names and pictures. I just want to be able to click them and have a label say the name of the person in the picture that was clicked.
This is what I currently have (for the pictureBox only)
private void captainFalcon_Click(object sender, EventArgs e)
{
Label.Show("Captain Falcon");
}
FINAL EDIT: Everything is working now, I just followed everyone's suggestions!
Is this what you need?
private void captainFalcon_Click(object sender, EventArgs e)
{
Label.Text = "Captain Falcon";
}
Something like below will work.
private void pictureBox1_Click(object sender, EventArgs e)
{
label1.Text = "Captain Falcon";
}
Obviously change the control names (pictureBox1 and label1) to match yours.
Use
private void captainFalcon_Click(object sender, EventArgs e)
{
xxxxxx.Text ="Captain Falcon";
}
and instead of xxxxx use your label name.
Select your label on designer, look at its properties, and use its "(Name)" property.
For example if your label name is characterName, then your code will be
characterName.Text ="Captain Falcon";

My groupbox is not showing

I have created many groupbox staked one on top of the other and made them to be invisible however ,if i call the first group box it shows but if i call the other groupboxes they will not show
private void buttonFinish_Click(object sender, EventArgs e)
{
groupBox1.Show();// is showing
}
private void buttonFinish_Click(object sender, EventArgs e)
{
groupBox2.Show();// is not showing
}
The second related problem is that if i try an if statement it the selected if statement shows nothing at all
private void buttonFinish_Click(object sender, EventArgs e)
{
if (comboBoxType.Text == "Car" && comboBoxName.Text == "BMW"
" && radioButtonBlack.Checked){
if (checkBoxTwoseater.Checked || radioButtonLeather.Checked ||
radioButtonBooster.Checked ){
groupBox1.show}
I cannot insert an image because i am new
Well, you said they are all on top of each other, so what is likely happening is the second call works just fine, but it is actually appearing behind the first one.
What you need to write is:
private void buttonFinish_Click(object sender, EventArgs e)
{
groupBox1.Show();// is showing
}
private void buttonFinish2_Click(object sender, EventArgs e)
{
groupBox1.Hide();
groupBox2.Show();// is showing now!
}
Note that this code is odd, since both methods appear to be named the same. I changed that to make "compilable" code, but you should check yours to make sure that it isn't causing a problem.
Show just sets the Visible property to true, it does not affect the Z-Order (see MSDN)
The second problem will require you stepping through the code with a debugger and checking all your conditions, you haven't given enough information for us to help.

Categories

Resources