Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Basically I was attempting to use an open file dialog and a list box to list recently opened files in a list box also yes I do use fastcoloredtextbox why do 90% of people I talk to hate it also sorry if the code looks like it was designed by some depressed 11 year old because I am very new.
private void button5_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
string fm = openFileDialog1.FileName;
string rf = File.ReadAllText(fm);
fastColoredTextBox.Text = rf;
string files = Path.GetFileName(fm);
foreach (string file in files)
{
listBox1.Items.Add(file);
}
}
Also it looks unfinished because it is unfinished - I've been trying to fix this and it just kept making more errors so like yea uhh help random people on the internet who I don't know uhh I'm going to stop randomly typing things now
wow edit #3 if it helps anyone I'm having the error on
foreach (string file in files)
First you have to add a new Instance of var openFileDialog = new OpenFileDialog(); if you don't have. Then you could do something like this to get the text of the file and add it to you listbox:
private void button5_Click(object sender, EventArgs e)
{
var openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == true)
{
var file = openFileDialog.FileName;
var text = File.ReadAllText(file);
fastColoredTextBox.Text = text;
var filename = Path.GetFileName(file);
listBox1.Items.Add(filename);
}
}
Also you do not have to do this:
foreach (string file in files)
because Path.GetFile() returns only a string with your FileName. So you can add it directly to your ListBox.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
I want to make some app and I want the button that when I click i can browse images from file on my computer and then change that image to the form background
I have tried many of code but still can't find one
private void ChangeBackgroundImage_Click(object sender, EventArgs e)
{
//how can I open the browse dialog and choose image file
//and after that, change that image to form backgroundimage?
}
*Im new to this
First, create a button. You will have to use OpenFileDialog to get the file path, after that you can configure the button click by lambda
this.BackgroundImageChanged += new EventHandler((object sender1, EventArgs e1) =>
{
//if the background image has changed
});
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "images|*.png;*.jpeg;*.jpg";
// You can Use "|All Files|*.*" and other to filter the files.
if (dialog.ShowDialog() == DialogResult.OK) //if path picked
{
button1.Click += new EventHandler((object sender1, EventArgs e1) =>
{
string PathFile = dialog.FileName;
this.BackgroundImage = Image.FromFile(PathFile);
});
}
else
{
//if path not picked
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm sure this is really simple to do but I'm struggling.
I've 6 Textboxes and I would like to copy them in Clipboard and paste them to others.
So to copy I do:
private void Btn_Copy_P1_Click(object sender, EventArgs e)
{
StringBuilder cop = new StringBuilder();
cop.AppendLine(X_Point_1_Rob.Text);
cop.AppendLine(Y_Point_1_Rob.Text);
cop.AppendLine(Z_Point_1_Rob.Text);
cop.AppendLine(Rx_Point_1_Rob.Text);
cop.AppendLine(Ry_Point_1_Rob.Text);
cop.AppendLine(Rz_Point_1_Rob.Text);
Clipboard.SetText(sb.ToString());
}
private void Btn_Paste_P2_Click(object sender, EventArgs e)
{
Clipboard.GetText()
// How it's possible to paste the 6 values from Clipboard ?
}
Do you have a working method or a solution?
Something like this, maybe?
private void Btn_Paste_P2_Click(object sender, EventArgs e)
{
string[] text = Clipboard.GetText()?.Split('\\n');
if(text == null || text.Length == 0)
return;
X_Point_1_Rob.Text = text[0];
Y_Point_1_Rob.Text = text[1];
...
Rz_Point_1_Rob.Text = text[5];
}
By the way, where do you want to paste copied snippets of text?
You can try below solution.
// Copy text to the clipboard.
private void btnCopy_Click(object sender, EventArgs e)
{
Clipboard.SetText(txtCopy.Text);
}
// Paste text from the clipboard.
private void btnPaste_Click(object sender, EventArgs e)
{
txtPaste.Text = Clipboard.GetText();
}
For more info : Copy and paste text to and from the clipboard C#
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to do a filtering algorithm with C#, it will be a textbox and I will enter the forbidden words there. If banned words are found in the text I enter in richtextbox later, the word will be deleted as soon as it is written. How can I do this?
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
string[] badWords = textBox1.Text.Split(',');
string[] myText = richTextBox1.Text.Split(',');
foreach (var badWord in badWords)
{
if (myText.Contains(badWord))
{
richTextBox1.Text.Replace(badWord, "");
}
}
}
C# strings are immutable, you need to assign the Text property of your RichTextBox:
if (myText.Contains(badWord))
{
richTextBox1.Text = richTextBox1.Text.Replace(badWord, "");
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
My application prints without showing the PrintDialog (and it need to do so) but I'd like to be able to set to which printer it should print so I added an appsetting in the app.config where it stores the printer name. what I want is a dialog where it shows all printer and the user should be able to choose a printer and it will save in the app.config (I could actually do it from the PrintDialog and get the chosen printer but the button says Print and I don't want to confuse the User...)
Thanks
edit
#methodMan Asked for code so I added My code
System.Windows.Forms.PrintDialog ps = new System.Windows.Forms.PrintDialog();
//set the selected printer in the dialog to the current printer
ps.PrinterSettings.PrinterName = MyApp.Properties.Settings.Default.ContinuesLabelPrinter;
var result = ps.ShowDialog();
if(result == System.Windows.Forms.DialogResult.OK)
{
MyApp.Properties.Settings.Default.ContinuesLabelPrinter = ps.PrinterSettings.PrinterName;
}
OK here is something that will work for you I think...Create a windows Form with a listbox and two buttons.
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
{
listBox1.Items.Add(printer);
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
this.DialogResult = System.Windows.Forms.DialogResult.OK;
}
}
In your code do this:
Form2 form2 = new Form2();
if(form2.Show() == System.Windows.Forms.DialogResult.OK)
{
MyApp.Properties.Settings.Default.ContinuesLabelPrinter = form2.listBox1.SelectedItem.ToString();
}
form2.Dispose(); // <-- this might not be necessary
You will need to make the listBox1 public for this to work.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
im trying to create program that changes format of imported images i dont know how to save it any help would be appreciated
--Edit; im trying save each file imported to specific folder with selected format
my code ;
private void button1_Click(object sender, EventArgs e)
{
if (dialog.ShowDialog() == DialogResult.OK)
{
foreach (String file in dialog.FileNames)
{
listView1.Items.Add(System.IO.Path.GetFileNameWithoutExtension(file));
string filee = saveFileDialog1.FileName;
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
foreach (ListViewItem item in listView1.SelectedItems)
{
listView1.Items.Remove(item);
}
}
}
}
var image = System.Drawing.Image.FromFile(inputFilename);
image.Save(outputFileName, System.Drawing.Imaging.ImageFormat.Gif);
That should do it, if I'm not mistaken. You load an image into memory from a file, then save it given whatever format you want, I chose gif here as an example.