C# clear TextBoxes when a button is clicked - c#

On a C# project, I have a "SerialConnectorView.xaml" file that describes an interface with some components.
I added the "Clear all fields" button, called "DoClear" in the xaml. In the file "SerialConnectorsViewModel.cs" I added the "public void DoClear()" method that fires when I click the button. You can see the contents of DoClear() below.
Then I have a "SerialDataView.xaml" file that contains the TextBoxes that display the data.
SerialDataView.xaml and SerialConnectorView.xaml are loaded to the mainview.xaml. The main window looks like the following (there are other components loaded too that don't matter for this):
This is my DoClear method.
public void DoClear()
{
//Clear all the textBoxes
SerialDataView test = new SerialDataView();
test.DataViewParsed.Clear();
test.DataViewHex.Clear();
test.DataViewRaw.Clear();
MessageBox.Show("Still not able to clean the text fields damn it!");
}
The project runs and most things work properly. When I click the "Clear all fields" button the MessageBox shows up but the TextBoxes are not cleared. I don't have any experience with C# but I thought it wouldn't be that hard to clear some textboxes. Well, it's hard for me.
Any suggestions on how I can clear those textboxes on each press of the clear button on the file setup I described?
TIA.

Related

Getting a TextBox to accept user input, not open a folder browser

I am currently working on updating a C# project that makes use of WinForms. One of the minor changes I want to make is as follows.
The project has a Form that currently allows the user to click a Button, which then opens a Folder Browser window where they can select a folder for the project to retrieve information from. The selected directory is entered into a TextBox after being selected. However, clicking on the TextBox also opens up a Folder Browser window. You are also currently unable to manually enter text into the TextBox.
What I want to do is (hopefully) pretty simple: I want the user to be able to enter a directory manually into the TextBox and for the project to accept that text input, and for the TextBox to not open a Folder Browser form upon being clicked.
Some other things to keep in mind:
I am not familiar with all the ins and outs of WinForms, so I could very well be missing something simple that I could do.
I am also in the process of completely restructuring the project, so if this is not possible in WinForms, but is possible in, say, WPF, that would not be a major obstacle for me in this case.
Here is the code for the Button, or at least the pertinent part, as a reference. txtProjectDir is the TextBox in question. Not much else is done with the TextBox in the code except for this part.
private void btnBrowse_Click(object sender, EventArgs e)
{
if (chooseProjectFolderDialog.ShowDialog() == DialogResult.OK)
{
clbConvertProjects.Items.Clear();
clbProjects.Items.Clear();
txtProjectDir.Text = chooseProjectFolderDialog.SelectedPath;
cur_projDir = txtProjectDir.Text;
Update: I have made some changes based on the input of several users (thanks to all of you, by the way). This is what the pertinent part of the code looks like now:
private void btnBrowse_Click(object sender, EventArgs e)
{
if (chooseProjectFolderDialog.ShowDialog() == DialogResult.OK)
{
clbConvertProjects.Items.Clear();
clbProjects.Items.Clear();
cur_projDir = txtProjectDir.Text;
I also had to change the TextBox to not be read-only, as well as remove a reference to the above method from its Event properties. Now it is able to accept user input, and doesn't open a Folder Browser when clicked into.
The only problem is this: if I only enter text directly into the TextBox, instead of selecting a folder via the browser popup, the program doesn't seem to properly accept the input (i.e., no information is being collected from the directory). Obviously, I still need to make the program accept user input, as it currently doesn't.
Update 2: After more suggestions (again, thanks guys) and good old trial-and-error, I have re-inserted the line txtProjectDir.Text = chooseProjectFolderDialog.SelectedPath; as removing it from the method had undesired effects on the program's functionality. I am still having an issue with the program accepting the user's manual input into the TextBox, though.
Update 3: As per #blaze_125's recommendation, I am going to have to create a new event for the TextBox when the user Leaves it. Thank you all for the help, I appreciate it!
However, clicking on the TextBox also opens up a Folder Browser window.
The only event for the TextBox is Action -> Click, which is set to btnBrowse_Click
What I want to do is (hopefully) pretty simple: I want the user to be able to enter a directory manually into the TextBox and for the project to accept that text input, and for the TextBox to not open a Folder Browser form upon being clicked.
If you don't want that event(aka action->click) to happen, then you must remove the text btnBrowse_Click from that textbox and leave it blank. That will remove the event you currently have linked to your textbox click.
There will be an even linked to the text box txtProjectDir to check this select the text box and in the properties select the lightning bolt.
I would assume that the even is a click into the textbox however.
If this is the case you probably want to remove this.
Now in terms of how to accept what is in the text box as the value you just want to reference the text and that is in the textbox whitch would be txtProjectDir.Text
private void btnBrowse_Click(object sender, EventArgs e)
{
if (chooseProjectFolderDialog.ShowDialog() == DialogResult.OK)
{
clbConvertProjects.Items.Clear();
clbProjects.Items.Clear();
cur_projDir = txtProjectDir.Text;

Create a hyperlink from a text file in C#

I have a C# project (Win Forms), where a form reads an external text file, and then puts the text in a textbox on the form. What I would like to do is create a hyperlink from the text that the program reads in.
For example, if the text file reads "To go to Google, click HERE [www.google.com]", then I want the program to make "HERE" clickable, and go to www.google.com if HERE is clicked on.
Right now the program can read in the text file and recognize the web address just fine. I just don't know how to make "HERE" clickable.
Note: Due to external factors, I cannot make a button, or LinkLabel, or other object for the user to click on. The word itself has to be the hyperlink (if that is at all possible). Also, I have to read the string from an external file. I can't simply put textBox1.Text = "To go to Google, click HERE [www.google.com]";
Thanks in advance!
The easiest work-around for what you are looking for would be to add a handler for DoubleClick, and then just compare the selected text.
If it must be a single click, you want to use OnClick, and then get the test up to the last space to the left and to the right, and do the same compare.
VERY HACKISH btw.
private void textBox1_DoubleClick(object sender, EventArgs e)
{
if (string.Compare(textBox1.SelectedText.Trim(), "HERE") == 0)
System.Diagnostics.Process.Start("http://www.google.com");
}
Building on the previous response, you might want to add an eventhandler to a label instead. You should be able to format the label to be blue and underlined to look like a link, also.

ASP.NET WebForms WebSite - controls changes their's values in codebehind but not on frontend

I will try my best to express what I do not understand about WebForms. Maybe somone can explain it to me....
I work in 'quite big' WebForms (website) Application.
Application already has a PopUp (which I HAVE TO USE, because I dont have time to make a new one) like MessageBox in Winforms, but:
it's place is in MasterPage.master (I use it like ((MasterPage)Poup.Show("blablabla", yes_no));
I can add buttons to it and change style etc.
it doenst stop application (like .ShowDialog() in WinForms), so I have to assign onClick events dynamicly and assign/catch them on Page_Load to go to the wanted method depending on users input on form. At the moment i can't to anything about it.
And here is the problem:
Depending on form validation, if users press a button (on a form) - then form realods it's TextBox.Text's values and changes DropDownLists's SelectedIndexes and SelectedValues.
BUT IF YOU GO TO THE SAME METHOD WITH MY POPUP WINDOW:
ex: Do you confirm? YES/NO
If you press You press ANY button, then: CHANGES ARE VISIBLE BY CODEBEHIND BUT TEXTBOXES ARE STILL VISIBLE WITH PREVIOUS DATA ON THE SCREEN, ALSO DROPDOWNLISTS HAVE OLD VALUES SELECTED
BUT IF YOU PRESS A FORM BUTTON (WHATEVER WHICH), EVEN IF IT'S METHOD DOES NOTHING - ALL FORM WILL "RELOAD" AND HAVE PROPER DATA ON THE SCREEN.
(it's not about Runat="server" or AutoPostBack, I checked it)
I dont event know what to do about it :(
Update panels are used to partial refresh of some fields inside him, caused by certain triggers, like buttons, textchanged, etc.
See more about update panel's:
https://msdn.microsoft.com/en-us/library/bb399001.aspx
http://www.asp.net/web-forms/overview/older-versions-getting-started/aspnet-ajax/understanding-asp-net-ajax-updatepanel-triggers
http://www.asp.net/ajax/documentation/live/tutorials/IntroductionUpdatePanel.aspx
(Haven't worked with web forms events in a while, just answering in case nobody else does.)
Out of curiosity, what happens if, in your javascript handler for your popop button, you getElementById one of the form buttons on the main page and call ".click()" on it?
The basic problem sounds like there's a runat/server form on the main page, with certain behaviors associated with it, which is submitted by the main page runat server buttons but not by your popup JS.
Easy enough to test with the above approach. If that works, take a step further and try to figure out why it worked, based on what those buttons are doing.
(If you're not using JS at all for the popup, then View Source the output HTML, because I'm sure it is. Backtracking those calls should help you understand the trace from popup button press to form submission.)
I probably mislead you by "OnClick", it's NOT javascript OnClick="method()" issue.
I mean ClickEvent on a button (in codebehind). For Ex. If form is validated I have to do:
ViewState["PopUpSelection"] = "yes_no";
(MasterPage)PopUp.Show("blabla",yes_no) //loads popup from MasterPage;
return;
then on Page_Load I have:
**if (string)ViewState["PopUpSelection"] == "yes_no"
{
ButtonConfirmYes.Click += new EventHandler(MyMethod);
}**
And if method "MyMethod" is called from a FORM button, everything "will go ok".
If you call the same method from PopUp button: changes are visible by codebehind, but on the screen nothing happens, unless i press ANY FORM BUTTON, whatever it does

ScriptManager.RegisterStartupScript On Button Click (within updatepanel) Does not fire on FIRST click

My project is essentially a web page with a menu and an updatepanel. The user selects a choice from the menu, and a usercontrol is loaded into the updatepanel. I would prefer not to use an update panel, but, there are multiple databound controls that contain information that will then change the contents of another databound control. Anyways, my project calls for a message box to appear on the screen, which I have no issue in getting to work:
string s= "<script>alert('HayGuise');</script>";
ScriptManager.RegisterClientScriptBlock(c.Page, c.Page.GetType(), GUID.NewGUID().ToString(),s,false);
Now, I've tried a number of different tactics, including replacing GUID with a static string; I've used RegisterStartupScript, etc. My issue is that the message box does not show on the first click of the call button AFTER the updatepanel has updated to show a new usercontrol. But, if I click the button again, everything is great. If this is done on the very first form loaded into the updatepanel (opening up the site and selecting from the menu), everything works fine; it is only when the updatepanel updates again with a new form, that this will occur, and again, only on the first click.
Additionally, the code is being fired, and all code that follows the ScriptManager code fires, but the message box will not show until the button is clicked again. As I am testing this behavior at the moment, I've added the popup code to each of the buttons on the form, and the behavior is the same with each of them. It sucks that its taken so long to implement a reliable piece of code (for the messagebox) only to have this slight issue, and this is after I just figured out why my button events were firing twice on a single click. I pretty sure that it's going to be something simple, some sort of property that I'm not familiar with that needs to be set correctly. Damn, I'd love to just be able to settle all these little issues so I can proceed with the rest of the project. Sooooo close. Thank you for any help.
Edit: Additionally, the buttons are located in another usercontrol that is added to the usercontrol form that is shown in the updatepanel.
Edit II:
Thanks for your input, I apologize for getting back to this so late, I moved on to other parts of the project and just came back to this. The issue is not the startupscript method, nor the clientscriptblck method; the issue resides in the loss of value/viewstate/clientid's on postback.
At the time I posted this question, this project was a single page application. I tested functionality by creating a multipage application, and just kept going from there, vowing to come back to this issue. Well, I'm back and the issue does not reside in the update panel, it has something to do with my code, as I clear out the update panel (tried with a regular panel as well) and then reload the new usercontrol. When cleared, something goes haywire, and nothing fires on the first postback. Its a weird issue, that I have yet to find much (a couple articles) information on that is specific to just the first postback causing this issue. I'll update with an answer if I ever find one; the only fix I've found is to add a line to the menu select code, Server.Transfer(thecurrentpage), essentially refreshing the page. This solves all of the issues, but it seems a bit in poor taste. Thanks again for your input.
If you are working with AJAX UpdatePanel, following code should work for you. In my case, I was using update panel and none of the about worked except this one.
ScriptManager.RegisterStartupScript(myUpdatePanelID, myUpdatePanelID.GetType(), "myFunction", "myFunction();", true);
If you are using update panel in your page then the following code should work for you. You should use the RegisterStartupScript in the whole page using "this".ScriptManager.RegisterStartupScript(this, this.GetType(), "myFunction", "myFunction();", true);

Looking for controls on parent page from DevExpress ASPXPopUpControl

good sirs!
I've been messing around with the next scenario:
First, I have a webform structured as a WebForm containing a DevExpress ASPXPopUpControl and some other controls. Inside the PopUpControl there is a UserControl (lets call it ucA) containing some other controls and a UserControl (called ucB) that contains a cursed ASPxHtmlEditor (added because it's a new requirement).
When the user hits a button on main webform I show the PopUp (originally was a jQuery dialog but since HTMLEditor messes up with jQuery I've been forced to break the standard and use the popup) which contains the ucA. The user fills some fields in ucA and hit the save button. After user hits, I save some dataz and at this point I need to recover a textbox value placed in the webform.
I'm using Parent.FindControl["myTextBox"] but it considers the popupcontrol as parent. When I was using jQuery (before implementing the editor) it worked like a charm.
I feel it's something trivial but thrust me when I say that this stole many hours of research.
Thanks in advance.
EDIT I forgot to mention that I want to look for another UserControl at main webform. This uc its used to display core messages to the user so when he hits the save button, save happens, popup is closed and i look (Parent.FindControl("myUCMessageBoard")) from the ucA for the usercontrol to display a "Transaction complete" message.
I'm thinking you're going to have to do something a little hacky, by using ViewState. If I understand correctly, you are trying to get access to a TextBox's Text on the Web Form, from a UserControl nested within a PopupControl (so you can't traverse all the way up to Web Form Level).
So, what I'd do at some point in the process is store the text in a ViewState variable that you can access from the User Control. It's not optimal, but since you're already hacking to get it to work, what's a little more hacking?
You should expose all controls from ucA as properties, then look for the control inside the DevxPopup the same way you doing. Given that all the controls that you need at the ucA has properties to access them, you could do all the logic you need!
Example:
public ucA : UserControl
{
public string myTextBoxText
{
get
{
return ((TextBox)Controls.FindControl("myTextBox")).Text;
}
}
/*And lot of controls*/
}
Then you looking for the popup at the Form
var ucA = (UcA)Form.Controls.FindControl("myPopup").Controls.FindControl("myucA");
ucA.myTextBoxText = /*Do stuff here with the text*/
Hopes this help you!

Categories

Resources