So I'm trying to figure out how to change a buttons text based on having the buttons name as a variable. I already know how to change a buttons text by hardcoding its name like Button1.Text = "hello";
But I want to be able to do something like storing Button1's name in a string called value and then use that string to modify the buttons text so i can use a method for several buttons instead of pasting a huge similar block of code in every buttons method, I just cant seem to figure out how. Any ideas would help.
private void Button3_Click(object sender, EventArgs e)
{
Button obj = sender as Button;
string buttonname = obj.Name;
boxfill(buttonname);
}
private void Button4_Click(object sender, EventArgs e)
{
Button obj = sender as Button;
string buttonname = obj.Name;
boxfill(buttonname);
}
private void Button5_Click(object sender, EventArgs e)
{
Button obj = sender as Button;
string buttonname = obj.Name;
boxfill(buttonname);
}
// this will send the buttons name to boxfill
private void boxfill(string value)
{
// will ideally change the button named with value's
// text to "x"
}
Related
Now solved. Thanks for your answers!
This is my code right now:
//Listbox scripts is the name of my folder
private void Form1_Load(object sender, EventArgs e)
{
foreach (var file in Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + #"Listbox scripts"))
{
string file2 = file.Split('\\').Last();
listBox1.Items.Add(file2);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
webBrowser1.Document.InvokeScript("SetText", new object[]
{
File.ReadAllText(string.Format("./Listbox scripts/{0}", listBox1.SelectedItem.ToString()))
});
}
I'm new to coding in C# and I have a textbox that has the names of text files in a directory and when I click on the text file in the listbox it's supposed to load the text from it into my textbox (named 'ScriptBox')
Here's my code:
private void Form1_Load(object sender, EventArgs e)
{
string User = System.Environment.MachineName;
textBox1.Text = "{CONSOLE} Welcome to Linst, " + User + "!";
directory = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + #"Scripts");
files = directory.GetFiles("*.txt");
foreach (FileInfo file in files)
{
listBox1.Items.Add(file.Name);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var selectedFile = files[listBox1.SelectedIndex];
ScriptBox.Text = File.ReadAllText(selectedFile.FullName); //these parts are the parts that dont work
}
Thanks in advance!
Add the below into your Form1.cs. What this is going to do is when a user clicks a listbox item, its going to call (raise an event) the "listBox1_MouseClick" method and set the text of the textbox to the text of the listbox item. I just quickly created an app and implemented the below and it works.
private void listBox1_MouseClick(object sender, MouseEventArgs e)
{
textBox1.Text = listBox1.Text;
}
And add the below to the Form1.Designer.cs where the rest of your list box properties are. The below is subscribing to an event, the listBox1_MouseClick method in Form1.cs, so when a user clicks on a listbox item, the listBox1_MouseClick method is going to run.
this.listBox1.MouseClick += new MouseEventHandler(this.listBox1_MouseClick);
I hope the above makes sense.
Your code is nice, and perfect but it just need a little validation check in list index selection
Try thing in your listbox_selectedIndexChanged
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedIndex!=-1)
{
FileInfo selectedFile = files[listBox1.SelectedIndex];
ScriptBox.Text = File.ReadAllText(selectedFile.FullName);
}
}
I actually don't see a problem with your code, could it be a typo somewhere?
I did this and it worked for me:
private void Form1_Load(object sender, EventArgs e)
{
foreach (var file in System.IO.Directory.GetFiles(#"c:\"))
{
listBox1.Items.Add(file);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedItem != null)
{
textBox1.Text = System.IO.File.ReadAllText(listBox1.SelectedItem.ToString());
}
}
I want to change the text of the Button but also add the mnemonic key.
private void button3_Click(object sender, EventArgs e)
{
button3.UseMnemonic = true;
button3.Text = "&foo";
}
What I want to get:
What I get right now:
I've started to learn about System.IO in C# and I want to achieve something like this:
I have two buttons and one TextBox.
The first button event is supposed to use FolderBrowserDialog and let me choose a specific a folder.Then, to save its path in a variable.
The text box is supposed to get as a value the number of folders that I want to create in the choosen path.
The second button is going to create the number of folders(with different name each) written in the textbox at the first button specified path.
My buttons and textbox events so far:
...
int value;
String path;
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog(this) == DialogResult.OK)
{
MessageBox.Show(fbd.SelectedPath);
path = folderBrowserDialog1.SelectedPath;//store selected path to variable "path"
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
value = Convert.ToInt32(textBox1.Text);//store the value from the textbox in variable "value"
}
private void button2_Click(object sender, EventArgs e)
{
if (!Directory.Exists(path))//if selected path exists
{
for(int i=0;i<value;i++)//trying to go through as folders as I wrote in the TextBox
{
Directory.CreateDirectory(path + "something");//is something wrong here, I guess.
}
}
}
My questions so far:
what's wrong with my code ?
how can I create each time the for(){} executes a folder with different name ?
I would appreciate any help
int value;
string path = null;
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog(this) == DialogResult.OK)
{
MessageBox.Show(fbd.SelectedPath);
path = fbd.SelectedPath; //fbd not folderBrowserDialog1
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
value = Convert.ToInt32(textBox1.Text);//store the value from the textbox in variable "value"
}
private void button2_Click(object sender, EventArgs e)
{
if (path != null && Directory.Exists(path))
for(int i=0;i<value;i++)
Directory.CreateDirectory(Path.Combine(path,string.Format("SomeFolder{0}",i)));
}
i have form, it goes like this
that nomor tabungan is auto generate
the problem is, i want to get nomor nasabah value from different form, through the datagridview. take a look at the following picture
there's a button ambil to retrieve the nomor nasabah value, and pass them to nomor nasabah textbox.text
i have successfully to get the nomor_nasabah value from datagridview. it is shown like the following picture
how do i pass the value to the tambah tabungan . nomor nasabah textbox.text ?
i have set the textbox modifier into public so when i click the ambil button, the textbox is filled with the retrieved value automatically.
how do i do that ?
i did the following code, and idk why it doesn't work
here is the ambil button code
private void button2_Click(object sender, EventArgs e)
{
Tabungan.Tambah tambahtabungan = new Tabungan.Tambah();
//nomornya is the retrieved value
tambahtabungan.textBox2.Text = nomornya;
}
here is the cari button code, to show getCustomer form
private void button2_Click(object sender, EventArgs e)
{
if (getcustomer == null || getcustomer.IsDisposed)
{
getcustomer = new getNasabah();
getcustomer.Show();
}
}
You should try this.
first make a public property on getCustomer form like
public string nomornyaValue { get; set;}
and modify your ambil button click event like, and set this property to your datagrid value.
private void button2_Click(object sender, EventArgs e)
{
nomornyaValue = nomornya;
this.DialogResult = DialogResult.OK;
}
and on tambah tabungan button Cari click call getCustomer form like
private void Cari_Click(object sender, EventArgs e)
{
/*getCustomer getCustomerForm = new getCustomer();
if(getCustomerForm.ShowDialog() == DialogResult.OK)
{
textBox2.Text = getCustomerForm.nomornyaValue;
}*/
if (getcustomer == null || getcustomer.IsDisposed)
{
getcustomer = new getNasabah();
}
if(getcustomer.ShowDialog() == DialogResult.OK)
{
textBox2.Text = getcustomer.nomornyaValue;
}
}
On clicking Go button the name given in textbox should be displayed as a link label and this should increment dynamically.
Here my code:
private void buttongo_Click(object sender, EventArgs e)
{
linkLabelName.Text = textBoxName.Text;
}
private void btnGo_Click(object sender, EventArgs e)
{
LinkLabel link = new LinkLabel();
link.Text = txtText.Text;
panTable.Controls.Add(link);
}