Display picture only if textbox is blank - c#

My Program: I have a textBox and a pictureBox (which contains an error picture, placed exactly next to textBox) in userControl in my program.
My Goal: I want to HIDE the picture in the pictureBox only if the user types text in the textBox. If the textBox is left blank, the image in the pictureBox should be shown.
I tried using errorProvider but I was totally lost because I am a newbie in C# Programming. There are many errorProvider examples online but all examples are using Form and I am trying to do it in UserControl. So, I thought I should try this method. Please can you help me with the code? Thanks for your help in advance.
ANSWER:
Sealz answer works! My program will be working offline. So, this one also works:
if (String.IsNullOrEmpty(textBox1.Text))
{
//Show Picture
pictureBox2.Visible = true;
}
else
{
//Hide Picture
pictureBox2.Visible = false;
}
Thanks everybody for looking at my question! You all are awesome. =)

You can use IsNullOrEmpty
if (String.IsNullOrEmpty(textBox1.Text))
{
//Show Picture
pictureBox1.ImageLocation = "locationofimg";
}
else
{
//Hide Picture
pictureBox1.ImageLocation = "";
}
To get fancy with it.
On form_Load() set the picturebox to nothing
private void Form1_Load(object sender, EventArgs e) {
pictureBox1.ImageLocation = "";
}
Then in the Textbox Change Method
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(textBox1.Text))
{
pictureBox1.ImageLocation = "";
}
else
{
pictureBox1.ImageLocation = "Image\Location.com.etc";
}
}
This will make the box empty to start with no image and as you type it will pop up. If the boxes text is deleted fully the image will vanish.

Just test if the textbox has any text, and set the property accordingly.
pictureBox1.ImageLocation = (textBox1.Text.Length > 0) ?
"imagefile" : String.Empty;
If this needs to update dynamically, just perform this action in the textbox's TextChanged event.

Related

C# Windows Form Application, button name with readline

I want to create button that is named by the user.
This is what I have (sorry I'm so bad at this but I am trying :D)
string 1;
private void button1_Click(object sender, EventArgs e)
{
if (1 = null)
{
Console.Write("Give Button's name");
1= Console.ReadLine();
button1.Name = 1;
}
}
I also want the button to open a file path selected by the user. Is there a way to do that?
(sorry for my stupid question and anything else... :D)
Based on yr comments:
1. Use a TextBox(called txtUserInput) to get the user's input
2. Use OpenFileDialog to help user to choose a file
private void button1_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(txtUserInput.Text))
{
button1.Text = txtUserInput.Text;
var openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
MessageBox.Show("You chose: " + openFileDialog.FileName);
}
}
}
You can't mix Console Application input with Windows Forms input. You need to decide what you want. From the comments, it looks like you want a Windows Form Application so you will need some way to get user input like a text box.
On your form you will need to create a text box and give it a name like tbxUserInput. After that you can change you click method to do something like this:
private void button1_Click(object sender, EventArgs e)
{
button1.Text = tbxUserInput.Text
}
One thing that was wrong was that you were assigning the Name property of the button and not the Text property. The text property is what is actually displayed on the button.
It may be a good idea to read some tutorials about Windows Forms.

If PictureBox Clicked Event

I'm trying to program a matching game. My idea is:
(1). When a certain pictureBox1 is clicked, it becomes invisible
(2). A MessageBox shows up, prompting "Pick another box."
(3). Finally, I need to program an if/else statement where if pictureBox13 is clicked it becomes invisible; else, (if another pictureBox is clicked) a MessageBox prompts "Try again." And both picture boxes become invisible, but I don't know what I am doing wrong:
// Program From Below
private void pictureBox1_Click(object sender, EventArgs e)
{
MessageBox.Show("Now Pick Another Hidden Picture!");
pictureBox1.Visible = false;
if (pictureBox13_Click)
{
MessageBox.Show("Great!");
pictureBox13.Visible = false;
}
else
{
MessageBox.Show("Try Again!");
}
}
There is a red squiggly line under if (pictureBox13_Click)
It would be better if every PictureBox had it's a state, that you would then manipulate using a Click_Event. Microsoft has a comprehensive tutorial for a matching game here: https://msdn.microsoft.com/en-us/library/dd553235.aspx
As other suggested, you can use same event handler for all your pictureBoxes and cast sender to PictureBox to see what PB was clicked :
List<string> selectedPictureBoxes;
public MyForm() // ctor
{
selectedPictureBoxes = new List<string>();
foreach(Control c in this.Controls)
if(c is PictureBox) c.Click += pictureBox_Click;
}
private void pictureBox_Click(object sender, EventArgs e)
{
PictureBox _clicked = sender as PictureBox;
if(!selectedPictureBoxes.Contains(_clicked.Name))
selectedPictureBoxes.Add(_clicked.Name);
else ....
}
You could create an int for the selected boxes (in this example, box1 and box2) which are both set to 0 and then create an on click event which sets the int to the clicked box.
if(box1 != 0)
{
box2 = 'insert selected box number'
}
else
{
box1 = 'insert selected box number'
}
After two boxes have been selected, both integers can be set to false, this allows for you to use a switch instead of if, which could shorten the code substantially if a separate if statement is required for each pair of pictures.

TextBox shown in the wrong place after a scroll down

i have a from c# and i want to show text Box after a click in a check box.
but when i scroll down, and check , the text Box is shown in the wrong place !!!
the text Boxes must be in the same level with check Boxes.
private void checkBox1_Checkedchanged(object sender, EventArgs e)
{
textBox1.Visible = true;
}
and changing the Location of the text Box don't give good results !
thanks for help.
You are running into an awkward quirk of the Panel control, it only scrolls controls that are visible. When you make it visible in your code, it will have the wrong Location property if you've used the scrollbar. You will need to make the correction yourself. Make it look like this:
private void checkBox1_Checkedchanged(object sender, EventArgs e)
{
if (!textBox1.Visible) {
textBox1.Location = new Point(textBox1.Left + panel1.AutoScrollPosition.X,
textBox1.Top + panel1.AutoScrollPosition.Y);
textBox1.Visible = true;
}
}
A better alternative is to use the Enabled property instead, also much less disorienting for the user. Set it to False in the designer and then:
private void checkBox1_Checkedchanged(object sender, EventArgs e)
{
textBox1.Enabled = true;
}

How to expand the picturebox into another form?

Currently, my application displays 6 picture boxes, each displaying an picture which is being constantly updating.
Now, I want upon clicking any picture box that picture box extends and fill up the whole screen just showing that chosen picture box.
Is this possible? Must i create another form to do this?
Thanks In Advance,
Perumal
in the onclick event for each the picture box (they can all point to this same method)
picturebox_Click(object sender .....)
{
PictureBox pb= (PictureBox)sender;
if (pb.dock==DockStyle.None)
{
pb.dock=DockStyle.Fill;
pb.BringToFront();
}
else
pb.dock=DockStyle.None;
}
Not seeing any code, here is how you can programmatically change a picture box on click.
pictureBox1.Dock = DockStyle.Fill
So you need to create a on click event handler and call your picture box's Dock function like the above.
update in response to comments
There is a DockStyle.None to revert the picture back to original size.
If i understand you correctly, you want to have 6 pictures and then when you click one it fills, click again, shrinks, click another one, fills etc etc...
To do this, you would use the Dock and Visible properties on the picture boxes. Now it also seems as if you are asking how to actually write the code. Well if you show some code, I could give pointers, with nothing to go on the way I'd approach it is to:
Put all your picture boxes in a list and assign a state to them Big or Small.
Write a OnClick for each picture box to change the state of the picture box clicked on.
Each OnClick then calls a helper function that iterates through each picture box in the list and hides the small one and DockStyle.Fill the big one.
Does the above algorithm accomplish what you need?
try something like this. the code is not re factored but I am sure you can do that
private bool isfill = false;
private void pictureBox1_Click(object sender, EventArgs e)
{
if (!isfill)
{
pictureBox1.Dock = DockStyle.Fill;
pictureBox2.Visible = false;
isfill = true;
}
else
{
pictureBox1.Dock = DockStyle.None;
pictureBox2.Visible = true;
isfill = false;
}
}
private void pictureBox2_Click(object sender, EventArgs e)
{
if (!isfill)
{
pictureBox2.Dock = DockStyle.Fill;
isfill = true;
pictureBox1.Visible = false;
}
else
{
pictureBox2.Dock = DockStyle.None;
isfill = false;
pictureBox1.Visible = true;
}

Control the position of the vertical scrollbars

I have a multi-line textbox (let's call it textBox1) that has plenty of text inside.
After doing a search, I highlight the string I was looking for with:
textBox1.SelectionStart = textBox1.Text.ToLower().IndexOf(STRING);
textBox1.SelectionLength = STRING.Length;
Now when I call the form that contains the textbox it highlights the selected text, but what I would like to do is that the scrollbars would scroll automatically to the highlighted text.
I tried
textBox1.ScrollToCaret();
But didn't work.
Any ideas?
Thank you.
What event are you firing this from? The Form probably isn't in a state where it can process this. If you call from Form.Load it will be too soon. If you call from Form.Shown, it should work properly.
private void Form1_Shown(object sender, EventArgs e) {
var STRING = "Suspendisse mi risus";
textBox1.SelectionStart = textBox1.Text.IndexOf(STRING);
textBox1.SelectionLength = STRING.Length;
textBox1.ScrollToCaret();
}

Categories