C# Forms - Loading images - c#

I'm trying to load a specific image in my picture box if a specific radio button is checked.
The run-time error was a "_Couldn't locate file in this directory.", so I moved the images to that directory, but that just caused the picture box to load it automatically.
I've also imported all of the images I need as well.
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
pictureBox1.Load("10C.jpg");
}
}
When I run the program pictureBox1 had already loaded the image. What I want my pictureBox1 to do is remain blank until the user selects a radio button and clicks the "change image" button.

You should place pictureBox1.Visible = false; in the form load event
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Visible = false;
}

You could make it so that pictureBox1.Visible = false;
Then you could say:
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
pictureBox1.Visible = true;
pictureBox1.Load("10C.jpg");
}
}
This way the image is not seen until the button is clicked.

I would recommend you to set a default image in picture box at startup and if a radiobutton is checked and a button is clicked load the desired image (10C.jpg) into picture box instead of dealing with the visible property of the picture box.
The code will look like:
private void button1_Click(object sender, EventArgs e)
{
// pictureBox1.Visible will be always set to true
if (radioButton1.Checked)
{
pictureBox1.Load("10C.jpg");
}
else
{
pictureBox1.Load("placeholder.jpg");
}
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Visible = true;
pictureBox1.Load("placeholder.jpg");
}
Recommended: search google for placeholder image
Hope it helps!
EDIT
In response of:
The run-time error was a "_Couldn't locate file in this directory.",
so I moved the images to that directory, but that just caused the
picture box to load it automatically. I've also imported all of the
images I need as well.
Define a key in config file that holds an image path and use it to access your images by concatenating filename at the end. With this approach you can change image path even after deployment.

Related

Properties.Settings.Default Doesn't save the values

I making a wpf application but in settings window, I'm changing the text of textboxes listed in view, then I click save
button to update Properties.Settings.Default. Then, I close settings window. I go back to the MainWindow. But when I shutdown the application, in the next session my values does not show up. How could I solve it?
Save Button:
private void save_Click(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.PC_Name = pcname.Text;
Properties.Settings.Default.Acc_name = name.Text;
Properties.Settings.Default.Acc_sname = sname.Text;
}
Loaded Event(Page):
private void ayarlarekrani_Loaded(object sender, RoutedEventArgs e)
{
pcname.Text = Properties.Settings.Default.PC_Name;
name.Text = Properties.Settings.Default.Acc_name;
sname.Text= Properties.Settings.Default.Acc_sname;
}
Looks like you are missing:
Properties.Settings.Default.Save();
http://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/how-to-write-user-settings-at-run-time-with-csharp

Change Path of OpenFileDialog

At the moment I am using this here in my WPF App which works as it should.
private void buttonPresentations_Click(object sender, EventArgs e)
{
openFileDialogPresentations.ShowDialog();
}
It remembers the last path I was in but I want to change it to a set path now.
I have 3 Radiobuttons and each Radiobutton should lead to a different path so I thought about doing it with a variable I give to the openFileDialog but I am not sure on how to go with that. Has anyone done this and can give me a lead on it ?
You can set IntitialDirectory to the folder you want in code where you show the dialog.
private void buttonPresentations_Click(object sender, EventArgs e)
{
openFileDialogPresentations.IntitialDirectory = youFolderPath;
openFileDialogPresentations.ShowDialog();
}
The standard file dialogs have an InitialDirectory property that determines in which folder the dialog opens.
private void buttonPresentations_Click(object sender, EventArgs e)
{
openFileDialogPresentations.InitialDirectory = #"X:\Data\Presentations";
openFileDialogPresentations.ShowDialog();
}
You can do it by using InitialDirectory property. You can set three different paths for radio buttons
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.InitialDirectory=#"D:\MyDir";
dialog.ShowDialog();
}

Using firefox to open c# url button not default browser

I have a very simple program that has 4 buttons each button opens a url.
What i would like is to get each button to use Firefox as the default browser.
I know i can set it as the default browser and then it will work however if a user sets internet explorer as the defualt it will open in that instead.
These url only work in Firefox as it has certificate issues.
So essentially my question is how can i change my code to make sure that if you click on any of the 4 buttons, that it will open up in firefox and not internet explorer.
Here is my code
private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://192.168.2.56/dummy.htm?settings=save&user_active1=on");
BtnOff.BackColor = Color.Red;
Bnton.BackColor = Color.Chartreuse;
}
private void button2_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://192.168.2.56/dummy.htm?settings=save&user_active1=off");
Bnton.BackColor = Color.Red;
BtnOff.BackColor = Color.Chartreuse;
}
private void button3_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("https://217.20.xxx.xxx/api/agent/pause.json?active_host=217.20.xxx.xxx&active_user=3012532&username=buildstore&password=cha9pU7U&client=snom");
BtnDND.BackColor = Color.Chartreuse;
BtnQue.BackColor = Color.Red;
}
private void button4_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("https://217.20.xxx.xxx/api/agent/unpause.json?active_host=217.20.xxx.xxx&active_user=3012532&username=buildstore&password=cha9pU7U&client=snom");
BtnQue.BackColor = Color.Chartreuse;
BtnDND.BackColor = Color.Red;
}
Thanks for taking the time to look appreciate it.
you can use
System.Diagnostics.Process.Start(#"C:\Program Files (x86)\Mozilla Firefox\firefox.exe", "http://yoursite.com/dummy.htm");
you can keep the path of firefox.exe in config file instead of hard coding. Alternatively set the PATH environment variable.

How to disable a button in c# if a value is false

I'm creating a clipboard editing program and I encountered an error when I use the Copy button. If the text box from where it copies to the clipboard's contents are null, then I get a "ArgumentNullException was not handled". I know this is because the TextBox it copies the text from is empty. I want to write a method where if the TextBox is empty, then the button is disabled. Here is the code for this button:
// Copies the text in the text box to the clipboard.
private void copyButton_Click(object sender, EventArgs e)
{
Clipboard.SetText(textClipboard.Text);
}
Any and all help is appreciated. If I'm missing some more details please let me know so I can add them.
You have to initially set the button to be disabled.
Then you can use that code to detect the change in the text box:
private void textClipboard_TextChanged(object sender, EventArgs e)
{
copyButton.Enabled = textClipboard.Text.Length > 0;
}
You should check for null:
// Copies the text in the text box to the clipboard.
private void private void textClipboard_LostFocus(object sender, System.EventArgs e)
{
if(!string.IsNullOrEmpty(textClipboard.Text)
{
Clipboard.SetText(textClipboard.Text);
}
else
{
copyButton.Enabled = false; //Set to disabled
}
}
You could initially set the button.enabled to false, and add a KeyUp event to your textbox:
private void textClipboard_KeyUp(object sender, KeyEventArgs e)
{
copyButton.Enabled = !string.IsNullOrEmpty(textBox1.Text);
}

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

Categories