Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm building a program with C# in Windows Forms, and the following question came to me. I have several buttons in my form and when any of them is clicked, I want to be able to store its ID in a single variable that will handle only one ID at a time. I already have a method that does this, but the fact is I don't want to call this method from the event handler of each button:
button1_Click(object senders /* ... yada yada ... */)
Is there a way how I can simplify this with a single method? Is it even possible?
You don't need many Click event handlers for your buttons, just 1 is enough:
private void buttons_Click(object sender, EventArgs e){
Button button = sender as Button;
//do something with the clicked button
//...
}
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I want write a program that :
In textbox type a picturebox name and in other textbox type location number,then press button to move that picturebox that name is equal textbox1 to that location.
my question is how I can send a object(picturebox) to a Function?
If I understand you correctly you want to move a picture box by entering its name into a textbox? Then this is a possibility:
private void button_click(object sender, EventArgs e)
{
string pBoxName = textbox1.Text;
// I don't quite understand what you mean by 'location number'
int newPos = int.Parse(textbox2.Text);
// boxList is a List<PictureBox>
PictureBox pBox = boxList.Where(x => x.Name.Equals(pBoxName)).FirstOrDefault();
if(pBox != null)
{
MoveTheBox(pBox, newPos);
}
}
private void MoveTheBox(PictureBox box, int newPos)
{
// move the box
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a pictureBox and I want to perform a some statement when the image is clicked, So how can i check when the pictureBox is selected inside an if statement ?
Add an event handler to the MouseClick event, and then add in the code you want to run within the handler method:
MyPictureBox.MouseClick += new MouseEventHandler(MyEventHandler);
private void MyEventHandler(object sender, MouseEventArgs e)
{
//your code here
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
In my project, I need to get the listview containing the data from the back end, If I place the listview in the form and upon clicking a button I can load data, but the requirement is initially the listview should be hidden or not present there, only on clicking the button the listview should be viewed with data. Thank you.
Set the List View's property Visible = Falsein the properity window.
Within the button click event handler set it to true;
listView1.Visible = true;
You use the Visible Property. At the form load, you set it to false and then in the button_click
private void button1_Click(object sender, EventArgs e)
{
listView1.Visible = true;
}
I think You should use listView1.Hide(); initially in Form constructor
And on button click You should use listView1.show()
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Could anyone explain me how the compiler knows to perform appropriate method when we select RadioButtons in this example ?
It's hard to say for sure what you're asking. I think you're asking how the system knows that it should execute the iconType_CheckChanged method when one of the icon radio buttons is clicked, and how it knows that, for example, the asteriskRadioButton changed.
The answer is in two parts. First, in creating the program in Windows Forms, you hooked up the CheckChanged event handler for each of the radio buttons. So the asteriskRadioButton CheckChanged method contains the value iconType_CheckChanged. That information is added to the partial class that you don't usually see. It's in your Form.Designer.cs file in the InitializeComponent method. It looks something like:
this.asteriskRadioButton.CheckChanged += iconType_CheckChanged
You don't typically see the Form.Designer.cs file. To view it, expand the form node in the Visual Studio Solution Explorer and you'll see the file listed:
The second part of the answer is that when you click the radio button (or when some code changes the state of the radio button), the underlying control machinery calls iconType_CheckChanged, passing a reference to the control that triggered the event in the Sender argument.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Im wondering how to make my textbox format "DD.MM.YYYY" that after load form the textbox will be filled with "__ .__ .__" and those spaces and dots cannot be deleted. Is there any way to do that?
Thanks
Why don't you use a MaskedTextbox? Use the following as mask, you can set the mask either at design time or at form load.
private void Form1_Load(object sender, EventArgs e)
{
maskedTextBox1.Mask = "00.00.0000";
}
You would probably be looking at something like Ajax Masked Edit
Another way to do it would be to use a jquery plugin
You could use a MaskedTextbox that will do exactly what you describe. A DateTimePicker might be better though...