I want to create button that is named by the user.
This is what I have (sorry I'm so bad at this but I am trying :D)
string 1;
private void button1_Click(object sender, EventArgs e)
{
if (1 = null)
{
Console.Write("Give Button's name");
1= Console.ReadLine();
button1.Name = 1;
}
}
I also want the button to open a file path selected by the user. Is there a way to do that?
(sorry for my stupid question and anything else... :D)
Based on yr comments:
1. Use a TextBox(called txtUserInput) to get the user's input
2. Use OpenFileDialog to help user to choose a file
private void button1_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(txtUserInput.Text))
{
button1.Text = txtUserInput.Text;
var openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
MessageBox.Show("You chose: " + openFileDialog.FileName);
}
}
}
You can't mix Console Application input with Windows Forms input. You need to decide what you want. From the comments, it looks like you want a Windows Form Application so you will need some way to get user input like a text box.
On your form you will need to create a text box and give it a name like tbxUserInput. After that you can change you click method to do something like this:
private void button1_Click(object sender, EventArgs e)
{
button1.Text = tbxUserInput.Text
}
One thing that was wrong was that you were assigning the Name property of the button and not the Text property. The text property is what is actually displayed on the button.
It may be a good idea to read some tutorials about Windows Forms.
Related
I am in the process of writing a vocabulary program. C # Windows Form.
Description of the program operation:
Use the buttons to select the location of text files with the words "PL" and "ENG". (two separate files)
Click the start button to start the program
the first word from the board appears in the label
I'm translating the word into the textbox and the Messagebox "OK" or "WRONG" pops up
And here a problem arises. The program instead of every time I wait until I introduce a new word to the textbox, it loops, the questions in the label are changed and MessageBox displays.
How best to do this to make the program work correctly? `` `[
private void sprawdzButton_Click(object sender, EventArgs e)
{
BazaSlow.bazaPolskichSlowek = _fileReader.Read(adresPlikuPL);
BazaSlow.bazaAngielskichSlowek = _fileReader.Read(adresPlikuANG);
string odpowiedz = odpTextBox.Text;
int i = 0;
while (i < BazaSlow.bazaPolskichSlowek.Length)
{
trescSlowkaLabel.Text = BazaSlow.bazaPolskichSlowek[i];
if (odpowiedz.Equals(BazaSlow.bazaAngielskichSlowek[i].ToLower()))
{
MessageBox.Show("OK");
}
else
{
MessageBox.Show("ŹLE");
}
i++;
}
}
This approach will not quite work.
If you use WinForms then you can do it via events. I'll quickly use english variable names since I don't speak your language.
This could be one approach to do it: I used the "TextChanged" event from the textBox.
string[] wordsLanguage1;
string[] wordsLanguage2;
int currentIndex = 0;
private void Form1_Load(object sender, EventArgs e)
{
wordsLanguage1 = System.IO.File.ReadAllLines("somePath1");
wordsLanguage2 = System.IO.File.ReadAllLines("somePath2");
}
private void ReportAndCheckInput(string input)
{
if (input.ToLower().Equals(wordsLanguage2[currentIndex].ToLower())) {
//right translation
currentIndex++;
label1.Text = wordsLanguage1[currentIndex];
textBox1.Text = "";
}
else
{
//wrong translation
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
ReportAndCheckInput(textBox1.Text);
}
Now this approach uses the TextChanged event. So the ReportAndCheckInput method will be called on every text-change. That means that your Feedback would pop up on every keystroke which would not be nice. You could use any other event instead of TextChanged. For example a button click. Another solution would be to use a label for your feedback and not a message box. Then the user would never have to click anything but woudl instantly see whether or not he was correct.
You all know those Login Textboxes, where inside it says "Username" and "Password".
So my problem is, that i can´t find anything in the Internet that explains to me how i can detect if anything is written in the textbox.
Then i could use "Username" as a background image, that turns white when i start typing. Something like:
if (Textbox_is_not_empty)
{
Change_Background_image_To_white_colour
}
You can erase the text inside the TextBox by handling the Enter event.
private void textBox1_Enter(object sender, EventArgs e)
{
textBox1.Clear();
}
And set the text to "Username" when you leave and the TextBox is empty.
private void textBox1_Leave(object sender, EventArgs e)
{
if(textBox1.Text == "")
textBox1.Text = "Username";
}
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;
}
}
Is there an easy way to show an dialog when the program is started for the first time (and only the first time), for some kind of instruction or specifying settings?
You could save it as a bool in your settings and you should check at load event of first form.
Your settings file should have a setting that I called "FirstRun" do this with following steps:
Right click your Project
Click "Properties"
Click "Settings" tabpage(probably on the left)
Add setting like I did as seen in image above
Note: The Scope can be changed to "Application", if that is your application's need, since you didn't mention in your question.
Your Settings file should look like image below:
public void Form1_Load(object sender, EventArgs e)
{
if((bool)Properties.Settings.Default["FirstRun"] == true)
{
//First application run
//Update setting
Properties.Settings.Default["FirstRun"] = false;
//Save setting
Properties.Settings.Default.Save();
//Create new instance of Dialog you want to show
FirstDialogForm fdf = new FirstDialogForm();
//Show the dialog
fdf.ShowDialog();
}
else
{
//Not first time of running application.
}
}
Note: wrote this from my phone, so I couldn't compile to test
Edit: Checked code and added image from desktop.
You can have bool value in your settings file which is a "user setting" which means you can change it to true save it for this specific user.
When your application starts just check that value. If it's false show your dialog and change it to true and it will stay true.
public void Form_Load(object sender, EventArgs e)
{
if(Settings.Default.ShowDialog)
{
Settings.Default.ShowDialog = false;
Settings.Default.Save();
// show first disalog
}
// rest of code if needed
}
Here's an MSDN link on user settings:
http://msdn.microsoft.com/en-us/library/bb397750(v=vs.110).aspx
Ok, so I assume you're creating WinForms application. First of all, locate the Load event in your main Form event lists (or simply double click your Form in Designer panel). The following method stub will pop up:
public void Form1_Load(object sender, EventArgs e)
{
}
And modify it like this:
public void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show("Your message here");
}
I have a windows form application which consists of a bunch of controls, but more specifically, two textBoxes. One of them is read only. The read only textBox value is supposed to be the same as the textBox that the user can type into.
So if the user types "Hello World" into textBox A, the value in textBox B should be automatically updated to "Hello World".
How do I go about doing this? I know I just need to set the text values, I'm just not sure where I place the code to get it done automatically rather than executed when a button is click or something along those lines.
TextChanged event:
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox2.Text = textBox1.Text;
}
It sounds like you want something like:
writableTextBox.TextChanged += delegate {
readonlyTextBox.Text = writableTextBox.Text;
};
In other words, whenever the text in one textbox changes, update the other. This uses the Control.TextChanged event.
If you want textBoxB to be updated as soon as the text of textBoxA is changed (i.e immediately after the user press a key in textBoxA) the event is TextChanged:
this.textBoxA.TextChanged += new System.EventHandler(this.textBoxA_TextChanged);
private void textBoxA_TextChanged(object sender, EventArgs e)
{
textBoxB.Text = textBoxA.Text;
}
If you prefer to update the text in textBoxB only after the user has finished to edit textBoxA, you should use the Leave event:
this.textBoxA.Leave += new System.EventHandler(this.textBoxA_Leave);
private void textBoxA_Leave(object sender, EventArgs e)
{
textBoxB.Text = textBoxA.Text;
}
This should do what you need:
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox2.Text = textBox1.Text;
}
Even shorter (better?) than the event approach is using winform's databinding. Just use this right after the InitializeComponents call:
readonlyTextBox.DataBindings.Add("Text", writableTextBox, "Text");