Open File Dialog box on Clicking Link Label - c#

I've created a log in panel in which I've used Transparent group box(with user name text box and password text box), and used a wallpaper on background, now I've Used a link label on this log in panel by clicking on which the user can change the background wallpaper of the log in panel.
Means when user clicks on the link label (lnklblChangeBackGround) with text "Click Here to Change Background" open dialogue box will open and user can select the Wallpaper from here and then by clicking on OK or Select the wallpaper will be assigned to background of the log in panel
Can any one help me out that
how can i open a open dialogue box by clicking on the link label
how can i assign a select wallpaper to the background of my log in panel
Note: I'm Creating this using VS 2010 using C#. and it's a desktop App and i'm using winform here.

At first you have to add an Event (LinkClicked) to your Link Label.
Just place this code here to open a file dialog.
private String getPicture()
{
string myPic = string.Empty;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "jpg (*.jpg)|*.jpg|png (*.png)|*.png";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
myPic = openFileDialog1.FileName;
return myPic;
}
You can edit the filter to avoid the user choosing images, which is not supported in your opinion.
With this code below you can set the Background Image of your pictureBox
private void setBackground(String picture)
{
pictureBox1.Image = null;
pictureBox1.Image = Image.FromFile(picture);
}
And the final version would look like this
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
String myFile = getPicture();
setBackground(myFile);
}
if this is too much code or too complicated for you, then you can just put it all in one function like this
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
string myPic = string.Empty;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "jpg (*.jpg)|*.jpg|png (*.png)|*.png";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
myPic = openFileDialog1.FileName;
pictureBox1.Image = null;
pictureBox1.Image = Image.FromFile(myPic);
}

Related

How change Mouse pointer in Outlook VSTO

I am trying to upload a file to a remote server using a custom button in outlook ribbon. Following steps are happened
Users click the upload file button in the ribbon and the File picker dialog is seen.
File selection popup will be seen and user select one or more files
After click ok button, I just want to change the default cursor to wait for the cursor
Once file uploading is complete, I tried to restore default curosor
My code is like following
public void UploadFiles(Office.IRibbonControl control)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
Cursor.Current = Cursors.WaitCursor
//some code which will call rest API to upload file
Cursor.Current = Cursors.default
}
}
But above code is not changing curosor. The button is present in compose window of outlook
You need to import Word.DLL and then use the following code
public void UploadFiles(Office.IRibbonControl control)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
Outlook.Inspector currentInspector = Globals.ThisAddIn.Application.ActiveInspector();
Word.Document document = currentInspector.WordEditor;
Word.Application wordApp = document.Application;
wordApp.System.Cursor = Word.WdCursorType.wdCursorWait;
//perform your task
//Switch to default Cursor
wordApp.System.Cursor = Word.WdCursorType.wdCursorNormal;
}
}

How to make UserControl add attachments

How to make user control in Windows application c#
I need make attachments files in form but I need use an user control when click
button browse and choose the files or image add user control in form ?
Add a button on the form and use OpenFileDialog, like that:
private void buttonGetFile_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Text files | *.txt"; // file types, that will be allowed to upload
dialog.Multiselect = false; // allow/deny user to upload more than one file at a time
if (dialog.ShowDialog() == DialogResult.OK) // if user clicked OK
{
String path = dialog.FileName; // get name of file
using (StreamReader reader = new StreamReader(new FileStream(path, FileMode.Open), new UTF8Encoding())) // do anything you want, e.g. read it
{
// ...
}
}
}

C# OpenFileDialog opening twice

I am having a problem with a windows form coded in Visual Studio using C# as part of a web development course. When I click on the menu item ('Import') the file dialogue box opens twice.
The first dialogue seems incorrect as it does not have the filter or title applied. Then the second dialogue box which immediately opens afterward has the correct filter and title applied.
Obviously it is only meant to open once to enable me to read the selected file and add it to the Listbox. This works, but only once I have selected the file twice.
Thanks in advance for your help.
private void importToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
openFileDialog1.Filter = "Text|*.txt";
openFileDialog1.Title = "Choose a file to import";
if (openFileDialog1.ShowDialog() == DialogResult.Cancel)
{
MessageBox.Show("Oh. No file selected!");
}
else
{
string usersFile = openFileDialog1.FileName;
string[] lines = File.ReadAllLines(usersFile);
foreach (string line in lines)
{
groceryList.Items.Add(line);
}
}
Start of your code you have
private void importToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
this is unneccessary, the ShowDialog will show a dialog with the filters not correctly set up as your next lines have:
openFileDialog1.Filter = "Text|*.txt";
openFileDialog1.Title = "Choose a file to import";
Then you ShowDialog again.
Therefore, just remove the first
openFileDialog1.ShowDialog();
and you should be fine.

Enabling textbox on button click event

How can i add the mechanism in my downloader for the case although many threads on SO deals either with php etc and not upto the need.
I have a browse button at the front of a textbox where i get's the user entered Path on local drive to fix the location for downloading but i have already hardcoded one for system drive.I want textbox button to be disabled unless user clciks browse button and then new path can be entered for downloading after then.
How can i go?
A quick example:
private void browseButton_Click(object sender, EventArgs e)
{
var saveFileDialog = new System.Windows.Forms.SaveFileDialog();
var selectedPath= saveFileDialog.ShowDialog();
if (selectedPath == System.Windows.Forms.DialogResult.OK)
{
_savePath = saveFileDialog.FileName;
textBox1.Enabled = true;
textBox1.Text = _savePath;
}
}

c# Openfiledialog

When I open a file using this code
if (ofd.ShowDialog() == DialogResult.OK)
text = File.ReadAllText(ofd.FileName, Encoding.Default);
A window appear and ask me to choose file (The File Name is blank as you can see on the image)
If I press second time the button Open to open a file the File Name show the path of the previous selected file (see on image) How I can clear this path every time he press Open button?
You are probably using the same instance of an OpenFileDialog each time you click the button, which means the previous file name is still stored in the FileName property. You should clear the FileName property before you display the dialog again:
ofd.FileName = String.Empty;
if (ofd.ShowDialog() == DialogResult.OK)
text = File.ReadAllText(ofd.FileName, Encoding.Default);
try this:
ofd.FileName = String.Empty;
You need to reset the filename.
openFileDialog1.FileName= "";
Or
openFileDialog1.FileName= String.Empty()
you can simply add this line before calling ShowDialog():
ofd.FileName = String.Empty;
To clear just the filename (and not the selected path), you can set the property FileName to string.Empty.
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
label1.Text = sender.ToString();
}
What about this one.

Categories