When user changes background color for example, the Settings.settings file is modified. And it works.
But the application doesn't change it's background color after user clicks OK.
It works only when I close and build the application again.
How can I reload my form or user control on button click? (Tried with .Refresh(), but it doesn't work)
private void refreshSettings()
{
this.BackColor = Properties.Settings.Default.bgdColor;
this.Font = Properties.Settings.Default.fontType;
this.ForeColor = Properties.Settings.Default.fontColor;
}
private void Settings_Load(object sender, EventArgs e)
{
refreshSettings();
bgdColorLBL.BackColor = Properties.Settings.Default.bgdColor;
fontColorLBL.BackColor = Properties.Settings.Default.fontColor;
fontTypeLBL.Font = Properties.Settings.Default.fontType;
fontTypeLBL.Text = Properties.Settings.Default.fontType.Name;
}
private void okBTN_Click(object sender, EventArgs e)
{
LeagueUC lg = new LeagueUC();
InitializeComponent();
this.Close();
}
private void bgdColorLBL_Click(object sender, EventArgs e)
{
ColorDialog dlg = new ColorDialog();
dlg.Color = Properties.Settings.Default.bgdColor;
if (dlg.ShowDialog() == DialogResult.OK)
{
Properties.Settings.Default.bgdColor = dlg.Color;
Properties.Settings.Default.Save();
bgdColorLBL.BackColor = dlg.Color;
}
}
Run whatever code you have that sets the control's properties at start up from the settings file.
e.g.
private void bgdColorLBL_Click(object sender, EventArgs e)
{
ColorDialog dlg = new ColorDialog();
dlg.Color = Properties.Settings.Default.bgdColor;
if (dlg.ShowDialog() == DialogResult.OK)
{
Properties.Settings.Default.bgdColor = dlg.Color;
Properties.Settings.Default.Save();
Settings_Load(null, null);
}
}
On the button click event, just load the backcolor from your settings file. Something like:
this.BackColor = Properties.Settings.Default.Color;
You can create binding for it. With a little tricks the binding can even allow the immediate interface language switching.
try this, this changes background color of form in color that you chose from ColorDialog:
private void button2_Click(object sender, EventArgs e)
{
ColorDialog dlg = new ColorDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
this.BackColor = System.Drawing.Color.FromName(dlg.Color.Name);
}
}
Related
I recently started working on an application with windows forms using C#. To navigate through the different forms I am using:
Form Form_name = new Form ();
Form_name .TopLevel = false;
panel1.Controls.Clear();
panel1.Controls.Add(Form_name);
Form_name.Show();
The current setup is that I have the main menu form where you then navigate to a upload photo form. But after uploading an image in the upload photo I want to show the next form. But because we now are working in another form we cant use:
panel1.Controls.Clear();
panel1.Controls.Add(Form_name);
Because panel1 is referenced in the first form. I tried to create a public class to clear it but had no success.
My questions come down to:
Is there a better method to navigate through different forms/windows?
Can we create a public function to clear the panel in the first form?
MainMenu.cs
private void mainMenu1_Click(object sender, EventArgs e)
{
ME_Upload ME_UploadConstruct = new ME_Upload();
ME_UploadConstruct.TopLevel = false;
panel1.Controls.Clear();
panel1.Controls.Add(ME_UploadConstruct);
ME_UploadConstruct.Show();
}
private void mainMenu2_Click(object sender, EventArgs e)
{
IndividualEditor individualEditor = new IndividualEditor();
individualEditor.TopLevel = false;
panel1.Controls.Clear();
panel1.Controls.Add(individualEditor);
individualEditor.Show();
}
}
ME_Upload.cs
private void ME_UploadButton_Click(object sender, EventArgs e)
{
OpenFileDialog opf = new OpenFileDialog();
opf.Filter = "Choose Image(*.jpg; *.png; *.gif)|*.jpg; *.png; *.gif";
if (opf.ShowDialog() == DialogResult.OK)
{
ME_ImageEditor ME_ImageEditorConstruct = new ME_ImageEditor();
ME_ImageEditorConstruct.TopLevel = false;
panel1.Controls.Clear(); ///Not Working
panel1.Controls.Add(ME_ImageEditorConstruct); ///Not Working
ME_ImageEditorConstruct.Show();
}
}
I'm not sure what to title this, so that's why I used quotations. Bare in mind, I'm very new to C#.
I'm just creating a basic Notepad in C# VS.
using System;
using System.IO;
using System.Windows.Forms;
namespace Notepad_Project
{
public partial class notepadMain : Form
{
public notepadMain()
{
InitializeComponent();
}
private RichTextBox GetRichTextBox()
{
RichTextBox rtb = null; // initialising null
TabPage tp = tabControl1.SelectedTab;
if (tp!=null)
{
rtb = tp.Controls[0] as RichTextBox;
}
return rtb;
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
TabPage tp = new TabPage("New Document"); // Allows for new tab creation
RichTextBox rtb = new RichTextBox(); // Allows for new richtext box object
rtb.Dock = DockStyle.Fill; // Applys dock with RTB
tp.Controls.Add(rtb); // Tabs RTB
tabControl1.TabPages.Add(tp); // Add tabs to tab control
}
private void cutToolStripMenuItem_Click(object sender, EventArgs e)
{
GetRichTextBox().Cut();
}
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
GetRichTextBox().Copy();
}
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
GetRichTextBox().Paste();
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
Stream myStream;
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
if((myStream=openFileDialog1.OpenFile())!=null) {
string filename = openFileDialog1.FileName;
string readfiletext = File.ReadAllText(filename);
TabPage tp = new TabPage("New Document");
RichTextBox rtb = new RichTextBox();
rtb.Dock = DockStyle.Fill;
tp.Controls.Add(rtb);
tabControl1.TabPages.Add(tp);
rtb.Text = readfiletext;
}
}
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog savefile = new SaveFileDialog();
RichTextBox rtb = new RichTextBox();
savefile.Filter = "Plain Text (.txt)|*.txt|Batch File (.bat)|*.bat|Visual Studio (.vbs)|*.vbs";
if (savefile.ShowDialog() == DialogResult.OK) {
rtb.SaveFile(savefile.FileName, RichTextBoxStreamType.PlainText);
}
}
}
}
I basically, in short want to use RichTextBox rtb = New RichTextbox(); once and be able to declare it everywhere else, like a global variable. I've seen that you can use classes for this, but I'm unsure on how to implement it this way. If you notice, you can see that I have to declare RichTextBox rtb = New RichTextBox(); everytime I add a new function or so on.
If I was to remove the RichTextBox rtb = New RichTextbox(); from this section:
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog savefile = new SaveFileDialog();
RichTextBox rtb = new RichTextBox();
savefile.Filter = "Plain Text (.txt)|*.txt|Batch File (.bat)|*.bat|Visual Studio (.vbs)|*.vbs";
if (savefile.ShowDialog() == DialogResult.OK) {
rtb.SaveFile(savefile.FileName, RichTextBoxStreamType.PlainText);
}
}
I will then obtain this error too: The name 'rtb' does not exist in the current context
So, how would I implement and use this globally in C#? This is also so I can learn how to shorten certain things and keep it clean, efficient and tidy.
You creating controls dynamically so in your case you don't need to remove that line. You need to GET active tab rich text control and you have method for that.
Of course you can replace GetRichTextBox() method with property like this:
private RichTextBox ActiveRichTextBox
{
get
{
RichTextBox rtb = null; // initialising null
TabPage tp = tabControl1.SelectedTab;
if (tp!=null)
{
rtb = tp.Controls[0] as RichTextBox;
}
return rtb;
}
}
and use it in your code like this:
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog savefile = new SaveFileDialog();
savefile.Filter = "Plain Text (.txt)|*.txt|Batch File (.bat)|*.bat|Visual Studio (.vbs)|*.vbs";
if (savefile.ShowDialog() == DialogResult.OK) {
ActiveRichTextBox.SaveFile(savefile.FileName, RichTextBoxStreamType.PlainText);
}
}
I
open an Image i tried this methode but when i click on the button the window doesn't appear
so i cant load an image
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
if (open.ShowDialog() == DialogResult.OK)
{
pictureBox1.ImageLocation = open.FileName;
}
Do this it will work perfectly and display full image.
private void button1_Click_1(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
if (open.ShowDialog() == DialogResult.OK)
{
pictureBox1.ImageLocation = open.FileName;
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
}
}
There are "Open" and "Cancel" Buttons on OpenfileDialog. So because there is no "OK" button on it, "if condition" will not be true. You should change it to:
Private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
if (open.ShowDialog() != DialogResult.Cancel)
{
pictureBox1.ImageLocation = open.FileName;
}
}
I'm using WinForms. I have a button in this form. When the button is clicked I want the program to print a specific file every time i click on it.
My Problem is: My code does not print the specific file in my computer. It prints out a blank document.
Example: Button click prints document "C:\image.jpg" all the time.
private void btn_Print_Click (object sender, EventArgs e)
{
printDialog1.AllowSomePages = true;
printDialog1.AllowSelection = true;
printDocument1.PrinterSettings.PrintToFile = true;
if (printDialog1.ShowDialog() == DialogResult.OK)
{
printDialog1.PrinterSettings.PrintFileName = #"C:\\image";
printDocument1.Print();
}
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
}
The following code works. You may need to adjust it as needed, but this would be your starting point.
Reference: https://social.msdn.microsoft.com/Forums/windows/en-US/f83aff78-44c1-465c-86b4-9e4e5a2d97e2/how-to-print-files-stored-at-your-local-hard-drive-in-c?forum=winforms
Make sure you have:
using System.Drawing;
using System.Drawing.Printing;
here it is:
private void button1_Click(object sender, EventArgs e)
{
using (PrintDocument pd = new PrintDocument())
{
if (printDialog1.ShowDialog() == DialogResult.OK)
{
string filePath = #"C:\image.JPG";
pd.OriginAtMargins = true;
pd.PrintPage += pd_PrintPage;
pd.DocumentName = filePath;
pd.Print();
pd.PrintPage -= pd_PrintPage;
}
}
}
public void pd_PrintPage(object sender, PrintPageEventArgs e)
{
string labelPath = ((PrintDocument)sender).DocumentName;
e.Graphics.DrawImage(new Bitmap(labelPath), 0, 0);
}
I have a windows form application I have a button that opens the SaveFileFrom dialog
private void button1_Click(object sender, EventArgs e)
{
SaveFileDialog savefileDialog1 = new SaveFileDialog();
savefileDialog1.ShowDialog();
}
I was wondering how I could put the file that is chosen in a text box even like so
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
UPDATE* Ok, well since the user might want to open more than one file to save I wanted to take off my button SaveFileFrom and instead make the textbox run through the OpenFileDialog when clicked.
Also, is their a way to make a text link instead of a button?
Like I want a text link to add another text box/
Try this
private void button1_Click(object sender, EventArgs e)
{
SaveFileDialog savefileDialog1 = new SaveFileDialog();
if (savefileDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = savefileDialog1.Filename;
}
}
The filename selected will be saved in SaveFileDialog.FileName
example:
private void button1_Click(object sender, EventArgs e)
{
SaveFileDialog SvDlg = new SaveFileDialog();
if (SvDlg.ShowDialog() == DialogResult.OK)
{
textBox1.Text = SvDlg.FileName;
}
else
{
MessageBox.Show("No file selected.");
}
}