My labels text wont change on loading form - c#

I have tried to change my labels text in both the form_load and form_shown methods and nothing happens. I don't want to set the text in the properties as I want to use the text in another label later. Ive tried both these methods neither works.
private void History_Load(object sender, EventArgs e)
{
populateHistoryQuestionArray();
historyQ1.Text = historyQuestion[0];
}
and
private void History_Shown(object sender, EventArgs e)
{
populateHistoryQuestionArray();
historyQ1.Text = historyQuestion[0];
}

You must add ToString()
private void History_Load(object sender, EventArgs e)
{
populateHistoryQuestionArray();
historyQ1.Text = historyQuestion[0].ToString();
}

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?

Remeber Me C# app setting

I Create login form and want to put "remember me" check box on it.
But every time i open program it doesn't change.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
Project.Properties.Settings.Default.rememberMe = true;
Project.Properties.Settings.Default.Save();
}
else
{
Project.Properties.Settings.Default.rememberMe = false;
Project.Properties.Settings.Default.Save();
}
}
Also i want to save user login information, should i save them in app setting just like remember me setting or there is better way?
You're saving the settings, but you need to retrieve those settings too.
Subscribe to the Form's load event and set the value of the CheckBox.
private void Form1_Load(object sender, EventArgs e)
{
checkBox1.Checked = Project.Properties.Settings.Default.rememberMe;
}
Also, and this is just common practice, but your code could be shorter:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
Project.Properties.Settings.Default.rememberMe = checkBox1.Checked;
Project.Properties.Settings.Default.Save();
}

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

Passing text from one textBox to another in a same form using C# Windows Form

I'm trying to do something very simple ( at least I do think so). I have a Form with a Split Container and each part (they are two at all) has only a textBox and the idea is while I'm writing at textBox1 to visualize this text at textBox2. I figure out how to pass some data from the one box to the other but I don't know how to get the value of the textBox and pass it to the other. An again I want to mention that the 2 textBoxes are in one form, so it should be a pretty easy task I suppose.
Here is my code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
//how to get the text from textBox1?
textBox2.AppendText("I want to pass the text form textBox1");
}
}
And also, I'm using Visual Studio 2010.
Is it as simple as:-
textBox2.Text = textBox1.Text;
?
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox2.Text = textBox1.Text;
}
try this
`private void textBox1_TextChanged(object sender, EventArgs e)
{
//how to get the text from textBox1?
textBox2 = textBox1.Text;
}`
If you just want to get the text from textbox1
use this:
private void textBox1_TextChanged(object sender, EventArgs e)
{
//how to get the text from textBox1?
textBox2 = textBox1.Text;
}
but if you want to add the text from textbox1 to textbox2
use this:
private void textBox1_TextChanged(object sender, EventArgs e)
{
//how to get the text from textBox1?
textBox2.AppendText(textbox1.text);
}

Expand collpase splitcontainer in win form c#

i am working with split container. my split container has two panel and horizontal orientation. in first panel there are some textboxes and one button. when i click on button then a code run to collapse Panel1 of split container. code is like
private void button1_Click(object sender, EventArgs e)
{
splitContainer1.Panel1Collapsed = !splitContainer1.Panel1Collapsed;
}
my problem is when collapse occur then my button and all the textboxes getting invisible. so i next time not being able to make those control visible. so i want trick like button will not be invisible as a result i can click on that button again to make panel1 visible. if possible guide me how to fix or place my button on splitter rather on panel. so guide me how can i do it.
private void button1_Click(object sender, EventArgs e)
{
splitContainer1.Panel1Collapsed = !splitContainer1.Panel1Collapsed;
button1.Parent = splitContainer1.Panel1Collapsed ? splitContainer1.Panel2 : splitContainer1.Panel1;
}
Related to my previous comment on your entire posting. this is a small solution with a ToolBarButton. It will only be enabled if the SplitContainer.Panel1 is collapsed.
Code:
private void Form1_Load(object sender, EventArgs e)
{
splitContainer1.Panel1Collapsed = true;
toolStripButton1.Enabled = true;
}
private void button1_Click(object sender, EventArgs e)
{
splitContainer1.Panel1.Hide();
toolStripButton1.Enabled = true;
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
if (splitContainer1.Panel1Collapsed)
{
toolStripButton1.Enabled = false;
splitContainer1.Panel1.Show();
}
}

Categories

Resources