C# Changing Label Text to Listbox Selection Text - c#

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();
}

Related

visual studio C# combo box event

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");
}

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";

How to run a diffrent method each time a new comboboxItem has been selected

Hi there just building a small conversion calculator and Im adding a combo box to it so its not as cluttered and easy to manage. I wont to add a few options to my combo box so that the user has different options to choose from. However I'am going to build a small class with the conversion calculations so that when a different option is selected within the combo box the correct method will be called. I will add a code snippet to show use were i'am at. I was just using the message box just so i know that it was working. Any code snippets would be great.
private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBoxItem kilo = ((sender as ComboBox).SelectedItem as ComboBoxItem);
}
private double workOutKilo()
{
double result = 2;
return result;
}
Assign each ComboBoxItem's Tag control a function, within the SelectionChanged event, call the function.
Yeah if you use a switch than it might be easy to choose combined by the code you already have.
I would suggest something like this:
private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBoxItem kilo = ((sender as ComboBox).SelectedItem as ComboBoxItem);
switch (kilo.ToString())
{
case "Kilo":
//Method();
break;
//...
}
}
I guess this would do the job:
private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox kilo = (sender as ComboBox);
int index = kilo.SelectedIndex;
switch (kilo.ToString())
{
case "0":
//Method();
break;
//...
}
}

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.

.NET WinForms PropertyBinding

I have a WinForm with a menu bar, a menu and a menuItem (called BlaBlub).
the menu item has CheckOnClick = True and (ApplicationSettings)->(PropertyBindings)->Checked mapped to the setting SomeBool (type bool, scope user, initial value= false)
the value is read correctly from the settings-file (i use a label to check it and also the menu item gets selected/deselected when I make changes to the file between sessions).
However, using the following code I was not able to open the application, click on the menu item and store the changed value back into file
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Properties.Settings.Default.Save();
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = string.Format("Value is: {0}", Properties.Settings.Default.SomeBool);
}
I was able to store the value back into file, using the following code, but since this does not seem to be the idiomatic approach, I still seek some enlightment as to how to do this.
private void blaBlubToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
{
Properties.Settings.Default.SomeBool = blaBlubToolStripMenuItem.Checked;
}
You said:
the value is read correctly from the settings-file
However, based on the code presented, that wouldn't be correct because on load you aren't setting the checked state. Instead, I think your testing is showing the initial persisted setting value (being false) is also the default Checked state for the menu item.
Therefore, you should also intialize the control too by adding:
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = string.Format("Value is: {0}", Properties.Settings.Default.SomeBool);
blaBlubToolStripMenuItem.Checked = Properties.Settings.Default.SomeBool;
}
Note: Ordinarily I would tell you to use databinding but you can't because I believe MenuItem's do not support this.

Categories

Resources