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.
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 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.
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 5 years ago.
Improve this question
I've a c# project in which a form hides itself after some background task finishes, to be opened on command, but under certain conditions, the call to this.Show() or setting this.Visible to true doesn't make the form reappear! I even had the state of the form's Visible value output to be sure, and the form seems to think it's on screen, but it's nowhere to be seen.
The condition that seems to cause it to break is if I give another window control before the form hides itself. If I let it stay in front while it's working, hide itself, then tell it to come back it always does, even if I change focus after the fact, but I change focus away beforehand, it doesn't reappear, even though form.Show appears to be called.
Does anyone have any insight as to why this might be happening? It's such a weird case, especially since the state of form.Visible changes.
public partial class testForm : Form {
private void testForm_Sometrigger(object sender, EventArgs e) {
//some delay. In the actual program, this is thanks to a background worker working.
Thread.Sleep(5000);
//manually change focus to another process/window before this
this.Hide();
//I've been adding a wait here, since in practice the form won't be called again right away.
Thread.Sleep(3000);
//show form again.
this.Show();
// this will be true even if the form isn't actually visible
Console.WriteLine("is visible? "+this.Visible.ToString());
}
}
Code block added upon request. There isn't much to this bit, just showing and hiding and time passing, really.
Turns out the window not appearing probably has something to do with form inactivity and the UI thread not coming back to it, so adding a Form.Activate() after the show call fixed it.
Original question updated with solution.
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 6 years ago.
Improve this question
I'm working on my first mobile app. I'm getting input from the User and having them click a button before retrieving data into a dataset. All that is working well but getting the data displayed has stopped me in my tracks. Can/Should I display it in the same page or create a 2nd one? How do I call a second page from my c# code? I don't know best practices for Xamarin and mobile so any assistance or direction would be helpful.
There are multiple ways of doing this and reasons for doing it, it all depends on your app. I assume you're not using xamarin forms, so I'll give default android/iOS answers.
Can/Should I display it in the same page?
If you're making the usual search control it helps displaying the search results in the same page as the one you are typing in.
If you have a list of items, where selecting one shows more details, then the usual way of doing it is to navigate to a new page that displays the information. So you'll have to create another page.
There are multiple ways of doing this it all depends on your app.
How do I call a second page from my c# code?
You don't call the second page from your first page, you rather launch the page and pass the information you want.
Android: You'll create a new Activity with it's layout file and use an intent to launch the activity from your Action (clicking a button in your case).
You can use this tutorial to get up to speed with miltiscreen Xamarin applications for android. Your code should look similar to the following.
Button yourButton = FindViewById<Button> (Resource.Id.YourButtonId);
yourButton.Click += (sender, e) =>
{
var intent = new Intent(this, typeof(SecondActivity));
StartActivity(intent);
};
To pass information to the second page you can use one of the intent.PutExtra() methods. Similar to
intent.PutStringArrayListExtra("YourKey", myList);
Which passes a list to the launching activity through the intent, storing the value under YourKey. In your SecondActivity you can get the data from your intent using
intent.GetExtra("YourKey")
iOS: Not much code involved so I'll list the steps to do this briefly. You can use this tutorial to get started with multiscreen iOS applications.
You'll create a new controller in your Main storyboard, using Interface Builder or Xamarin Studio.
On your first view, place your button.
press Ctrl and drag it to the new ViewController. On releasing your click select Show (push) segue.
In your FirstViewController override prepareForSegue and set the data you want to use in your SecondViewController.
Something like below.
public override void PrepareForSegue (UIStoryboardSegue segue, NSObject sender)
{
base.PrepareForSegue (segue, sender);
// set the View Controller that’s powering the screen we’re
// transitioning to
var secondViewController = segue.DestinationViewController as SecondViewController;
secondViewController.YourData = "Your data you want to pass over";
}
Let me know if this answers your question. Goodluck.
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 7 years ago.
Improve this question
Let's take an example of what i want. If i have a form in C# and i want this form design and action varies according to some files say XML files sometimes this form has a picture box and button do something, sometimes this form has some labels, buttons , panels do something else according to the external file given to the program. may these files has form styles, controls, action??
How is it possible to do that and if it is possible how to do it??
By reading from an Xml file, this can be achieved.
You need to have a FormDesignManager which should be able to parse the xml and create the form put all the controls and return the form.
publci class FormDesignManager
{
public static Form CreateForm(string xmlPath)
{
var form = new Form();
//design the controls for the form
//you can get System.Windows.Forms.TextBox or any other controls from the xml
var control = typeof(Control).Assembly.GetType("System.Windows.Forms.TextBox", true);
var textBox = Activator.CreateInstance(textBoxType);
return form;
}
}
Please, see this C# Create Control From a String Value