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 am quite new to this and I am building an easy app in visual studio in C# which is plotting graphs and user can customize them by using checkboxes and radiobuttons. These are linked to events and when checkstate is changed, the event is called and the code do its job. But these events are called even when the checkstate was not changed and all plotted areas reload multiple times which is not very pleasant to the user. Can you please advise me how to call the event only when it is required. It's WinForms. An example is below. I want to display the output in both cases - if the bool value is true or false, the output is dependent on this and the outcome is different.
`
private void CheckBoxCountInvalid_CheckStateChanged(object sender, EventArgs e)
{
if (checkBoxCountInvalid.Checked)
countInvalid = true;
else
countInvalid = false;
ShowOutput();
}
`
An (if else) sounds like if would work fine for that problem.
Related
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 3 years ago.
Improve this question
Pretty much new to Windows Forms, I know the C# language, just not in the same context. I have searched around for a while and it seems to me that every solution is doing something similar to this:
Label1.Text = "I'm a label".
But I don't understand where Label1 is coming from.
All I have is a new Windows Form Application, which comes with one form preloaded and a Program class. So as this class came with some code, I thought this would be a logical way of accessing the label's properties:
static class Program
{
static void Main(String[] args)
{
FormUpdate frmUpdate = new FormUpdate();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(frmUpdate);
// Why isn't this a suitable way of getting the label?
frmUpdate.label1.Text = "I cause an error!";
}
}
But I don't understand where Label1 is coming from.
Someone used the Visual Studio Designer for Windows Forms and dragged and dropped a Label component onto their form. As Visual Studio has no way of naming them, but needs a name, it simply counts up. The first dropped label is called "Label1".
The access specifier for those controls added is private by default and I'd suggest to leave it that way. If you want to interact with your form, either do it from inside your form or write a public method that you call that will then set all the private properties like the text of a certain label.
Generally speaking, Application.Run(frmUpdate); is running the program, based on the starting form you gave. Anything after that will have little effect. So you ran your form and after you closed it, you set the label. That's not going to have any visible effect. You need to do that before you run the form or while you are running it.
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 4 years ago.
Improve this question
I just need to execute some code. But I don't need to put it in any event handler.
Is there any posibility to do that in C# language? In particular, in Windows Form.
In other words how to execute a code beyond any event handlers like "button click", "mouse click", "timer tick" and so forth.
if (!boolA)
{
//do here something
}
else return;
You can put it in the constructor of the form or double click your form and put the code in the Load handler (although this is an event handler but it is very customary to put code here that needs to be executed before the form is shown). You can also put the code in the class named Program.cs and that code will be executed when the application starts-just put the code before you show the entry form.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I want to make an event that when entering text into the text box the first letter of the text to be capitalized automatically in fastest way.
Try something simple like this:
create a text_changed event on the text box
private void textBox1_TextChanged(object sender, EventArgs e)
{
{
if ((textBox1.Text.Length) == 1)
{
textBox1.Text = textBox1.Text[0].ToString().ToUpper();
textBox1.Select(2, 1);
}
}
}
Should you have issues with the formatting of the text due to other requirements, than it needs to be done in a different way.
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 6 years ago.
Improve this question
I already set validation in all my output that works fine but I have a lot of view and a next button, I want that button to be disabled every time there is an error in the output, it's already set on IsValid and I did this in the code behind of the view:
private void abc_Click(object sender, RoutedEventArgs e)
{
if (Validation.GetHasError(CinInput) == true|| .......)
Console.WriteLine("+++++++++++++Nope+++++++++++++++++");
else
Console.WriteLine("+++++++++++++OK+++++++++++++++++");
}
I need a solution to bind the the result to my viewmodel so i can set isvalid to false any suggestion?
If you're using data binding then its possible to pass a command or command parameter to the viewmodel.
Looks like you're not following MVVM pattern from your abc_click.
You can access your viewmodel like this from your code behind :
var viewModel = DataContext as ViewModelClassName;
viewModel.SomeBooleanProperty = true; // Or false
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
In the system I'm doing , I need a calendar that has only the weekends of each month enabled to be selected , the rest is off ..
How can I do this in C # Winforms ?
I researched on the internet and here at the Forum and found nothing exactly like that, I found some things but I could not do what I want.
Welcomee to stackoverflow,
This option is not available within the current tools. You could go and make a custom control by making a custom DateTimePicker inherited from the standard one. I never done this but this might get you started. or you might be able to do something with this article.
If you don't want to go into custom Controls which will make you spend a lot of time, you could just do something like this:
private void dateTimePicker1_ValueChanged(object sender, EventArgs e) {
var dtp = sender as DateTimePicker;
var selectedDate = dtp.Value;
if (!(selectedDate.DayOfWeek == DayOfWeek.Saturday || selectedDate.DayOfWeek == DayOfWeek.Sunday))
{
var offset = (int)DayOfWeek.Saturday - (int)selectedDate.DayOfWeek;
var saturday = selectedDate + TimeSpan.FromDays(offset);
dtp.Value = saturday;
label1.Text = "only saturday or sunday please";
}
}
and prompt the user with a label message telling him that it has been changed.
There is no specific control in Winforms. Easiest workaround is to use a WPF control and then a Winforms host for WPF control. You can find some details here: https://stackoverflow.com/a/27319756/3330348