I'm trying to load a string in my textbox when my form loads. Somehow nothing appears. Can somebody explain to me why this is so?
private void Form1_Load(object sender, EventArgs e)
{
string TestText = "hello";
FolderLocation.Text = TestText;
}
I followed pstrjds 's prompt to create a Minimal, Complete and Verifiable example. Turns out I had an error in another part of the code that somehow led to the textbox unable to load any texts. Thanks a lot.
Related
I would like to paste some text into a web browser element. For example:
private void pasteBtn_Click(object sender, EventArgs e){
WebBrowser1.PasteString("some text");
}
PasteString() Clearly doesnt exist but you get what i mean.
I have just tried pasting the string outright with a button, but the browser loses focues before it pastes.
I have tried many thing but none of them work.
Anything would help, i just wanna paste a string into a textbox on a website with a button, or something alike.
I think you need something like this:
N.B. not tested
private void pasteBtn_Click(object sender, EventArgs e){
var elemWithFocus = WebBrowser1.Document.ActiveElement;
elemWithFocus.SetAttribute("value", theStringToPaste);
}
I'm trying to get a DataGridView control (on tab control of Form1) to update when I write data to the underlying MS Access database from two other Forms.
I've read a vast number of replies to similar questions here on StackOverflow and all around the internet but the usual responses;
myDataGridView.Update()
and
myDataGridView.Refresh()
don't work.
Neither does
myBindingSource.ResetBindings(false)
or changing the
DataGridView.DataSource
by setting it null and then setting it to myBindingSource.
I've also tried using the
myTableAdpter.Fill(myDataSet)
approach but again absolutely nothing.
It's a WinForms application in C# using VS2017 and a simple "Refresh" button on the tab in Form1.
I'm absolutely baffled so any help would be greatly appreciated.
Thanks for any help
Regards
Matthew
p.s. Here's the current code I have for the Reset Button but it doesn't do anything.
private void button1_Click(object sender, EventArgs e)
{
RefreshDisplay();
}
private void tabControl1_TabIndexChanged(object sender, EventArgs e)
{
RefreshDisplay();
}
private void RefreshDisplay()
{
sacsDataGridView.DataSource = null;
sacsBindingSource4.ResetBindings(false);
sacsDataGridView.DataSource = sacsBindingSource4;
sacsDataGridView.Update();
sacsDataGridView.Refresh();
}
Regards
Matthew
So I was make the display name (content) of a Lable from another page of the GUI. After realising a public static void function can't change the Displayname because it's not static, I messed around with Events and got a Handler set up to run when there other page publishes the event.
It looks like this
public void UpdateEventHandler(object sender, System.EventArgs e)
{
System.Windows.Forms.MessageBox.Show("Event Received");
tab1.DisplayName = Globals.tab1Name;
}
When the event goes off the message box pops up but the DisplayName does not change.
There was no errors, nothing.
The problems is not Globals.tab1Name because I ran it through the MessageBox and it was fine.
So I made another button, on the same page as the Lable:
private void Button_Click(object sender, RoutedEventArgs e)
{
tab1.DisplayName = Globals.tab1Name;
System.Windows.Forms.MessageBox.Show("clicked");
}
This time all the code worked, lable changed and msgbox popped up.
I made another function with the same two lines of code in it, called it with the event handler, again only the msgbox works. But when I call the same function with the button it all works.
Any help would be great, thanks in advance.
MessageBoxes pause the execution of the current thread. Therefore, when you show the MessageBox first the second line isn't hit thus the string isn't updated
Try adding the display name as a parameter for the form load. By default the form has sender and e as parameters, but you can always add your own too.
I created a UserControl with the buttons Save, Close and Cancel. I want to close the form without saving on the Cancel button, prompt a message to save on the Close button and Save without closing on the Save button. Normally, I would have used this.Close() on the Cancel button, but the UserControl doesn't have such an option. So I guess I have to set a property for that.
Scrolling down the "Questions that may already have your answer" section, I came across this question: How to close a ChildWindow from an UserControl button loaded inside it? I used the following C# code:
private void btnCancel_Click(object sender, EventArgs e)
{
ProjectInfo infoScreen = (ProjectInfo)this.Parent;
infoScreen.Close();
}
This does the job for one screen, but I wonder if I have to apply this code for all the screen I have? I think there should be a more efficient way. So my question is: Do I need to apply this code for every form I have, or is there another (more efficient) way?
you can use
((Form)this.TopLevelControl).Close();
you can use the FindForm method available for any control:
private void btnCancel_Click(object sender, EventArgs e)
{
Form tmp = this.FindForm();
tmp.Close();
tmp.Dispose();
}
Do not forget to Dispose the form to release resources.
Hope this helps.
You also can close one form in any part of the code using a remote thread:
MyNamespace.MyForm FormThread = (MyNamespace.MyForm)Application.OpenForms["MyForm"];
FormThread.Close();
I found the simple answer :) I all ready thought of something like that.
To close a WinForm in a ButtonClicked Event inside a UserControl use the following code:
private void btnCancel_Click(object sender, EventArgs e)
{
Form someForm = (Form)this.Parent;
someForm.Close();
}
In my WindowsPhone application I have a Text Box that needs to receive focus at a given time. What I've tried so far:
textBox1.Focus();
textBox1.UpdateLayout();
textBox1.Focus();
textBox1.IsTabStop = true;
textBox1.UpdateLayout();
textBox1.Focus();
textBox1.IsTabStop = true;
textBox1.Focus();
Nothing seems to work. In the Emulator, when the Focus() method is called, the keyboard starts to rise, but then crashes back. The TextBox has IsTabStop set to true in the properties.
Does this help?
this.ActiveControl = textBox1;
If you want the text box to be selected without you having to click into it (if that is what you mean by focus), try:
textBox1.Select();
This seems to be a Silverlight bug. I've used the TextChanged event on the TextBox, and set the Focus() in there. Kind of an ugly workaround, but it worked for me. If anybody has another solution, please post.
Even I have tried all the above solutions but didn't work for me. Finally I tried with the following thing, and it worked.
private void txtBox_LayoutUpdated(object sender, EventArgs e)
{
txtBox.Focus();
}
Just set focus property of any control at Form_Activated time..
private void Form_Activated(Object sender, EventArgs e){
Textbox.Focus();
}
you can accomplish this by programmatically giving it focus. This can be done by
calling its Focusmethod, although this call can fail (and return false) under certain conditions.
For example, you cannot set focus on a control from a page’s constructor; it’s too early. You can,
however, call it from a page’s Loadedevent.
if this is a textbox created in the runtime like this:
TextBox TextBox1 = new TextBox();
TextBox1.Name = "TextBox1";
then add this line
TextBox1.Loaded += new RoutedEventHandler(TextBox1_Loaded);
and then add this following event also:
private void TextBox1_Loaded(object sender, RoutedEventArgs e)
{
((TextBox)sender).Focus();
}
This may be a little late a couple years late but after playing around in windows phone 8.1 development environment I found the solution:
TextBox.Focus(FocusState.Keyboard);
You need to pass in the type of FocusState through the FocusState class.
It's worked for me
this.YourTextbox.TabIndex = 0;
After spend hours in debugging, I found the solution
await Task.Delay(100);
tbInput.Focus(FocusState.Programmatic);
if we have tow textboxes
exp:
txtPrincipal
txtPrincipal_Contact
if want move from txtPrincipal to txtPrincipal_Contact
we need to write second textbox name with focus function in leave event of the current textbox exp:
private void txtPrincipal_Leave(object sender, EventArgs e)
{
txtPrincipal_Contact.Focus();
}