c# Button Press Detection [closed] - c#

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 4 months ago.
Improve this question
I'm making a "web browser" in C#, and I'm coming across problems. I don't think my button to go to the navigated URL is working, and I don't know how to test it. When you click the button, it runs mainBrowser.Navigate(urlTextBox.Text);, but that's not working for some reason. This is my code:
private System.Windows.Forms.GroupBox browserGroup; //container of sorts
private System.Windows.Forms.TextBox urlTextBox; //self-explanatory
private System.Windows.Forms.Button navigateToURL; //button to go
private System.Windows.Forms.WebBrowser mainBrowser; //browser
private void navigateToURL_Click(object sender, EventArgs e) //on the button's click
{
mainBrowser.Navigate(urlTextBox.Text);
}
What am I doing wrong?
PS: I literally just started c# today.

I suppose you are using Visual Studio IDE ...
So, here are some directions for you to start debugging and solve it yourself:
Place a breakpoint inside the 'navigateToURL_Click' event handler method. Run on debug mode, add a URL to the URL text box and check if you can catch the 'navigateToURL' button click there.
If that doesn't work, check the button 'Click' event in the button properties dialog. It should point to your 'navigateToURL_Click' method, as seen in this next image:
On the other end, if that breakpoint works, you can debug and watch the 'urlTextBox.Text' value to see if it is correct.
That's the idea of debugging (an inseparable part of any coding), from here you can think of more checks & test ideas like this ...
Good luck with C#... Have fun coding ...

Related

How to access design Windows Forms C# [closed]

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.

WPF Window Closing does not work after Cancel [closed]

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 have an issue that I can't seem to resolve via Google (likely because I'm not searching the right criteria). I have a Closing Event that checks if a button is enabled and pops a messagebox with a result (Yes/No). If the user says NO, I get the desired results in the app; however, the X in the top right corner stops working. How to I "reinstate" the close button ("X" in the top right hand corner) so it works again if pressed (and also evaluates the logic again).
I tried this: Stackoverflow Question
i don't think I want to play with Visibility of the window. The window doesnt go anywhere. They have dirty data and they need to fix it or have it auto saved.
What I have in the application is:
private void DXWindow_Closing(object sender, CancelEventArgs e)
{
if (BtnPatSave.IsEnabled == false) return;
if (MessageBox.Show(Properties.Resources.PatAcctMsgBox3, Properties.Resources.PatAcctMsgBox1,
MessageBoxButton.YesNo,
MessageBoxImage.Warning) == MessageBoxResult.No)
{
e.Cancel = true;
}
else
{
var patId = TbPatId.Text;
var usl = new UnSetLockValue();
usl.UnSetVal(patId);
Log.Info("Patient Account is now unlocked by user: " + Environment.UserName);
}
}
Thats because you are using the MessageBox class.
It disables the "X" button to allow the user only to provide the values you specify.
If you don't want this behaviour i think you have to create your own "MessageBox".

Form.Show() not showing form [closed]

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.

Opening new form is reseting the design? [closed]

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 7 years ago.
Improve this question
So I have a project with two forms: the MainForm and AnotherForm.
In the MainForm I have a button which should open the AnotherForm. I'm using the following code to do that:
private void btnTimeReminder_Click(object sender, EventArgs e) {
AnotherForm anotherForm = new AnotherForm();
anotherForm.Show();
this.Hide();
}
The problem is, when I click the button to open the "AnotherForm", it just opens a blank form in the WindowsDefaultPosition. All the design of the AnotherForm is deleted (The buttons, images, even the size of the AnotherForm).
I have no ideia why is this happening... Am I using a bad code to open the form?
Ensure that
InitializeComponent();
is being called in the constructor of your new form.
If you need to change the initial position of your new form, you can use
anotherForm.StartPosition = FormStartPosition.CenterParent;
or whichever start position is necessary.

Maintaining focus on textbox while I click on different buttons [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am currently trying to develop a custom keyboard for WinCE application.Currently I have a form with a text box and a button. The issue is how can I maintain the focus on the keyboard when I click on the mouse to SendInput (to make sure that the textbox capture that input). One way is to set the "Focusable" property but I can't seem to set that on a Windows Form. I hope someone could help me on this. Thanks!
If you are not afraid of moving to the native side you may consider to implement a Software Input Panel (SIP). It will behave in the way you describe and can be used by any application running on the device.
This documentation is for Compact 2013, but it's also valid for previous releases (you can find release-specific versions on MSDN but they were pretty good in hiding them):
http://msdn.microsoft.com/en-us/library/ee500563.aspx
You should set the TextBox.Focus() on the button press event handler. I assume the button has a KeyPress or KeyDown function.
A more flexible alternative would be to store the last focused control.
private Control lastFocusedControl;
And when the text box is focused on it sets the value using the GotFocus event.
private void TextBox_GotFocus(object sender, EventArgs e)
{
lastFocusedControl = (Control)sender;
}
And then in the event handler you can simply do.
lastFocusedControl.Focus();
Although it is for VB.Net it has some good ideas: http://msdn.microsoft.com/en-us/magazine/hh708756.aspx
See WS_EX_NOACTIVATE and
If (m.Msg = WM_MOUSEACTIVATE) Then
m.Result = MA_NOACTIVATE
Else
MyBase.WndProc(m)
End If
Now the challenge is to adopt this to your idea (a separate process and form? or a panel with buttons?).
OTOS MS provides a custom keyboard SDK API set to write custom software keyboards for Windows Mobile (c/C++).

Categories

Resources